[英]Using itertools groupby to find the group of all even numbers in a list
I am trying to understand the usefulness of the itertools.groupby()
function and have created a rather naive use case. 我试图了解itertools.groupby()
函数的有用性,并创建了一个相当幼稚的用例。 I have a list of numbers, and want to group them by oddness or evenness. 我有一个数字列表,想按奇数或偶数对它们进行分组。 Below is my code using itertools.groupby()
: 下面是我使用itertools.groupby()
代码:
for decision, group in groupby(range(2, 11), key=lambda x: x % 2 == 0):
...: print(decision, list(group))
And below is the output I get: 下面是我得到的输出:
True [2]
False [3]
True [4]
False [5]
True [6]
False [7]
True [8]
False [9]
True [10]
Basically what I was expecting is something like where all the "True's" are grouped together and all the "False's" are grouped together. 基本上,我期望的是将所有“ True”组合在一起而将所有“ False”组合在一起的情况。
Is this even possible with groupby()
? 使用groupby()
甚至可能吗?
groupby()
combines consecutive values where their key output is equal, giving you a shared iterator for each such group. groupby()
组合键输出相等的连续值 ,从而为每个此类组提供一个共享的迭代器。 It will not process the whole input in one go, it gives you the groups as you iterate . 它不会一口气处理整个输入,而是在迭代时为您提供组。
For your example, the key changes for each value, and so the groups consist of a single value each; 在您的示例中,键针对每个值而变化,因此,每个组均由一个值组成; it starts with 2
and the key output is True
, then next comes 3
and the key produces False
. 它以2
开头,并且键输出为True
,然后是3
,并且键产生False
。 Because True != False
, that's a new group. 因为True != False
,所以这是一个新组。 The next value 4
changes the key again, to True
, so it's another group, etc. 下一个值4
再次将键更改为True
,因此是另一个组,依此True
。
What you want to do can't be done with groupby()
; 您想要做的事情不能通过groupby()
; to sort values into buckets across the whole iterable, just use a dictionary: 到排序值到在整个迭代桶,只使用字典:
grouped = {}
for value in range(2, 11):
key = value % 2 == 0
grouped.setdefault(key, []).append(value)
You could also sort your input first, with sorted(range(2, 11), key=lambda x: x % 2 == 0)
, but that's a waste of time . 您还可以使用sorted(range(2, 11), key=lambda x: x % 2 == 0)
首先对输入进行sorted(range(2, 11), key=lambda x: x % 2 == 0)
,但这很浪费时间 。 Sorting is a more complex algorithm, taking O(n log n) ( quasilinear ) time, whereas using a dictionary to group the values takes O(n) ( linear ) time. 排序是一种更复杂的算法,需要O(n log n)( 准线性 )时间,而使用字典对值进行分组则需要O(n)( 线性 )时间。 That might not matter when you have just 9 elements, but when you have to process 1000 elements, sorting would increase the time taken by a factor of nearly 10, for 1 million elements, a factor of nearly 20, etc. 当您只有9个元素时,这可能并不重要,但是当您必须处理1000个元素时,排序将花费大约10倍的时间,对于100万个元素而言,将近20倍,等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.