本文介绍了groupby周围的列表导致空组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩这个以获得对itertools groupby 更好的感觉,所以我按照数字对元组列表进行了分组,并尝试获取结果组的列表。当我将 groupby 的结果转换为列表时,我得到一个奇怪的结果:除最后一组之外的所有组都是空的。这是为什么?我假设将迭代器转换为列表效率较低但从不改变行为。我猜这些列表是空的,因为遍历了内部迭代器,但是何时/何地发生?

I was playing around to get a better feeling for itertools groupby, so I grouped a list of tuples by the number and tried to get a list of the resulting groups. When I convert the result of groupby to a list however, I get a strange result: all but the last group are empty. Why is that? I assumed turning an iterator into a list would be less efficient but never change behavior. I guess the lists are empty because the inner iterators are traversed but when/where does that happen?

import itertools

l=list(zip([1,2,2,3,3,3],['a','b','c','d','e','f']))
#[(1, 'a'), (2, 'b'), (2, 'c'), (3, 'd'), (3, 'e'), (3, 'f')]

grouped_l = list(itertools.groupby(l, key=lambda x:x[0]))
#[(1, <itertools._grouper at ...>), (2, <itertools._grouper at ...>), (3, <itertools._grouper at ...>)]

[list(x[1]) for x in grouped_l]
[[], [], [(3, 'f')]]


grouped_i = itertools.groupby(l, key=lambda x:x[0])
#<itertools.groupby at ...>
[list(x[1]) for x in grouped_i]
[[(1, 'a')], [(2, 'b'), (2, 'c')], [(3, 'd'), (3, 'e'), (3, 'f')]]


推荐答案

来自:

groupby()的输出转换为列表前进 groupby()对象。

Turning the output from groupby() into a list advances the groupby() object.

因此,你不应该打字输入 itertools.groupby 要列出的对象。如果您想将值存储为 list ,那么您应该执行类似 list comprehension 的操作,以便创建<$ c $的副本c> groupby object:

Hence, you shouldn't be type-casting itertools.groupby object to list. If you want to store the values as list, then you should be doing something like this list comprehension in order to create copy of groupby object:

grouped_l = [(a, list(b)) for a, b in itertools.groupby(l, key=lambda x:x[0])]

这将允许您多次迭代列表(从 groupby 对象转换)。但是,如果您只对迭代结果一次感兴趣,那么您在问题中提到的第二个解决方案就足以满足您的要求。

This will allow you to iterate your list (transformed from groupby object) multiple times. However, if you are interested in only iterating the result once, then the second solution you mentioned in the question will suffice your requirement.

这篇关于groupby周围的列表导致空组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:34