P
P
Pavel2020-06-04 10:29:15
Python
Pavel, 2020-06-04 10:29:15

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']},
]


How can I iterate over it and implement the following structure

list = [
  {'name': 'Pavel', 'id': '233082', 'pos': ['85742', '95380', '40979']},
  ...
  {'name': 'Oleg', 'id': '233082', 'pos': 
  },
  ...
  {'name': 'Nikolay', 'id': '233082', 'pos': ['25781', '94116', '20264']},
]


Thanks in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Pankov, 2020-06-04
@velllum

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'])
    )
]

Only you seem to have an error in the example for cases with single occurrences. Missing double nested list.
If you need it this way, then you will need to add "stripping" single-element lists.
Don't forget to presort if the items being grouped might not be adjacent.

A
aRegius, 2020-06-04
@aRegius

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()]

D
Dmitry, 2020-06-04
@LazyTalent

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 question

Ask a Question

731 491 924 answers to any question