在字典中查找列表的最大值 [英] Finding the max value of list in dictionary

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

问题描述

我有一个字典,每个键的后面都有一个存储的列表. 看起来像这样:

I have a dict and behind each key is a list stored. Looks like this:

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返回列表中某个位置的最大值.结果应包含KEY和具有最大值的列表的整个元素.完美的方法是将返回的元素也存储在dic中.

What I want is to iterate through the dic lists and return the max in a certain position of the list for each KEY. The result should contain the KEY and the whole element of the list with the max value. Perfect would be to store the returned element in a dic as well.

示例: 最大值,这里的值位于列表属性的最后位置.

Example: Max Value here with the values at the attributes last position of the list.

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)
}

通过看这些弗雷德斯,我尝试了一些想法:

I tried a couple of thinks, by looking at these Freds:

在字典中获取具有最大值的键?

列表字典的最大值/最小值

列表字典中的Python最小值

列表字典中的Python最小长度/最大值

但是我无法回避它.这完全超出了我的能力.我刚刚开始学习python.我尝试过这样的事情:

But I cannot get my head around it. It is simply above my abilities. I just started to learn python. I tried something like this:

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 如果我可以学习一些东西,那么如果将代码删掉一篇文章作为答案可以解释,那就太好了!

Is that possible? I am working on Python 2.7 It would be awesome if the code snipped one posts as an answer could be explained, since I want to learn something!

顺便说一句,我不是在寻找现成的代码.一些提示和代码段对我有用.我将继续努力.这就是我将学到最多的方法.

Btw I am not looking for a ready to use code. Some hints and code snippet work for me. I will work my way from there on through it. That's how I will learn the most.

推荐答案

具有第二个参数,可调用的key,可让您指定如何计算最大值.对于可迭代输入中的每个条目,将调用它,并且其返回值用于查找最大值.您需要将其应用于字典中的每个 value ;您会在此处找到每个 list 的最大值,而不是字典中所有值的最大值.

max() takes a second parameter, a callable key that lets you specify how to calculate the maximum. It is called for each entry in the input iterable and its return value is used to find the highest value. You need to apply this against each value in your dictionary; you are finding the maximum of each individual list here, not the maximum of all values in a dictionary.

将其用于值;其余的只是格式化输出;我在这里使用了 dict理解来处理您的每个输入中的键值对,并再次为输出生成字典:

Use that against the values; the rest is just formatting for the output; I've used a dict comprehension here to process each of your key-value pairs from the input and produce a dictionary again for the output:

{k: max(v, key=lambda i: i[-1]) for k, v in somedic.iteritems()}

您还可以使用 operator.itemgetter()函数为您生成可调用对象,而不是使用lambda:

from operator import itemgetter

{k: max(v, key=itemgetter(-1)) for k, v in somedic.iteritems()}

两者都抓住每个输入元组的最后一个元素.

Both grab the last element of each input tuple.

演示:

>>> 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)}

这篇关于在字典中查找列表的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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