2 回答

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
results = {'agunii2035': ['agunii3007', 'agunii2006', 'agunii2003', 'agunii3000'],
'agunii3007': ['agunii2035', 'agunii2006', 'agunii2003', 'agunii3000'],
'agunii2006': ['agunii2035', 'agunii3007', 'agunii2003', 'agunii3000'],
'agunii2003': ['agunii2035', 'agunii3007', 'agunii2006', 'agunii3000'],
'agunii3000': ['agunii2035', 'agunii3007', 'agunii2006', 'agunii2003']}
score = {'agunii2035': [4, 4, 1, 3],
'agunii3007': [4, 3, 3, 1],
'agunii2006': [4, 3, 2, 2],
'agunii2003': [1, 3, 2, 1],
'agunii3000': [3, 1, 2, 1]}
ordered_results = {key:[item for _, item in
sorted(zip(score.get(key), value), reverse=True)]
for key, value in results.items()}
print(ordered_results)
輸出
{'agunii2035': ['agunii3007', 'agunii2006', 'agunii3000', 'agunii2003'],
'agunii3007': ['agunii2035', 'agunii2006', 'agunii2003', 'agunii3000'],
'agunii2006': ['agunii2035', 'agunii3007', 'agunii3000', 'agunii2003'],
'agunii2003': ['agunii3007', 'agunii2006', 'agunii3000', 'agunii2035'],
'agunii3000': ['agunii2035', 'agunii2006', 'agunii3007', 'agunii2003']}

TA貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個(gè)贊
您需要做的就是使用從分?jǐn)?shù)中查找字典的值作為對(duì)這樣的結(jié)果進(jìn)行排序的鍵。這是可行的,因?yàn)槟J(rèn)排序算法可以使用列表作為排序依據(jù)。key: values然后,它使用字典理解從排序列表中構(gòu)建一個(gè)新字典。
results = {
'agunii2035': ['agunii3007', 'agunii2006', 'agunii2003', 'agunii3000'],
'agunii3007': ['agunii2035', 'agunii2006', 'agunii2003', 'agunii3000'],
'agunii2006': ['agunii2035', 'agunii3007', 'agunii2003', 'agunii3000'],
'agunii2003': ['agunii2035', 'agunii3007', 'agunii2006', 'agunii3000'],
'agunii3000': ['agunii2035', 'agunii3007', 'agunii2006', 'agunii2003']
}
score = {
'agunii2035': [4, 4, 1, 3],
'agunii3007': [4, 3, 3, 1],
'agunii2006': [4, 3, 2, 2],
'agunii2003': [1, 3, 2, 1],
'agunii3000': [3, 1, 2, 1]
}
sorted_results = {
k: v
for k, v
in sorted(results.items(), key=lambda item: score[item[0]])
}
print(sorted_results)
# {'agunii2003': ['agunii2035', 'agunii3007', 'agunii2006', 'agunii3000'], 'agunii3000': ['agunii2035', 'agunii3007', 'agunii2006', 'agunii2003'], 'agunii2006': ['agunii2035', 'agunii3007', 'agunii2003', 'agunii3000'], 'agunii3007': ['agunii2035', 'agunii2006', 'agunii2003', 'agunii3000'], 'agunii2035': ['agunii3007', 'agunii2006', 'agunii2003', 'agunii3000']}
添加回答
舉報(bào)