python:具有2个键的字典中的最大值 [英] python: max value in a dictionary with 2 keys

查看:385
本文介绍了python:具有2个键的字典中的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典D设置为 D = {('a','b'):['1000','5','。3'],('c' 'd'):['2000','8',' - 。8']} 其中('a','b')和('c','d'键。我无法找到列表中第一个值的最大值。所以在使用max(D)我需要它返回('c','d')。记住我的列表是数百个配对。我只需要有max()函数能够识别第一个值'2000'和'1000',并找到最大值。任何帮助或建议将不胜感激。

解决方案

您需要遍历字典,将第一个值的项目转换为int ,然后保存它们以供以后使用:

  first_values = [] 
在valval()中的val:
first_values.append(int(val [0]))
print(max(first_values))


I have a dictionary D set up as D={('a','b'):['1000','5','.3'], ('c','d'):['2000','8','-.8']} where ('a','b') and ('c','d') are the keys. I am having trouble finding the maximum of the first values in the lists. So in using max(D) I need it to return ('c','d'). Keep in mind my list is hundreds of pairings. I just need to have the max() function be able to recognize the first value '2000' and '1000' and find the maximum of those. Any help or suggestions would be greatly appreciated.

解决方案

You need to iterate through the dictionary, converting the first value's item to an int, and saving them for later:

first_values = []
for val in D.values():
    first_values.append(int(val[0]))
print(max(first_values))

这篇关于python:具有2个键的字典中的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆