[Solved] How to print multiple max values? | SoloLearn: Learn to code for FREE!

+1

[Solved] How to print multiple max values?

https://code.sololearn.com/cMgBV9GA7wAA/?ref=app

7/3/2020 2:47:47 AM

Subham

20 Answers

New Answer

+4

Subham try this https://code.sololearn.com/crfOpHZB9qgI/?ref=app

+8

Here a solution using 2 comprehensions: people = {'Jackie':176, 'Wilson':185, 'Saersha':165, 'Roman':185, 'Abram':169} res = dict((k,v) for k, v in people.items() if v == max(people.values())) [print(f'{i[0]:7}: {i[1]}') for i in sorted(res.items())]

+4

If you wanted more than one max, like the three largest, you could do something like this: print(*sorted(kwargs, key=kwargs.get)[-3:])

+3

There are a few ways, depending on what you desire to be multiple max. You could loop over the max() code you have and store or output each value returned and then remove it from the dictionary. If you just wanted all the values that are equal to the max then do a value comparison in the loop and break out of the loop if they are no longer equal or the new value is less than the first etc. If you want all values above a certain value you could filter() the dictionary.

+3

You have to delete/remove the max each time or it will return the same value each time.

+3

+3

https://code.sololearn.com/crfOpHZB9qgI/?ref=app

+3

Lol, don't know what I was thinking (just really tired) , but you don't even need enumerate() since all I used was a sorted key list. Check the code again. It works exactly the same, but I removed enumerate and the omitted index.

+3

HonFu in order to get the correct output for the given sample you'd need to amend your code something like; keys = sorted(sorted(kwargs, key=kwargs.get)[-2:]) for key in keys: print(f"{key}: {kwargs[key]}") The keys returned from the first sort of the values would then need to be sorted as well. Then looped over or output in the correct fashion. Beyond that the only issue would be the scalability due to the hard coded amount of max values for the given dictionary. rodwynnejones you'd still need to sort them from the keys and then output them in the correct format.

+2

𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 Whoops, I'll fix it gimme a min. I moved the pop to the if condition, it can't be there.

+2

+2

def tallest_people(**kwargs): Keymax = {x: y for x, y in kwargs.items() if y == max(kwargs.values())} print(Keymax) tallest_people(Jackie=176, Wilson=185, Saersha=165, Roman=185, Abram=169)

+1

ChaoticDawg ...

+1

Keymax =[] for i in range (len(kwargs )): Keymax.append(max(kwargs, key=kwargs.get)) print(Keymax) Only outputs Wilson

+1

Sample Input 1: Jackie 176 Wilson 185 Saersha 165 Roman 185 Abram 169 Sample Output 1: Roman : 185 Wilson : 185 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥

+1

+1

K it's fixed

+1

from collections import defaultdict def tallest_people(**kwargs): d = defaultdict(list) for key, value in kwargs.items(): d[value].append(key) print(max(d.items())) tallest_people(Jackie=176, Wilson=185, Saersha=165, Roman=185, Abram=169) # (185, ['Wilson', 'Roman'])

+1

Correct output: Roman : 185 Wilson : 185 Your code output: Wilson : 185 Roman : 185 ChaoticDawg.. order seems important

+1

solves ChaoticDawg enumerate is something one should know IMO I have seen so many issues resolved using enumerate. With practice I should visualise enumerate in my codes, hopefully. I was working on def tallest_people(**kwargs): d = defaultdict(list) for key, value in sorted(kwargs.items()): d[value].append(key) print(max(d.items()))