Finding the max value of list in dictionary

2019-03-07 02:28发布

问题:

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

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:

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

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

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.

回答1:

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.

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

You could also use the operator.itemgetter() function to produce a callable for you, instead of using a 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.

Demo:

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


回答2:

Can you have a look at : https://wiki.python.org/moin/HowTo/Sorting#Sorting_Mini-HOW_TO

The answer may be :

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