列表包含字典,它们的值也是列表。如何从值中打印最大值和国家/地区名称。
data = [{'country':[2,'US']},{'country':[1,'EN']}]
Run Code Online (Sandbox Code Playgroud)
示例:在值中,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'
Run Code Online (Sandbox Code Playgroud)
尝试:
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75 次 |
| 最近记录: |