Python 对列表进行分组

本文旨在解决Python中的列表按相同元素分割的问题

实例数据如下:

s = [
    {'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'F', 'info': randint(80, 100)},
    {'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'A', 'info': randint(80, 100)},
    {'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'D', 'info': randint(80, 100)},
    {'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'A', 'info': randint(80, 100)},
    {'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'A', 'info': randint(80, 100)},
    {'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'B', 'info': randint(80, 100)},
    {'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'B', 'info': randint(80, 100)},
    {'one': 'SIT', 'two': '业务组', 'third': '智能', 'name': 'E', 'info': randint(80, 100)},
    {'one': 'SIT', 'two': '业务组', 'third': '图像', 'name': 'E', 'info': randint(80, 100)},
    {'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'A', 'info': randint(80, 100)},
    {'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'A', 'info': randint(80, 100)},
    {'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'B', 'info': randint(80, 100)},
    {'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'A', 'info': randint(80, 100)},
    {'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'A', 'info': randint(80, 100)}
]
  1. 对于很多博客中用到的 itertools 库中的 groupby 去操作,有部分bug,或许是我没有用正确,如有大佬请指正:
groups = groupby(s, key=lambda x: (x['one'], x["two"], x["third"], x["name"]))
for key, group in groups:
    print(key, list(group))

结果并非是我所想要的,如下: 可以看到 相同 的数据列是分开展示,并没有合在一起展示

('SDC', '业务组', '图像', 'F') [{'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'F', 'info': 89}]
('SDC', '业务组', '图像', 'A') [{'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'A', 'info': 95}]
('SDC', '业务组', '图像', 'D') [{'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'D', 'info': 83}]
('SDC', '业务组', '图像', 'A') [{'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'A', 'info': 98}, {'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'A', 'info': 97}]
('SDC', '业务组', '图像', 'B') [{'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'B', 'info': 95}, {'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'B', 'info': 82}]
('SIT', '业务组', '智能', 'E') [{'one': 'SIT', 'two': '业务组', 'third': '智能', 'name': 'E', 'info': 97}]
('SIT', '业务组', '图像', 'E') [{'one': 'SIT', 'two': '业务组', 'third': '图像', 'name': 'E', 'info': 89}]
('SDC', '业务组', '图像', 'A') [{'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'A', 'info': 93}, {'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'A', 'info': 80}]
('SDC', '业务组', '图像', 'B') [{'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'B', 'info': 84}]
('SDC', '业务组', '图像', 'A') [{'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'A', 'info': 87}, {'one': 'SDC', 'two': '业务组', 'third': '图像', 'name': 'A', 'info': 96}]

所以又换了一种方式去操作,pandas中的 groupby

2. 使用 Pandas 中的 groupby 完美解决,代码如下:
df = pd.DataFrame(s).groupby(['one', 'two', 'third', 'task'])
for k, v in df:
    print(k, len(v.to_dict(orient='records')), [i['info'] for i in v.to_dict(orient='records')])

打印结果如下: 统计信息 或者 其他列的数据都可以随时取出

('SDC', '业务组', '图像', 'A') 7 [100, 90, 91, 82, 84, 99, 83]
('SDC', '业务组', '图像', 'B') 3 [85, 95, 98]
('SDC', '业务组', '图像', 'D') 1 [89]
('SDC', '业务组', '图像', 'F') 1 [83]
('SIT', '业务组', '图像', 'E') 1 [97]
('SIT', '业务组', '智能', 'E') 1 [85]
PS: to_dict()

函数中只需填一个参数:orient即可,但对于写入的orient不同,字典的构造方式也不同,其中有一种是列表形式

  • orient = 'dict' 函数默认,转换后的字典形式:{column:{index:value}}

  • orient = 'list' 转换后字典形式:{column:[values]}

  • orient = 'series' 转换后字典形式:{column:Series(value)}

  • orient = 'split' 转换后字典形式:{'index':[index],'columns':[columns],'data':[values]}

  • orient = 'records' 转换后list形式:[{column:value}...{column:value}]

  • orient = 'index' 转换后字典形式:{index:{column:value}}

对于这些参数,各为大佬可以自行打印测试!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值