根据其他词典的值对词典的值进行排序

时间:2020-10-27 07:08:23

标签: python dictionary

我有一本像这样的字典。

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]}

我想根据“得分”字典的值对“结果”字典的值进行排序。

例如,

对于键“ agunii2035”,乐谱字典具有值 [ 4,4,1,3]。因此,我想使用与['agunii3007', 'agunii2006', 'agunii3000','agunii2003']相同的键而不是 ['agunii3007', 'agunii2006', 'agunii2003', 'agunii3000']对“结果”字典的值进行排序。

2 个答案:

答案 0 :(得分:1)

您需要做的就是使用score的字典查找值作为排序结果的键。这是可行的,因为默认的排序算法可以使用列表进行排序。然后,它使用字典理解功能从排序列表中使用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']}

答案 1 :(得分:1)

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']}