我有一个字典,每个键后面都是一个存储的列表。 看起来像这样:
dict with values: {
u'New_York': [(u'New_York', u'NY', datetime.datetime(2014, 8, 13, 0, 0), 10), (u'New_York', u'NY', datetime.datetime(2014, 8, 13, 0, 0), 4), (u'New_York', u'NY', datetime.datetime(2014, 8, 13, 0, 0), 3)],
u'Jersy': [(u'Jersy', u'JY', datetime.datetime(2014, 8, 13, 0, 0), 6), (u'Jersy', u'JY', datetime.datetime(2014, 8, 13, 0, 0), 7)],
u'Alameda': [(u'Alameda', u'CA', datetime.datetime(2014, 8, 13, 0, 0), 1), (u'Alameda', u'CA', datetime.datetime(2014, 8, 13, 0, 0), 2), (u'Alameda', u'CA', datetime.datetime(2014, 8, 13, 0, 0), 3), (u'Alameda', u'CA', datetime.datetime(2014, 8, 13, 0, 0), 1)]
}
我想要的是迭代dic列表并在每个KEY的列表的特定位置返回max。结果应该包含KEY和列表中具有最大值的整个元素。完美的方法是将返回的元素存储在一个dic中。
实施例: 这里的最大值是列表的属性最后位置的值。
somedic = {
u'New_York': (u'New_York', u'NY', datetime.datetime(2014, 8, 13, 0, 0), 10)
u'Jersy': (u'Jersy', u'JY', datetime.datetime(2014, 8, 13, 0, 0), 7)
u'Alameda': (u'Alameda', u'CA', datetime.datetime(2014, 8, 13, 0, 0), 3)
}
通过观察这些弗雷德斯,我试了几个想法:
Getting key with maximum value in dictionary?
Max/Min value of Dictionary of List
Python miminum value in dictionary of lists
Python miminum length/max value in dictionary of lists
但是我无法理解它。它超越了我的能力。我刚开始学习python。我试过这样的事情:
import operator
maxvalues = {}
maxvalues = max(countylist.iteritems(), key=operator.itemgetter(1))[0]
print "should be max values here: ", maxvalues
#gave me New York
这可能吗? 我正在研究Python 2.7 如果代码剪掉一个帖子作为答案可以解释,那将是很棒的,因为我想学习一些东西!
顺便说一下,我不是在寻找准备使用的代码。一些提示和代码片段对我有用。我将从那里开始工作。这就是我将如何学习的最佳方式。
答案 0 :(得分:1)
max()
接受第二个参数,一个可调用key
,可让您指定如何计算最大值。它为输入iterable中的每个条目调用,其返回值用于查找最高值。您需要对字典中的每个值应用此功能;您在此处找到每个列表的最大值,而不是字典中所有值的最大值。
对价值使用;其余的只是格式化输出;我在这里使用dict comprehension来处理输入中的每个键值对,并再次为输出生成一个字典:
{k: max(v, key=lambda i: i[-1]) for k, v in somedic.iteritems()}
您还可以使用operator.itemgetter()
function为您生成可调用对象,而不是使用lambda
:
from operator import itemgetter
{k: max(v, key=itemgetter(-1)) for k, v in somedic.iteritems()}
两者都抓住每个输入元组的最后一个元素。
演示:
>>> import datetime
>>> from pprint import pprint
>>> somedic = {
... u'New_York': (u'New_York', u'NY', datetime.datetime(2014, 8, 13, 0, 0), 10), (u'New_York', u'NY', datetime.datetime(2014, 8, 13, 0, 0), 4), (u'New_York', u'NY', datetime.datetime(2014, 8, 13, 0, 0), 3)],
... u'Jersy': [(u'Jersy', u'JY', datetime.datetime(2014, 8, 13, 0, 0), 6), (u'Jersy', u'JY', datetime.datetime(2014, 8, 13, 0, 0), 7)],
... u'Alameda': [(u'Alameda', u'CA', datetime.datetime(2014, 8, 13, 0, 0), 1), (u'Alameda', u'CA', datetime.datetime(2014, 8, 13, 0, 0), 2), (u'Alameda', u'CA', datetime.datetime(2014, 8, 13, 0, 0), 3), (u'Alameda', u'CA', datetime.datetime(2014, 8, 13, 0, 0), 1)]
... }
>>> {k: max(v, key=lambda i: i[-1]) for k, v in somedic.iteritems()}
{u'New_York': (u'New_York', u'NY', datetime.datetime(2014, 8, 13, 0, 0), 10), u'Jersy': (u'Jersy', u'JY', datetime.datetime(2014, 8, 13, 0, 0), 7), u'Alameda': (u'Alameda', u'CA', datetime.datetime(2014, 8, 13, 0, 0), 3)}
>>> pprint(_)
{u'Alameda': (u'Alameda', u'CA', datetime.datetime(2014, 8, 13, 0, 0), 3),
u'Jersy': (u'Jersy', u'JY', datetime.datetime(2014, 8, 13, 0, 0), 7),
u'New_York': (u'New_York', u'NY', datetime.datetime(2014, 8, 13, 0, 0), 10)}
答案 1 :(得分:-1)
您能看一下:https://wiki.python.org/moin/HowTo/Sorting#Sorting_Mini-HOW_TO
答案可能是:
In [2]: import datetime
In [3]: d = {
u'New_York': [(u'New_York', u'NY', datetime.datetime(2014, 8, 13, 0, 0), 10), (u'New_York', u'NY', datetime.datetime(2014, 8, 13, 0, 0), 4), (u'New_York', u'NY', datetime.datetime(2014, 8, 13, 0, 0), 3)],
u'Jersy': [(u'Jersy', u'JY', datetime.datetime(2014, 8, 13, 0, 0), 6), (u'Jersy', u'JY', datetime.datetime(2014, 8, 13, 0, 0), 7)],
u'Alameda': [(u'Alameda', u'CA', datetime.datetime(2014, 8, 13, 0, 0), 1), (u'Alameda', u'CA', datetime.datetime(2014, 8, 13, 0, 0), 2), (u'Alameda', u'CA', datetime.datetime(2014, 8, 13, 0, 0), 3), (u'Alameda', u'CA', datetime.datetime(2014, 8, 13, 0, 0), 1)]
}
In [4]: def give_max_values(d):
...: res = {}
...: for key, vals in d.iteritems():
...: res[key] = max(vals, key=lambda x: x[3])
...: return res
In [5]: somedic = give_max_values(d)
In [6]: print somedic
{u'New_York': (u'New_York', u'NY', datetime.datetime(2014, 8, 13, 0, 0), 10), u'Jersy': (u'Jersy', u'JY', datetime.datetime(2014, 8, 13, 0, 0), 7), u'Alameda': (u'Alameda', u'CA', datetime.datetime(2014, 8, 13, 0, 0), 3)}