Reputation: 1510
I have a list that contains a dictionary as its elements. I want to compare the value of a particular key here 'ce_fraction' with the value of the same keys in other dictionary and return the value of key 'ce_symbol' of which the value of 'ce_fraction' is: maximum. For example from the following I need to return 'ce_symbol': 'S:1', as its corresponding value for 'ce_fraction', is maximum between all dictionaries in the list.
d1 = [{'ce_symbol': 'S:1',
'ce_fraction': 0.9826993502161626,
'csm': 0.0,
'permutation': [0]},
{'ce_symbol': 'ST:7',
'ce_fraction': 0.01729545180302663,
'csm': 6.419666329644907,
'permutation': [3, 1, 4, 6, 0, 5, 2]},
{'ce_symbol': 'TL:3',
'ce_fraction': 5.112772362143661e-06,
'csm': 5.01422215914756,
'permutation': [0, 1, 2]},
{'ce_symbol': 'TS:3',
'ce_fraction': 6.631085890198608e-08,
'csm': 7.49194295399119,
'permutation': [0, 2, 1]},
{'ce_symbol': 'TY:3',
'ce_fraction': 1.8897589832589626e-08,
'csm': 7.720504649029379,
'permutation': [0, 1, 2]}]
I have many such lists and need to do for the one having more than one dictionary as elements in the list. I can print the values and keys in a new list but I need to compare them. Could anyone please help me with this. Thanks in advance.
for i in range(len(d1)):
ce_frac = []
symbol = []
if len(d1[i]) >1:
for j in range(len(d1[i])):
ce_frac.append(d1[j]['ce_fraction'])
symbol.append(d1[j]['ce_symbol'])
Upvotes: 0
Views: 57
Reputation: 2680
from operator import itemgetter
d1 = [{'ce_symbol': 'S:1',
'ce_fraction': 0.9826993502161626,
'csm': 0.0,
'permutation': [0]},
{'ce_symbol': 'ST:7',
'ce_fraction': 0.01729545180302663,
'csm': 6.419666329644907,
'permutation': [3, 1, 4, 6, 0, 5, 2]},
{'ce_symbol': 'TL:3',
'ce_fraction': 5.112772362143661e-06,
'csm': 5.01422215914756,
'permutation': [0, 1, 2]},
{'ce_symbol': 'TS:3',
'ce_fraction': 6.631085890198608e-08,
'csm': 7.49194295399119,
'permutation': [0, 2, 1]},
{'ce_symbol': 'TY:3',
'ce_fraction': 1.8897589832589626e-08,
'csm': 7.720504649029379,
'permutation': [0, 1, 2]}]
lis = [(i["ce_fraction"],i["ce_symbol"]) for i in d1]
max_ce_symbol = min(lis,key=itemgetter(1))[1]
print(max_ce_symbol)
Output
'S:1'
Upvotes: 0
Reputation: 20500
If I take your full example as an input, I would start by making a list of symbol and fractions.
ce_frac = []
symbol = []
for d in d1:
for item in d:
ce_frac.append(item['ce_fraction'])
symbol.append(item['ce_symbol'])
Then I find the maximum value in ce_frac list and find it's index, and the answer is the symbol appearing at that index in symbol list
#Get Maximum value of ce_fraction and find it's index
max_frac = max(ce_frac)
max_idx = ce_frac.index(max_frac)
#It's symbol will be present at the same index in symbol
print(symbol[max_idx])
#S:1
Update
For a faster approach, I have sorted the inner list of all the dictionaries in place on the key ce_fraction
, and the first element of your resultant list will be your answer
sorted_list = (sorted(d1[0], key = lambda i: i['ce_fraction'],reverse=True) )
print(sorted_list[0]['ce_symbol'])
#S:1
Upvotes: 1