Python 3.6 我有一个简单的对象列表:
for obj in obj_ts:
print(obj['address'])
这告诉我:
mwpJCSEEkphA1utQGA2Y9Vx8cufv85CgpR
mwpJCSEEkphA1utQGA2Y9Vx8cufv85CgpR
mwpJCSEEkphA1utQGA2Y9Vx8cufv85CgpR
mwpJCSEEkphA1utQGA2Y9Vx8cufv85CgpR
mwpJCSEEkphA1utQGA2Y9Vx8cufv85CgpR
mwpJCSEEkphA1utQGA2Y9Vx8cufv85CgpR
mwpJCSEEkphA1utQGA2Y9Vx8cufv85CgpR
mhRhCZBUP1BsrL6xuXSeAeCNnXcoBHWKAB
mwpJCSEEkphA1utQGA2Y9Vx8cufv85CgpR
mwpJCSEEkphA1utQGA2Y9Vx8cufv85CgpR
mwpJCSEEkphA1utQGA2Y9Vx8cufv85CgpR
mwpJCSEEkphA1utQGA2Y9Vx8cufv85CgpR
然后我想按这个对象分组:
for address, t_list in itertools.groupby(obj_ts, key=lambda obj: obj['address']):
print(address)
但我得到3组而不是2组。
mwpJCSEEkphA1utQGA2Y9Vx8cufv85CgpR
mhRhCZBUP1BsrL6xuXSeAeCNnXcoBHWKAB
mwpJCSEEkphA1utQGA2Y9Vx8cufv85CgpR
答案 0 :(得分:1)
评论解决方案:
正如groupby docs的第1段所述,你需要 在传递数据之前对数据(具有相同的键功能)进行排序 GROUPBY
https://docs.python.org/3/library/itertools.html#itertools.groupby