列表包含字典,它们的值也是列表。如何从值中打印最大值和国家/地区名称。
data = [{'country':[2,'US']},{'country':[1,'EN']}]
示例:值 2 是最大的,国家是“美国”,所以它应该打印而不是另一个字典。我运行了下面的代码,但没有得到关于如何提高效率的预期结果。
for i in data:
for m,j in i.items():
country = j[1][1]
points = j[1][0]
Example Output:
points = 2
country = 'US'
最佳答案
尝试:
data = [{'country': [2, 'US']}, {'country': [1, 'EN']}]
max_dic = max(data, key=lambda x: x['country'][0])
points, country = max_dic['country']
print(country)
print(points)
https://stackoverflow.com/questions/69379677/