-1

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.

Community
  • 1
  • 1
four-eyes
  • 10,740
  • 29
  • 111
  • 220
  • 1
    Show us the Python code you have tried. –  Aug 13 '14 at 14:33
  • Yes, this is possible, but this isn't a code-writing service. – jonrsharpe Aug 13 '14 at 14:35
  • @jonrsharpe I know that. I do not want a ready to use code. That would not help me at all since I do not learn anything at all through that. A few hints, some code snippet and I work from there on. But I do not know where to start. – four-eyes Aug 13 '14 at 14:38
  • 1
    This isn't really the site for tips and hints either. Though, as long as you've tried to solve your problem I see no problem with your question. – keyser Aug 13 '14 at 14:39
  • Your output sample is invalid; did you want each value to be a list with one entry, or *just* the entry? You have the opening `[` for each value but never close the list literal. – Martijn Pieters Aug 13 '14 at 14:42
  • @MartijnPieters that was typo, sorry. I want the output to be KEY:VALUE – four-eyes Aug 13 '14 at 14:45
  • @keyser I may have some understand questions than what this site is for. jonrsharpe says its not a code writing service - what I am not looking for - and you say its not there for hints and tips either. What would the answer to my question contain then since I cannot expect a code snippet nor useful hints to the solution of my problem? – four-eyes Aug 13 '14 at 14:47
  • You can definitely expect a code snippet – keyser Aug 13 '14 at 14:48
  • @keyser Then I ask jonrsharpe the question. – four-eyes Aug 13 '14 at 14:49
  • @jonrsharpe, please see my comment to keyser 3 comments above! – four-eyes Aug 13 '14 at 14:50
  • @Stophface there is extensive material in the Help Center on [asking questions](http://stackoverflow.com/help/asking), including what is on- and off-topic and how to write a good one. Please read through that. – jonrsharpe Aug 13 '14 at 14:51
  • @jonrsharpe alright. You are refering with your comment to my question before I postet the code I tried and linked the other freds I had a look at to solve the problem. I understand – four-eyes Aug 13 '14 at 15:14

2 Answers2

2

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)}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Ill have a look at this. Thanks a lot! – four-eyes Aug 13 '14 at 14:42
  • A few comprehension questions. What does that mean `{k:...`. why `{}` and a `k:` and is `key` somewhere defined or does python know that it is working with a `dict` and `key` is something python recognzies automatically – four-eyes Aug 13 '14 at 15:00
  • @Stophface: The format is `{: for something in somethingelse – Martijn Pieters Aug 13 '14 at 15:03
  • @Stophface: in the above there is a loop over `somedic.iteritems()`; that produces `(key, value)` pairs in the loop, these are assigned to `k, v` in the `for k, v in somedic.iteritems()` part. – Martijn Pieters Aug 13 '14 at 15:03
  • @Stophface: the `` part is then simply reusing `k`, the key from the `(key, value)` pair from the original dictionary. – Martijn Pieters Aug 13 '14 at 15:04
  • @Stophface: for each iteration of the loop, both the `` and `` expressions are evaluated and form a key-value pair in the new dictionary being produced. – Martijn Pieters Aug 13 '14 at 15:05
  • @Stophface: try out `{i: i for i in range(5)}` for example; that'll produce a dictionary with the keys and values the same, the integers from 0 through to 4. – Martijn Pieters Aug 13 '14 at 15:06
  • Aaaah, I have to read that backwords! Thats messing with my head these loops in one row. Thank you so much for taking your time and explaining that to me! I learned something and I hope I can reproduce that on another problem which surely will occur! – four-eyes Aug 13 '14 at 15:13
-1

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)}
Quentin THEURET
  • 1,222
  • 6
  • 12
  • You need just **one** value, the maximum. Sorting is overkill for that when you can get to the maximum value with the `max()` function directly. `max()` is a O(n) algorithm, sorting takes O(nlogn) time instead. – Martijn Pieters Aug 13 '14 at 14:44