avi.ks
avi.ks

Reputation: 77

Getting key corresponding to a max value in a dictionary of lists

I have a dictionary of ~1000 object of the form:

d = {
   'ID1': [[a1, b1, c1, d1, e1, f1], [g1, h1, i1]],
   'ID2': [[a2, b2, c2, d2, e2, f2], [g2, h2, i2]],
}

The keys are strings while the values are lists of integers.

I'd like to find the dictionary keys corresponding to the maximum of each of the following three differences: (bγ - aγ), (dγ - cγ) and (fγ - eγ), where γ is an int in 1:len(d). What would be a efficient/pythonic way of doing it?

Upvotes: 1

Views: 244

Answers (3)

B. M.
B. M.

Reputation: 18628

This seems to work :

data :

d={
 'a0': [[3, 3, 5, 2, 3, 3], [1, 1, 1]],
 'a1': [[1, 0, 1, 7, 5, 2], [1, 1, 1]],
 'a2': [[4, 9, 8, 8, 2, 8], [1, 1, 1]],
 'a3': [[5, 3, 4, 4, 9, 7], [1, 1, 1]],
 'a4': [[5, 5, 6, 7, 9, 9], [1, 1, 1]],
 'a5': [[8, 9, 7, 3, 1, 4], [1, 1, 1]],
 'a6': [[0, 9, 3, 3, 9, 9], [1, 1, 1]],
 'a7': [[3, 5, 1, 8, 7, 1], [1, 1, 1]],
 'a8': [[3, 7, 0, 0, 0, 4], [1, 1, 1]],
 'a9': [[6, 6, 4, 8, 0, 4], [1, 1, 1]]}

A function to calculate differences :

def f(lst):
    if lst:
        a,b = lst.pop(),lst.pop()
        l=f(lst)
        l.append(a-b)
        return l
    else:
        return []

A four steps resolution :

l1=[f(d[n][0][:]) for n in d.keys()]        

l2=[[l[i] for l in l1] for i in range(3)]

l3=[l.index(max(l)) for l in l2]

l4=list(d.keys())

For

print(l3,[l4[i] for i in l3]) 
[6, 7, 2] ['a6', 'a7', 'a2']   

or in 3 lines with pandas :

d1={ n : d[n][0] for n in d.keys()}   # prepare 
df=pd.DataFrame(d1).T.diff(axis=1).loc[:,1::2]  # sums 
print(df.apply(pd.Series.argmax))   # indexes of max

For :

1    a6
3    a7
5    a2
dtype: object

Upvotes: 0

ahed87
ahed87

Reputation: 1360

or you can do max over a dict.

d = {
   'ID1': [[0, 1, 2, 3, 4, 5], [1, 1, 1]],
   'ID2': [[0, 2, 4, 6, 8, 10], [2, 2, 2]],
}

diff = lambda d: [j-i for i, j in zip(d[:-1], d[1:])]
new_d = {k: max(diff(v[0])) for k, v in d.items()}
my_max_key = max(new_d)

Upvotes: 0

brandonchinn178
brandonchinn178

Reputation: 539

You're gonna need to brute-force it, since your data isn't sorted. But you can do it a little more nicely:

diffs = [
    (k, v[0][1] - v[0][0], v[0][3] - v[0][2], v[0][5] - v[0][4])
    for k, v in d.items()
]
max(diffs, key=lambda t: t[1])[0] # b - a

Upvotes: 1

Related Questions