kn = nx.karate_club_graph() kn.nodes(数据=真) [(0,{'club':'Officer'}),(1,{'club':'Mr. Hi'}),(2,{'club':'Mr. Hi'}),(3, {'club':'嗨先生'}),(4,''俱乐部':'嗨先生'}),(5,{'俱乐部':'官'}),(6,{'club' :'军官'}),(7,''俱乐部':'先生嗨'})]
我希望输出是字典,首先由'先生排序嗨'俱乐部然后由第二个。附:我正在使用Python 3.0
答案 0 :(得分:0)
看起来你只是想对那些元组列表进行排序。因此,在这种情况下,您只需要将sorted
与key
的lambda一起使用。一个例子:
mydata = [(0, {'club': 'Officer'}), (1, {'club': 'Mr. Hi'}), (2, {'club': 'Mr. Hi'}), (3, {'club': 'Mr. Hi'}), (4, {'club': 'Mr. Hi'}), (5, {'club': 'Officer'}), (6, {'club': 'Officer'}), (7, {'club': 'Mr. Hi'})]
mydata_sorted = sorted(mydata, key=lambda x: x[1]['club'])
print(mydata_sorted)
将显示
[(1, {'club': 'Mr. Hi'}), (2, {'club': 'Mr. Hi'}), (3, {'club': 'Mr. Hi'}), (4, {'club': 'Mr. Hi'}), (7, {'club': 'Mr. Hi'}), (0, {'club': 'Officer'}), (5, {'club': 'Officer'}), (6, {'club': 'Officer'})]