python sorted函式對元組排序
阿新 • • 發佈:2021-02-05
按照二元組的其中一個元素排序
print('degree sequence',H.degree())
H_degree_sequence = sorted(((n,d) for n, d in H.degree()), reverse=False)
print('This is H_degree_sequence in function', H_degree_sequence)
H.degree()函式返回(node, degree)。在sorted語句中key為n, 對(n,d)進行了排序;
若改為如下程式碼,則變為key=d, 對(d,n)進行了排序;
print('degree sequence',H.degree())
H_degree_sequence = sorted(((d,n) for n, d in H.degree()), reverse=False)
print('This is H_degree_sequence in function', H_degree_sequence)