Check if something in a dictionary is the same as the max value in that dictionary?

泄露秘密 提交于 2019-12-25 09:45:23

问题


How can I check if something in a dictionary is the same as the max in that dictionary. In other words, get all the max values instead of the max value with lowest position. I have this code which returns the max variable name and value:

d = {'g_dirt4': g_dirt4, 'g_destiny2': g_destiny2, 'g_southpark': g_southpark, 'g_codww2': g_codww2, 'g_bfront2': g_bfront2, 'g_reddead2': g_reddead2, 'g_fifa18': g_fifa18, 'g_motogp17': g_motogp17, 'g_elderscrolls': g_elderscrolls, 'g_crashbandicoot': g_crashbandicoot} 
print("g_dirt4", g_dirt4, "g_destiny2", g_destiny2, "g_southpark", g_southpark, "g_codww2", g_codww2, "g_bfront2", g_bfront2, "g_reddead2", g_reddead2, "g_fifa18", g_fifa18, "g_motogp17", g_motogp17, "g_elderscrolls", g_elderscrolls, "g_crashbandicoot", g_crashbandicoot)
print (max(d.items(), key=lambda x: x[1]))

Now it prints the variable with the highest value plus the value itself, but what if there are two or three variables with the same max value? I would like to print all of the max values.

Edit:

The user has to fill in a form, which adds values to the variables in the dictionary. When the user is done, there will be one, two or more variables with the highest value. For example, the code gives me this:

2017-06-08 15:05:43 g_dirt4 9 g_destiny2 8 g_southpark 5 g_codww2 8 g_bfront2 8 g_reddead2 7 g_fifa18 8 g_motogp17 9 g_elderscrolls 5 g_crashbandicoot 6
2017-06-08 15:05:43 ('g_dirt4', 9)

Now it tells me that g_dirt4 has the highest value of 9, but if you look at motogp17, it also had 9 but it doesn't get printed because it's at a higher position in the dictionary. So how do I print them both? And what if it has 3 variables with the same max value?


回答1:


Given a dictionary

d = {'too': 2, 'one': 1, 'two': 2, 'won': 1, 'to': 2}

the following command:

result = [(n,v) for n,v in d.items() if v == max(d.values())]

yields: [('too', 2), ('two', 2), ('to', 2)]




回答2:


Let me introduce you to a more complicated but more powerful answer. If you sort your dictionary items, you can use itertools.groupby for some powerful results:

import itertools
foo = {"one": 1, "two": 2, "three": 3, "tres": 3, "dos": 2, "troi": 3}
sorted_kvp = sorted(foo.items(), key=lambda kvp: -kvp[1])
grouped = itertools.groupby(sorted_kvp, key=lambda kvp: kvp[1])

The sorted line takes the key/value pairs of dictionary items and sorts them based on the value. I put a - in front so that the values will end up being sorted descending. The results of that line are:

>>> print(sorted_kvp)

[('tres', 3), ('troi', 3), ('three', 3), ('two', 2), ('dos', 2), ('one', 1)]

Note, as the comments said above, the order of the keys (in this case, 'tres', 'troi', and 'three', and then 'two' and 'dos', is arbitrary, since the order of the keys in the dictionary is arbitrary.

The itertools.groupby line makes groups out of the runs of data. The lambda tells it to look at kvp[1] for each key-value pair, i.e. the value.

At the moment, you're only interested in the max, so you can then do this:

max, key_grouper = next(grouped)
print("{}: {}".format(max, list(key_grouper)))

And get the following results:

3: [('tres', 3), ('troi', 3), ('three', 3)]

But if you wanted all the information sorted, instead, with this method, that's just as easy:

for value, grouper in grouped: 
    print("{}: {}".format(value, list(grouper)))

produces:

3: [('tres', 3), ('troi', 3), ('three', 3)]

2: [('two', 2), ('dos', 2)]

1: [('one', 1)]

One last note: you can use next or you can use the for loop, but using both will give you different results. grouped is an iterator, and calling next on it moves it to its next result (and the for loop consumes the entire iterator, so a subsequent next(grouped) would cause a StopIteration exception).




回答3:


You could do something like this:

max_value = (max(d.items(), key=lambda x: x[1]))[1]
max_list = [max_value]

for key, value in d.items():
    if value == max_value:
        max_list.append((key, value))

print(max_list)

This will get the maximum value, then loop through all the keys and values in your dictionary and add all the ones matching that max value to a list. Then you print the list and it should print all of them.



来源:https://stackoverflow.com/questions/44439954/check-if-something-in-a-dictionary-is-the-same-as-the-max-value-in-that-dictiona

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!