相关疑难解决方法(0)

在字典中获取最大值的密钥?

我有一个dictionary:键是字符串,值是整数.

例:

stats = {'a':1000, 'b':3000, 'c': 100}
Run Code Online (Sandbox Code Playgroud)

我想得到'b'一个答案,因为它是具有更高价值的关键.

我使用具有反向键值元组的中间列表执行以下操作:

inverse = [(value, key) for key, value in stats.items()]
print max(inverse)[1]
Run Code Online (Sandbox Code Playgroud)

这是一个更好(或更优雅)的方法吗?

python dictionary max

786
推荐指数
20
解决办法
74万
查看次数

如何获取具有混合类型值的字典中具有最大日期值的项目?

在字典中a

from datetime import datetime

a = {"ID":3, "CITY":"ATLANTIS", "FOUNDATION":datetime(2014,10,12), "COLLAPSE":datetime(2010,10,12), "REGENERATE":datetime(2011,10,12)}
Run Code Online (Sandbox Code Playgroud)

您如何获取该字典中具有最旧日期的值(在本例中"COLLAPSE":datetime(2010,10,12))?请记住,并非所有值都具有相同的数据类型。

python datetime dictionary max

4
推荐指数
1
解决办法
6375
查看次数

标签 统计

dictionary ×2

max ×2

python ×2

datetime ×1