1

i have the following list which can contain multiple dictionaries of different sizes.
The keys in each dictionary are unique, but one key may exist in different dictionaries.
Values are unique across dictionaries.

I want to trim down my dictionaries so that they contain the keys and values for which the value is the highest among all dictionaries.

For example, the key '1258' exists in three of the four dictionaries, and it has the highest value only in the last one, so in the reconstructed list, this key and its value will be in the last dictionary only. If the key doesn't exist in other dictionaries, then it will remain in the dictionary where it belongs to. here is sample data:

[{'1258': 1.0167004,
  '160': 1.5989301000000002,
  '1620': 1.3058813000000002,
  '2571': 0.7914598,
  '26': 4.554409,
  '2943': 0.5072369,
  '2951': 0.4955711,
  '2952': 1.2380746000000002,
  '2953': 1.6159719,
  '2958': 0.4340355,
  '2959': 0.6026906,
  '2978': 0.619001,
  '2985': 1.5677016,
  '3075': 1.04948,
  '3222': 0.9721148000000001,
  '3388': 1.680108,
  '341': 0.8871856,
  '3443': 0.6000103,
  '361': 2.6682623000000003,
  '4': 5.227341,
  '601': 2.2614983999999994,
  '605': 0.6303175999999999,
  '9': 5.0326675},
 {'1457': 5.625237999999999,
  '1469': 25.45585200000001,
  '1470': 25.45585200000001,
  '160': 0.395728,
  '1620': 0.420267,
  '2571': 0.449151,
  '26': 0.278281,
  '601': 0.384822,
  '605': 5.746278700000001,
  '9': 1.487241},
 {'1258': 0.27440200000000003,
  '1457': 0.8723639999999999,
  '1620': 0.182567,
  '2571': 0.197134,
  '2943': 0.3461654,
  '2951': 0.47372800000000004,
  '2952': 0.6662919999999999,
  '2953': 0.6725458,
  '2958': 0.4437159,
  '2959': 0.690856,
  '2985': 0.8106226999999999,
  '3075': 0.352618,
  '3222': 0.7866500000000001,
  '3388': 0.760664,
  '3443': 0.129771,
  '601': 0.345448,
  '605': 1.909823,
  '9': 0.888999},
 {'1258': 1.0853083,
  '160': 0.622579,
  '1620': 0.7419095,
  '2571': 0.9828758,
  '2943': 2.254124,
  '2951': 0.6294688,
  '2952': 1.0965362,
  '2953': 1.8409954000000002,
  '2958': 0.7394122999999999,
  '2959': 0.9398920000000001,
  '2978': 0.672122,
  '2985': 1.2385512999999997,
  '3075': 0.912366,
  '3222': 0.8364904,
  '3388': 0.37316499999999997,
  '341': 1.0399186,
  '3443': 0.547093,
  '361': 0.3313275,
  '601': 0.5318834,
  '605': 0.2909876}]
timgeb
  • 76,762
  • 20
  • 123
  • 145
passion
  • 1,000
  • 6
  • 20
  • 47

1 Answers1

3

Here's one approach. I shortened your example to one that's easier to reason about.

>>> dcts = [
... {1:2, 3:4, 5:6},
... {1:3, 6:7, 8:9},
... {6:10, 8:11, 9:12}]
>>> 
>>> [{k:v for k,v in d.items() if v == max(d.get(k) for d in dcts)} for d in dcts]
[{3: 4, 5: 6}, {1: 3}, {8: 11, 9: 12, 6: 10}]

edit:

more efficient because the max is only computed once for each key:

>>> from operator import or_
>>> from functools import reduce
>>> allkeys = reduce(or_, (d.viewkeys() for d in dcts))
>>> max_vals = {k:max(d.get(k) for d in dcts) for k in allkeys}
>>> result = [{k:v for k,v in d.items() if v == max_vals[k]} for d in dcts]
>>> result
[{3: 4, 5: 6}, {1: 3}, {8: 11, 9: 12, 6: 10}]
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • is it possible to change it so that the dictionaries in results are ordered by their values descendingly? descending order within each dictionary. – passion Apr 21 '17 at 07:25
  • @passion dictionaries are unordered. – timgeb Apr 21 '17 at 08:05
  • yeah, i mean, get a list of tuples but in descending order by value. – passion Apr 21 '17 at 08:09
  • the goal is to return the first four keys with the highest values, for each dictionary – passion Apr 21 '17 at 08:12
  • 1
    @passion sure, that's possible, but a different question better not answered in a comment. Feel free to ask a new one. – timgeb Apr 21 '17 at 13:06
  • i have already asked here http://stackoverflow.com/questions/43536793/python-list-of-dictionaries-sort-descending-by-values please have a look – passion Apr 21 '17 at 13:19