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
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}]
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.
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}]
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
916 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
613 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.