0

我想对字典进行排序,其中我有一个字符串键,但我的值是元组列表。例如,假设我们有一个字典,其中每个人都映射到他们对不同学科的评分,其中 d.items() 将返回:

('Person':[("Math",5),("Chemistry",10),("History",2)])

有什么办法可以按字母顺序对每个键的值进行排序?例如,d['Person'] 现在会返回:

('Person':[(Chemistry",10),("History",2),("Math",5)])
4

1 回答 1

0

我的解决方案:

arr = {
    'Person': [("Math",5),("Chemistry",10),("History",2)]
}

def customKey(a):
    return a[0]

for i in arr.keys():
    arr[i] = sorted(arr[i], key=customKey)

print arr
于 2013-04-12T08:17:22.210 回答