简体   繁体   English

使用itertools groupby查找列表中所有偶数的组

[英]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.

相关问题 Itertools groupby使用另一个列表对列表进行分组 - Itertools groupby to group list using another list 使用 itertools.groupby 返回最长的连续负数列表 - Return the longest list of consecutive negative numbers using itertools.groupby 使用基本 python 将数字列表中的连续数字分组到元组列表中(不允许使用迭代工具或更多迭代工具) - Group consecutive numbers from a list of numbers into a list of tuples using base python (NO itertools or more itertools allowed) 如何使用 itertools.groupby 在两次出现的数字之间对数字进行分组 - How to group numbers between two occurences of a number using itertools.groupby 使用列表理解的数字 1...N 的所有排列(没有 itertools) - All permutations of numbers 1...N using list comprehension (without itertools) 使用itertools groupby创建列表列表 - Using itertools groupby to create a list of lists 使用列表理解,元组和itertools.groupby - Using list comprehension, tuples and itertools.groupby 如何使用 itertools.groupby() 按月和年对项目进行分组 - How to group items by month and year using itertools.groupby() 使用itertools.groupby - Using itertools.groupby 从在 Python 中使用 groupby itertools 创建的字典列表中删除重复项 - Remove duplicates from list of dictionaries created using groupby itertools in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM