0

I just wanted to sort this dict by value of runs scored by the player, but I'm not getting it how to do this.

orangecap({'test1':{'Ashwin':84, 'Kohli':120}, 'test2':{'ashwin':59, 'Pujara':42}})

here "orangecap" is the def for the function to be called, and I want to return the name of highest scorer's name (in this case: Kohli)..

Can this be done without using external libraries?

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Hououin Kyouma
  • 163
  • 1
  • 2
  • 8

2 Answers2

2

Setup

dict_of_dicts = {
    'test1': {'Ashwin' :84, 'Kohli': 120}, 
    'test2': {'ashwin': 59, 'Pujara': 42}, 
    'test3': {'Pandas': 120, 'R': 119}
}

Solution

It appears that you have a dict of dicts (not a list). You need to iterate through each value in the list and see if it is higher that the max value (initially set to None, along with the name of the player and the first key, e.g. test1, test2, etc.). If it is, reset their values to the new max value and append the player name and key to the list holding those who obtained the max value (to account for ties, e.g. Kohli & Pandas).

def orangecap(dict_of_dicts):
    max_key_name = []
    max_score = None
    for key in dict_of_dicts:
        for name, score in dict_of_dicts[key].iteritems():  # .items() in Python 3.
            if score > max_score:
                max_key_name = [(key, name)]
                max_score = score
            elif score == max_score:
                max_key_name.append((key, name))
    return max_key_name, max_score

>>> orangecap(dict_of_dicts=dict_of_dicts)
([('test1', 'Kohli'), ('test3', 'Pandas')], 120)
Alexander
  • 105,104
  • 32
  • 201
  • 196
  • what is "iteritems", I didn't get that. – Hououin Kyouma Sep 05 '18 at 06:45
  • Your question _was_ tagged both Python 2.7 and Python 3.x, so I wasn't sure what version you are using. The behavior is the same, but the function name changed from `iteritems` in Python 2 to `items` in Python 3. This function returns an iterator of tuple pairs of keys and values in your dictionary, e.g. ('Adwin', 84), ('Kohli', 120), etc. https://stackoverflow.com/questions/10458437/what-is-the-difference-between-dict-items-and-dict-iteritems – Alexander Sep 05 '18 at 06:50
0

A very simple solution for this would be to iterate over the dict of dict, use two variables to store runs and player, like this -

def orangecap(your_dict):
    max_runs = 0
    player_scored = ''

    for match, stats in your_dict.items():
        for player, runs in stats.items():
            if runs > max_runs:
                max_runs = runs
                player_scored = player
    return player_scored
Sushant
  • 3,499
  • 3
  • 17
  • 34
  • 1
    "This answer is not useful" downvoter, please explain how? – Sushant Sep 05 '18 at 06:21
  • Your answer doesn't sort the dictionary – Sayse Sep 05 '18 at 06:27
  • can you please explain this, I'm new to python and I didn't get it how it is working correctly.. and thanks for the answer. @ThatBird – Hououin Kyouma Sep 05 '18 at 06:30
  • 1
    @Sayse Sorting the dictionary is not a requirement but only a means to an end, which is to extract the max value and corresponding name. – Alexander Sep 05 '18 at 06:42
  • @Alexander - Without knowing the OP's full requirements, that is impossible to say for certain. – Sayse Sep 05 '18 at 06:44
  • It's iterating through the dictionaries, storing the maximum run it finds during iteration in a variable. Since you're new to python, go through [this](https://docs.python.org/3/tutorial/datastructures.html#dictionaries). Also, I think you need to return the max run scored, you don't need to sort the dictionaries so correct your question and consider accepting one of the answers – Sushant Sep 05 '18 at 06:46
  • @Sayse OP just said " I didn't get it how it is working correctly" so I think that's what OP wanted – Sushant Sep 05 '18 at 06:49
  • @Sayse Given the OPs requirement " I want to return the name of highest scorer's name", one could reasonably infer that "sorting" the dictionaries is just a means to this end. – Alexander Sep 05 '18 at 06:53