简体   繁体   English

Python中迭代器上的列表理解元组

[英]List Comprehension tuple over iterator in Python

I'm learning about iterators and the groupby function in Python's itertools module. 我正在学习Python的itertools模块中的迭代器和groupby函数。 I understand that the following code takes strings of integers and prints tuples (x,y) where x is the number of times y repeats. 我了解以下代码采用整数字符串并输出元组(x,y),其中x是y重复的次数。

from itertools import groupby
s = input()
print(*[(len(list(c)), int(k)) for k, c in groupby(s)])

What I don't understand is "k, c in groupby(s)" as a part of a list comprehension. 我不明白的是“ groupby(s)中的k,c”是列表理解的一部分。 It would make sense to me if "groupby(s)" was a list of tuples but it's an iterator. 如果“ groupby(s)”是一个元组列表,但它是一个迭代器,这对我来说很有意义。

My question is how does a list comprehension produce a list from two variables over an iterator? 我的问题是列表推导如何通过迭代器的两个变量生成一个列表?

My testing has shown that I can use one variable over an iterator to produce a list. 我的测试表明,我可以在迭代器上使用一个变量来生成列表。 How is that list different? 该列表有何不同?

The result of groupby(s) is not an iterator but tuples of keys and iterators. groupby(s)的结果不是迭代器,而是键和迭代器的元组。 Passing two variables, "k,c" assigns k's to keys in groupby(s) and c to iterators in groupby(s). 传递两个变量,“ k,c”将k分配给groupby中的键,将c分配给groupby中的迭代器。

There is ambiguity because 有歧义,因为

for k,c in groupby(s)

is the same as 是相同的

for (k,c) in groupby(s)

where k and c are assigned to values within the tuples, and 其中k和c分配给元组中的值,并且

for k in groupby(s)

assigns k to the tuples themselves. 将k分配给元组本身。

Also, in the example code list(c) creates list with the iterator c. 同样,在示例代码中,list(c)使用迭代器c创建列表。

The difference is that groupby(s) will return a series of pairs (tuples of length 2). 区别在于groupby将返回一系列对(长度为2的元组)。 You can bind each of these to a pair of variables. 您可以将每个绑定到一对变量。

As a general concept, this works just as if groupby(s) resolved to a list of the same pairs. 作为一般概念,这就像将groupby解析为相同对的列表一样。 The actual list comprehension can use any iterable at the right end; 实际的列表理解可以在右边使用任何可迭代的; it's not restricted to a static sequence (list, tuple, string). 它不限于静态序列(列表,元组,字符串)。

EXAMPLE

Let's look at a better example. 让我们看一个更好的例子。 groupby gathers adjacent elements with the same value. groupby收集具有相同值的相邻元素。 It does this with an iterator for each contiguous group of values. 它使用迭代器为每个连续的值组执行此操作。 The sample string below has contiguous strings of B, C, D, and Z, and two separate strings of a single A. groupby will instantiate an iterator for each starting location; 下面的示例字符串具有B,C,D和Z的连续字符串,以及单个A的两个单独的字符串。groupby将为每个起始位置实例化一个迭代器。 when invoked, the iterator will return a sequence of the consecutive letters, shown in the output. 调用时,迭代器将返回一系列连续的字母,如输出所示。

from itertools import groupby
s = "BBCCCDDDDAZZA"
for k, c in groupby(s):
    print k, list(c)

Output: 输出:

B ['B', 'B']
C ['C', 'C', 'C']
D ['D', 'D', 'D', 'D']
A ['A']
Z ['Z', 'Z']
A ['A']

As you've noted, if we print merely the iterator c , all we get is the object handle. 正如您已经指出的,如果只打印迭代器c ,那么我们得到的只是对象句柄。 To obtain the letters, we need to invoke the iterator. 要获取字母,我们需要调用迭代器。

In the code you posted, 在您发布的代码中,

for k, c in groupby(s)

receives the series of (letter, iterator) pairs from groupby . groupby接收一系列(字母,迭代器)对。 The list function is what invokes the iterator, fetching the list of letters. list函数是调用迭代器的功能,它获取字母列表。

Does that clarify things at all? 这是否可以澄清一切?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM