Wolf
Wolf

Reputation: 399

how to find max element from dictionary that has list of lists in python

I'm a newbie to python, i was thinking of writing a function that basically outputs the largest among sublists present inside a dictionary based on a threshold value.

ex: champs = create_champion_index({'a': [[0, 10], [1, 20], [2,15]], 'b': [[0, 20], [1, 15], [2, 10]]}, 2)

 >>champs['a']
   [[1, 20], [2, 15]]
 >>champs['b']
   [[0, 20], [1, 15]]

So based on the threshold value it should output the sublists that has the largest value. In my example, since i mentioned 2 to be my threshold value the output of term 'a' displays the two biggest lists in ascending order.

Upvotes: 0

Views: 270

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180401

If you have small sublists for simplicity you can sort and return the last n sublists after sorting,this presumes biggest to be the sublists with largest sum if you mean has the largest subelement change sum to max:

def create_champion_index(d, n):
    new_d = {}
    # iterate over dict items key/values
    for k, v in d.items():
        # add `n` highest summed sublists from each value/list 
        new_d[k] = sorted(v, key=sum,reverse=True)[:n]
    return new_d # return new dict to access later

champs = create_champion_index({'a': [[0, 10], [1, 20], [2,15]], 'b': [[0, 20], [1, 15], [2, 10]]}, 2)

print(champs['a'])
print(champs['b'])
[[1, 20], [2, 15]]
[[0, 20], [1, 15]]

You can do it in a couple more lines without sorting but for short lists it is fine to use sorted. I also presume you mean descending order not ascending as whether it is sum or max both outputs are in decsending order.

You need to add more logic to catch when a key does not exist and there are not at least n sublists.

Upvotes: 1

dslack
dslack

Reputation: 845

I'm not sure what you mean by "threshold" here, but I'll go with your variable name. So, I understand "threshold" to be the (maximum) number of items from each list to be output, and you want create_champion_index() to be the function that does this. I'm also not sure what you mean by "largest" sublist. You don't mean "longest" because all your sublists are 2 elements long. You might mean the sublist that has the largest individual element in it. I'll assume that this is what you mean.

The following seems to do what you want:

mydict = {'a': [[0, 10], [1, 20], [2,15]], 'b': [[0, 20], [1, 15], [2, 10]]}

def create_champion_index(in_dict, threshold=2):
    from heapq import nlargest
    from itertools import count
    retdict = {}
    for key in in_dict:
        this_list = in_dict[key]
        this_list_sublists_sorted = [sorted(x, reverse=True) for x in this_list]
        max_ndcs = nlargest(threshold, zip(this_list_sublists_sorted, count()))
        ndcs = [x[1] for x in max_ndcs]
        retlist = [this_list[i] for i in ndcs]
        retdict[key] = retlist
    return retdict

champs = create_champion_index(mydict, threshold=2)
print champs['a']
print champs['b']

Upvotes: 0

Related Questions