Get maximum values โ€‹โ€‹for categories in a dictionary list

I have a list of dictionaries that look something like this:

example = [
    {'id': 1, 'foo': 10, 'seq': 1, 'val':  0, 'sum':  0} ,
    {'id': 1, 'foo': 94, 'seq': 2, 'val':  5, 'sum':  5} ,
    {'id': 1, 'foo': 32, 'seq': 3, 'val': 32, 'sum': 37} ,
    {'id': 2, 'foo': 10, 'seq': 1, 'val':  0, 'sum':  0} ,
    {'id': 2, 'foo': 43, 'seq': 2, 'val':  3, 'sum':  3} ,
    {'id': 2, 'foo': 71, 'seq': 3, 'val': 14, 'sum': 17} ,
    {'id': 2, 'foo': 32, 'seq': 4, 'val':  6, 'sum': 23} ,
    {'id': 3, 'foo': 26, 'seq': 1, 'val':  0, 'sum':  0} ,
    {'id': 3, 'foo': 38, 'seq': 2, 'val':  8, 'sum':  8}
]

I need three key pieces of information from the list:

1) I need to identify the highest "seq" value for each "id" in the list.

2) Using the results of (1), I need to compare the "sum" value for each highest "seq" value with the other highest "seq" values โ€‹โ€‹and determine which "seq" has the lowest "sum" ,.

3) This process is complicated by the fact that I also need to make a comparison based on the value of โ€œfooโ€, so the comparison in (2) only compares the values โ€‹โ€‹of โ€œsumโ€, where the first and last โ€œfooโ€ values โ€‹โ€‹are the same.

, , "id" 1 2 , "foo" 10 "foo" 32.

- , , , , :

def getMinId(foo1, foo2, exampleList):
    # first limit exampleList to only ids that match the foos
    # next find the minimum 'sum' among the remaining ids

:

{(10,32): 23, (26,38): 8}

, , . , , - "sum" "foo" , , , .

+4
1

( Python 2.7). .

maxvaldict.py

example = [
    {'id': 1, 'foo': 10, 'seq': 1, 'val':  0, 'sum':  0},
    {'id': 1, 'foo': 94, 'seq': 2, 'val':  5, 'sum':  5},
    {'id': 1, 'foo': 32, 'seq': 3, 'val': 32, 'sum': 37},
    {'id': 2, 'foo': 10, 'seq': 1, 'val':  0, 'sum':  0},
    {'id': 2, 'foo': 43, 'seq': 2, 'val':  3, 'sum':  3},
    {'id': 2, 'foo': 71, 'seq': 3, 'val': 14, 'sum': 17},
    {'id': 2, 'foo': 32, 'seq': 4, 'val':  6, 'sum': 23},
    {'id': 3, 'foo': 26, 'seq': 1, 'val':  0, 'sum':  0},
    {'id': 3, 'foo': 38, 'seq': 2, 'val':  8, 'sum':  8}
]

# Dictionary identifying highest 'seq' value for each 'id' in the list.
# {id: [seq, sum, first_foo, last_foo]}
maxiddict = {}
for rowdict in example:
    if not maxiddict.get(rowdict['id']) or rowdict['seq'] > maxiddict.get(rowdict['id'])[0]:
        if not maxiddict.get(rowdict['id']):
            maxiddict[rowdict['id']] = [rowdict['seq'], rowdict['sum'], rowdict['foo'], rowdict['foo']]
        else:
            first_foo = maxiddict[rowdict['id']][2]
            maxiddict[rowdict['id']] = [rowdict['seq'], rowdict['sum'], first_foo, rowdict['foo']]

# Dictionary of groups of maxiddicts grouped by a combined key of first_foo, last_foo
# {'first_foo, last_foo': [{id: [seq, sum, first_foo, last_foo]}]}
groupdict = {}
for k, v in maxiddict.items():
    key = ', '.join([str(v[2]), str(v[3])])
    if not groupdict.get(key):
        groupdict[key] = [dict([(k, v)])]
    else:
        groupdict[key].append(dict([(k, v)]))

# Dictionary of lowest sums of maxidicts entries grouped by combined key of first_foo, last_foo
lowestsumsdict = {}
for groupkey, groupvallist in groupdict.items():
    minsum = min([entry.itervalues().next()[1] for entry in groupvallist])
    lowestsumsdict[groupkey] = minsum

print lowestsumsdict

:

(maxvaldict)macbook:maxvaldict joeyoung$ python maxvaldict.py 
{'26, 38': 8, '10, 32': 23}
0

Source: https://habr.com/ru/post/1611119/


All Articles