gpt4 book ai didi

python - group_by 返回重复的键

转载 作者:太空宇宙 更新时间:2023-11-03 14:34:26 26 4
gpt4 key购买 nike

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

最佳答案

评论中的解决方案:

As mentioned in the 1st paragraph of the groupby docs, you need to sort the data (with the same key function) before passing it to groupby

https://docs.python.org/3/library/itertools.html#itertools.groupby

关于python - group_by 返回重复的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47059364/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com