What is a good code for counting the same value values in Python list-map data?

Asked 2 years ago, Updated 2 years ago, 49 views

origin = [
    {'pk':1, 'attr':'MAN'},
    {'pk':1, 'attr':'WOMAN'},
    {'pk':1, 'attr':'BOY'},
    {'pk':1, 'attr':'MAN'},
    {'pk':1, 'attr':'MAN'},
    {'pk':2, 'attr':'MAN'},
]

result = [
    {'pk':1, 'MAN':3, 'WOMAN':1, 'BOY':1},
    {'pk':2, 'MAN':1, 'WOMAN':0, 'BOY':1}
]

The data in the list-map form assigned to the origin variable is The values of the attr key must be counted as assigned to the result variable. However, the result variable may be in the form of a list-map, or only pk may be classified in one map data type. In environments where pandas or data frames are unavailable, How can I use a more effective repeat sentence?

python map list python3

2022-09-20 17:55

3 Answers

You should look at all the attr items in the dataset because the attr values can vary.

origin = [
    {'pk':1, 'attr':'MAN'},
    {'pk':1, 'attr':'WOMAN'},
    {'pk':1, 'attr':'BOY'},
    {'pk':1, 'attr':'MAN'},
    {'pk':1, 'attr':'MAN'},
    {'pk':2, 'attr':'MAN'},
    {'pk':2, 'attr':'BOY'},
    {'pk':2, 'attr':'GIRL'},
]
import itertools as it
import operator as op
from collections import Counter

origin_grouped = it.groupby(origin, key=op.itemgetter('pk'))
origin_count = [{'pk':grouped[0], **dict(Counter([d['attr'] for d in list(grouped[1])]))} for grouped in origin_grouped]

total_keys = dict.fromkeys(set().union(*origin_count), 0)
[dict(total_keys, **d) for d in origin_count]

[{'WOMAN': 1, 'MAN': 3, 'pk': 1, 'GIRL': 0, 'BOY': 1},
 {'WOMAN': 0, 'MAN': 1, 'pk': 2, 'GIRL': 1, 'BOY': 1}]


2022-09-20 17:55


origin = [
    {'pk':1, 'attr':'MAN'},
    {'pk':1, 'attr':'WOMAN'},
    {'pk':1, 'attr':'BOY'},
    {'pk':1, 'attr':'MAN'},
    {'pk':1, 'attr':'MAN'},
    {'pk':2, 'attr':'MAN'},
]

counter = dict()

for entry in origin:
    pk = entry['pk']
    attr = entry['attr']
    d = counter.get(pk, dict())
    d[attr] = d.get(attr, 0) + 1
    counter[pk] = d

print(counter)
{1: {'MAN': 3, 'WOMAN': 1, 'BOY': 1}, 
 2: {'MAN': 1}}

It's not a very desired format, but this is possible.


2022-09-20 17:55

Shouldn't we be ignorant about this?

If the pk values are the same, it would be better to use attr as a list rather than adding dict with individual attr values.

origin = [
    {'pk':1, 'attr':'MAN'},
    {'pk':1, 'attr':'WOMAN'},
    {'pk':1, 'attr':'BOY'},
    {'pk':1, 'attr':'MAN'},
    {'pk':1, 'attr':'MAN'},
    {'pk':2, 'attr':'MAN'},
]

a = []
b = {}
for i in origin:
    if i['pk'] not in a:
        a.append(i['pk'])
        b.update({i['pk']:[]})
print(a)
print(b)

for i in origin:
    b[i['pk']].append(i['attr'])
print(b)


result = []
for i in b:
    c = b[i].count('MAN')
    d = b[i].count('WOMAN')
    e = b[i].count('BOY')
    result.append({'pk':i, 'MAN':c, 'WOMAN':d, 'BOY':e})
print(result)

>>> [{'pk': 1, 'MAN': 3, 'WOMAN': 1, 'BOY': 1}, {'pk': 2, 'MAN': 1, 'WOMAN': 0, 'BOY': 0}]


2022-09-20 17:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.