0

I have a dictionairy that is build as

>> dict = {'foo':[20,15],'bar':[10,5],'is':[35,3],'a':[20,10],'word':[50,1]}

I want to find the key that has the highest of list value [0] and the lowest of value [1] .. (or an approximation of it) but its giving me a total brainfreeze

So for this example the desired result would be

>> 'word':[50,1]

It has been suggested I should more clearly define my paramaters: right now I'm looking to print the top 10 highest results from the [0] value as long as the second value remains below 5

Thank you for taking the time to read the question

  • 2
    I don't think you provided enough information. Which element should be returned if you have such dict: `dict = { 'foo': [20,1], 'word': [50, 10]}`? – zanderle Sep 27 '15 at 15:44
  • Its hard to say because its about the relative difference, the higher the first value is (words in corpus) the higher the second value (documents pertaining word) can be. The eventual goal is to look for a result that has the highest [0] with the lowst [1] .. sorry for being unclear . I was Thinking i'd get the highest number of either (or with the highest relative distance) and then pick one manually – Pressing the buttons Sep 27 '15 at 16:09
  • You might want to explore SO question [How do I sort a list of dictionaries by values of the dictionary in Python?](http://stackoverflow.com/q/72899/2823755) and the answers provided. And take a look at the [Sorting How To Wiki](https://wiki.python.org/moin/HowTo/Sorting). – wwii Sep 27 '15 at 16:47
  • Seems like if you don't clearly define your requirements you won't be able to write anything. – wwii Sep 27 '15 at 16:51
  • Attempted to be more clear as to what I am looking for. – Pressing the buttons Sep 27 '15 at 17:00

2 Answers2

2

You can use max function with a proper key function :

>>> max(dict.items(),key=lambda x: (x[1][0],-x[1][1]))
('word', [50, 1])

Note that in this case the priority of x[1][0] (max value) is more than second one,So for some dictionaries like following :

>>> dict = { 'foo': [35,5], 'word': [60, 25]}

It will returns :

('word', [60, 25])

You can also get items based on the difference of the values (which seems more close to what you want):

>>> dict = { 'foo': [70,5], 'word': [68,1]}
>>> max(dict.items(),key=lambda x: (x[1][0]-x[1][1]))
('word', [68, 1])
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • First off, thanks for taking the time to answer the question. I dont fully understand how max(dict.items(),key=lambda x: (x[1][0]-x[1][1])) works .. when I run it on my corpus It returns equal numbers ( which I suppose have the lowest relative distance) – Pressing the buttons Sep 27 '15 at 16:13
  • 1
    @Pressingthebuttons - take a look at the docs for ```max``` - you can write any function that will return the items you are interested in and use it as a [key function](https://wiki.python.org/moin/HowTo/Sorting) for ```max```, key functions can be used for several Python built-ins, like ```sorted```, and other standard library functions.. – wwii Sep 27 '15 at 16:44
  • @wwii will give it a read, thank you. (it looks really handy) – Pressing the buttons Sep 27 '15 at 16:47
0

Try with below code. I am not sure whether it satisfies all your scenarios

    dict = {'a': [20, 10], 'word': [50, 1], 'is': [35, 3], 'foo': [20, 15], 'bar': [10, 5]}

    value = max(dict.values())
    b = value[1]
    for each in dict.values():
        if value[0] == each[0] and each[1] < b:
             value = each

    print (dict.keys()[dict.values().index(value)],value)
Vineesh
  • 253
  • 2
  • 7
  • hey there, first off thank you for answering. I tried to run your answer (in my program the dict is named 'vergelijkdict') If I run it as : `value = max(vergelijkdict.values()) b = value[1] for each in vergelijkdict.values(): if value[0] == each[0] and each[1] < b: value = each print (vergelijkdict.keys()[vergelijkdict.values().index(value)],value)` it gives me an AttributeError: 'dict_values' object has no attribute 'index' – Pressing the buttons Sep 27 '15 at 16:49
  • I think you are using python 3x. In python 3, dict.values(),dict.keys(),dict.items() returns views, not list. try like list(dict.values()) and list(dict.keys()) in all possible places. – Vineesh Sep 28 '15 at 02:08