我试图使用 itertools.groupby 来帮助我按正或负属性对整数列表进行分组,例如:
输入
[1,2,3, -1,-2,-3, 1,2,3, -1,-2,-3]
会回来
[[1,2,3],[-1,-2,-3],[1,2,3],[-1,-2,-3]]
但是如果我:
import itertools
nums = [1,2,3, -1,-2,-3, 1,2,3, -1,-2,-3]
group_list = list(itertools.groupby(nums, key=lambda x: x>=0))
print(group_list)
for k, v in group_list:
print(list(v))
>>>
[]
[-3]
[]
[]
但如果我不list()
groupby 对象,它会正常工作:
nums = [1,2,3, -1,-2,-3, 1,2,3, -1,-2,-3]
group_list = itertools.groupby(nums, key=lambda x: x>=0)
for k, v in group_list:
print(list(v))
>>>
[1, 2, 3]
[-1, -2, -3]
[1, 2, 3]
[-1, -2, -3]
我不明白的是,一个groupby对象是由一对key和_grouper
对象组成的迭代器,一个groupby的list()
调用对象不应使用 _grouper
对象?
即使它确实消耗了,我是如何从第二个元素中获取 [-3]
的?
最佳答案
根据 the docs ,明确指出,推进 groupby
对象会使前一个组不可用(实际上是空的):
The returned group is itself an iterator that shares the underlying iterable with
groupby()
. Because the source is shared, when thegroupby()
object is advanced, the previous group is no longer visible. So, if that data is needed later, it should be stored as a list.
基本上,不是直接使用 list
构造函数对 list
进行验证,而是需要一个将组迭代器转换为 list
的 listcomp s 在推进 groupby
对象之前,替换:
group_list = list(itertools.groupby(nums, key=lambda x: x>=0))
与:
group_list = [(k, list(g)) for k, g in itertools.groupby(nums, key=lambda x: x>=0)]
大多数 itertools
模块类型的设计旨在避免隐式存储数据,因为它们旨在用于潜在的巨大输入。如果所有的石斑鱼都存储了来自输入的所有数据的副本(并且 groupby
对象必须确保追溯填充它们),它会变得丑陋,并且可能会意外地破坏内存。根据 Python 的禅宗,通过强制您显式存储值,您不会无意中存储无限量的数据:
Explicit is better than implicit.
关于python - itertools groupby 对象未正确输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48655138/