吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 487|回复: 4
收起左侧

[学习记录] 【Python】 list dict数据合并汇总demo

[复制链接]
moduwusuowei 发表于 2024-10-12 15:30
【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[company] = []
​
         classified_case_sensitive[company].append(item_id)
     print(classified_case_sensitive)
​
​
if __name__ == '__main__':
     data_merge(a)结果:
{'CN920A': [214, 225, 239], 'CN920E': [226, 236, 194], 'CN930D': [193, 192], 'CN940F': [191, 190]}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

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

你是否在寻找 pandas

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
0  CN920A  [214, 225, 239]
1  CN920E  [226, 236, 194]
2  CN930D       [193, 192]
3  CN940F       [191, 190]
141442478 发表于 2024-10-12 18:04
[Python] 纯文本查看 复制代码
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[item['company']].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[item['company']].append(item['id'])
    print(classified_case_sensitive)

if __name__ == '__main__':
    data_merge(a)
wkdxz 发表于 2024-10-13 09:44
我加两个不规范数据进去捣乱

[Python] 纯文本查看 复制代码
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[the_company].append(the_id)
        else:
            new_dic[the_company] = [the_id]

print(new_dic)
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-12-14 22:55

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表