Answer the question
In order to leave comments, you need to log in
Python how to iterate value by key into list?
Hello, I need professional help!
There is a list structure with dictionaries
list = [
{'name': 'Pavel', 'id': '233082', 'pos': ['85742', '95380', '40979']},
...
{'name': 'Oleg', 'id': '233082', 'pos': ['81052', '8316', '3062']},
{'name': 'Oleg', 'id': '233082', 'pos': ['41052', '5318', '2304']},
{'name': 'Oleg', 'id': '233082', 'pos': ['21050', '1317', '9305.']},
...
{'name': 'Nikolay', 'id': '233082', 'pos': ['25781', '94116', '20264']},
]
list = [
{'name': 'Pavel', 'id': '233082', 'pos': ['85742', '95380', '40979']},
...
{'name': 'Oleg', 'id': '233082', 'pos':
},
...
{'name': 'Nikolay', 'id': '233082', 'pos': ['25781', '94116', '20264']},
]
Answer the question
In order to leave comments, you need to log in
Like this, for example.
from itertools import groupby
[
dict(
name=name,
id=id,
pos=[subitem['pos'] for subitem in subitems]
)
for (name, id), subitems in
groupby(
dic,
lambda item: (item['name'], item['id'])
)
]
from collections import defaultdict
pos_group_dict = defaultdict(list)
for data_dict in data:
pos_group_dict[(data_dict['name'], data_dict['id'])].append(data_dict['pos'])
new_data = [{'name': key[0], 'id': key[1], 'pos': value} for key, value in pos_group_dict.items()]
from pprint import pprint
import pandas as pd
dic = [
{'name': 'Pavel', 'id': '233082', 'pos': ['85742', '95380', '40979']},
{'name': 'Oleg', 'id': '233082', 'pos': ['81052', '8316', '3062']},
{'name': 'Oleg', 'id': '233082', 'pos': ['41052', '5318', '2304']},
{'name': 'Oleg', 'id': '233082', 'pos': ['21050', '1317', '9305.']},
{'name': 'Nikolay', 'id': '233082', 'pos': ['25781', '94116', '20264']},
]
df = pd.DataFrame(dic)
# print(df)
# name id pos
# 0 Pavel 233082 [85742, 95380, 40979]
# 1 Oleg 233082 [81052, 8316, 3062]
# 2 Oleg 233082 [41052, 5318, 2304]
# 3 Oleg 233082 [21050, 1317, 9305.]
# 4 Nikolay 233082 [25781, 94116, 20264]
foo = {'pos': lambda rows: [x for x in rows] if len(rows) > 1 else rows, 'name': 'first', 'id': 'first'}
df = df.groupby('name').aggregate(foo).to_dict(orient='record')
# pprint(df)
# [{'id': '233082', 'name': 'Nikolay', 'pos': ['25781', '94116', '20264']},
# {'id': '233082',
# 'name': 'Oleg',
# 'pos': },
# {'id': '233082', 'name': 'Pavel', 'pos': ['85742', '95380', '40979']}]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question