[英]How to get max value for a specific key from a python dictionary
I have a python dict as below: 我有一个Python字典,如下所示:
a={
u'data': [
[u'2013-03-19', u'1363762799', u'4004658'],
[u'2013-03-20', u'1363849199', u'4756668']
],
u'columns': [
u'dt_pst',
u'timestamp',
u'count'
]
}
How can I get the max value for key u'timestamp'? 如何获得密钥u'timestamp'的最大值? I want this number: 1363849199.
我想要这个电话号码:1363849199。
Thanks! 谢谢!
Convert it into a usable format first: 首先将其转换为可用格式:
>>> d = {row[0]: row[1:] for row in zip(a['columns'], *a['data'])}
>>> d
{u'count': (u'4004658', u'4756668'),
u'dt_pst': (u'2013-03-19', u'2013-03-20'),
u'timestamp': (u'1363762799', u'1363849199')}
If you're using 2.6, dict comprehensions won't work. 如果您使用的是2.6,那么dict理解将无法正常工作。 You need to use the
dict()
constructor: 您需要使用
dict()
构造函数:
d = dict((row[0], row[1:]) for row in zip(a['columns'], *a['data']))
Now, you can just use max
: 现在,您可以使用
max
:
>>> max(d['timestamp'], key=int)
u'1363849199'
>>> max(map(int, d['timestamp']))
1363849199
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.