moduwusuowei 发表于 2024-10-12 15:30

【Python】 list dict数据合并汇总demo

【Python】 list dict数据合并汇总demo需求:
[*]将列表a,根据company进行分类,同一个company的id放到一个列表里面数据:
a = [{
       "id": 214,
       "company": "CN920A"
   },
   {
       "id": 225,
       "company": "CN920A"
   },
   {
       "id": 239,
       "company": "CN920A"
   },
   {
       "id": 226,
       "company": "CN920E"
   },
   {
       "id": 236,
       "company": "CN920E"
   },
   {
       "id": 194,
       "company": "CN920E"
   },
   {
       "id": 193,
       "company": "CN930D"
   },
   {
       "id": 192,
       "company": "CN930D"
   },
   {
       "id": 191,
       "company": "CN940F"
   },
   {
       "id": 190,
       "company": "CN940F"
   }
   ]代码: def data_merge(a):
   classified_case_sensitive = {}

   for item in a:
         company = item['company']
         item_id = item['id']

         if company not in classified_case_sensitive:
             classified_case_sensitive = []

         classified_case_sensitive.append(item_id)
   print(classified_case_sensitive)


if __name__ == '__main__':
   data_merge(a)结果:{'CN920A': , 'CN920E': , 'CN930D': , 'CN940F': }

CIBao 发表于 2024-10-12 17:44


你是否在寻找 pandas

```python
import pandas as pd

# 原始数据
a = [
    {"id": 214, "company": "CN920A"},
    {"id": 225, "company": "CN920A"},
    {"id": 239, "company": "CN920A"},
    {"id": 226, "company": "CN920E"},
    {"id": 236, "company": "CN920E"},
    {"id": 194, "company": "CN920E"},
    {"id": 193, "company": "CN930D"},
    {"id": 192, "company": "CN930D"},
    {"id": 191, "company": "CN940F"},
    {"id": 190, "company": "CN940F"}
]

# 将数据转换为 DataFrame
df = pd.DataFrame(a)

# 按 company 进行分组,并将相同 company 的 id 放到一个列表中
result = df.groupby('company')['id'].apply(list).reset_index()

# 输出最终结果
print(result)
```

```
company            id
0CN920A
1CN920E
2CN930D      
3CN940F      
```

141442478 发表于 2024-10-12 18:04

from collections import defaultdict

# 原始数据
a = [
    {"id": 214, "company": "CN920A"},
    {"id": 225, "company": "CN920A"},
    {"id": 239, "company": "CN920A"},
    {"id": 226, "company": "CN920E"},
    {"id": 236, "company": "CN920E"},
    {"id": 194, "company": "CN920E"},
    {"id": 193, "company": "CN930D"},
    {"id": 192, "company": "CN930D"},
    {"id": 191, "company": "CN940F"},
    {"id": 190, "company": "CN940F"}
]


def data_merge(data):
    classified_case_sensitive = defaultdict(list)
    for item in data:
      classified_case_sensitive].append(item['id'])
    print(classified_case_sensitive)


if __name__ == '__main__':
    data_merge(a)

vethenc 发表于 2024-10-12 22:05

本帖最后由 vethenc 于 2024-10-12 22:06 编辑





`我帮楼上排个版`

```
from collections import defaultdict

# 原始数据
a = [
    {"id": 214, "company": "CN920A"},
    {"id": 225, "company": "CN920A"},
    {"id": 239, "company": "CN920A"},
    {"id": 226, "company": "CN920E"},
    {"id": 236, "company": "CN920E"},
    {"id": 194, "company": "CN920E"},
    {"id": 193, "company": "CN930D"},
    {"id": 192, "company": "CN930D"},
    {"id": 191, "company": "CN940F"},
    {"id": 190, "company": "CN940F"}
]


def data_merge(data):
    classified_case_sensitive = defaultdict(list)
    for item in data:
      classified_case_sensitive].append(item['id'])
    print(classified_case_sensitive)


if __name__ == '__main__':
    data_merge(a)
```

wkdxz 发表于 2024-10-13 09:44

我加两个不规范数据进去捣乱

a = [
    {"id": 214, "company": "CN920A"},
    {"id": 225, "company": "CN920A"},
    {"id": 239, "company": "CN920A"},
    {"id": 226, "company": "CN920E"},
    {"id": 236, "company": "CN920E"},
    {"id": 194, "company": "CN920E"},
    {"id": 193, "company": "CN930D"},
    {"id": 192, "company": "CN930D"},
    {"id": 191, "company": "CN940F"},
    {"id": 190, "company": "CN940F"},
    {"id": 9527},
    {"company": "TTTTTT"}
]

new_dic = {}

for single_dic in a:

    the_company = single_dic.get('company')
    the_id = single_dic.get('id')

    if the_company and the_id:
      if the_company in new_dic:
            new_dic.append(the_id)
      else:
            new_dic =

print(new_dic)
页: [1]
查看完整版本: 【Python】 list dict数据合并汇总demo