2 回答
TA贡献1785条经验 获得超8个赞
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贡献2021条经验 获得超8个赞
您需要做的就是使用从分数中查找字典的值作为对这样的结果进行排序的键。这是可行的,因为默认排序算法可以使用列表作为排序依据。key: values然后,它使用字典理解从排序列表中构建一个新字典。
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']}
添加回答
举报