简体   繁体   English

Python:如何将“扁平”序列转换为元组序列?

[英]Python: How to transform a 'flat' sequence into a sequence of tuples?

I needed to make a list of 2-tuples out of a one-dimensional list of coordinates, for example [1, 2, 1, 5] needed to become [(1, 2), (1, 5)] . 我需要从一维坐标列表中列出2元组的列表,例如[1, 2, 1, 5]变为[(1, 2), (1, 5)] As it happens I've already found the fastest, most general way to do this, at Activestate , but the code listed there is opaque to a relative Python newbie: 碰巧的是,我已经在Activestate处找到了最快,最通用的方法,但是相对于Python新手来说,这里列出的代码是不透明的:

def group3(lst, n):
    return itertools.izip(*[itertools.islice(lst, i, None, n) for i in range(n)])

Can anyone explain what this code is doing in English? 谁能用英语解释这段代码在做什么? What is the asterisk doing in this context?? 在这种情况下,星号在做什么? Thank you. 谢谢。

The itertools.islice function can produce a slice from any iterable. itertools.islice函数可以从任何可迭代对象中生成一个切片。 The statement: 该声明:

itertools.islice(lst, 0, None, 2)

In simplified terms means "return an iterator which, when evaluated, will return every other element in lst starting with the element at 0". 用简化的术语表示“返回一个迭代器,该迭代器在被求值时将首先从元素0开始返回所有其他元素”。 The 0 arg is the starting point and 2 is the step value (how many to skip to get the next value) 0 arg是起始点, 2是步长值(跳过多少以获得下一个值)

It would be better to use a 6-element list to illustrate, since looking at [(1, 2), (1, 5)] it may be unclear which function produces the inner tuples (they are not produced until the final izip). 最好使用6元素列表来说明,因为看[(1, 2), (1, 5)] ,可能不清楚哪个函数产生内部元组(直到最终izip才产生它们) 。

>>> lst = [1, 2, 3, 4, 5, 6]
>>> list(itertools.islice(lst, 0, None, 2))
[1, 3, 5]
>>> list(itertools.islice(lst, 0, None, 2))
[2, 4, 6]

Be careful using this with a generator; 在发电机上使用时要小心; the first call will traverse the whole sequence, draining the generator: 第一次调用将遍历整个序列,从而消耗生成器的电量:

>>> def foo():
...     for i in range(6):
...         yield i + 1
>>>
>>> gen = foo()
>>> list(itertools.islice(gen, 0, None, 2)
[1, 3, 5]
>>> list(itertools.islice(gen, 1, None, 2)
[]

Your function needs to produce 2 sequences, odds and evens. 您的函数需要产生2个序列,即奇数和偶数。 This is where the list comprehension comes in: 这是列表理解的地方:

>>> [itertools.islice(lst, i, None, 2) for i in range(2)]
[<itertools.islice object at 0x7f958a79eaf8>, <itertools.islice object at 0x7f958a79eaa0>]

Now you have 2 islice objects ready to be interleaved. 现在,您有2个islice对象准备好进行交织了。 To do this we could use zip , but itertools.izip is more efficient because it returns an iterator: 为此,我们可以使用zip ,但是itertools.izip效率更高,因为它返回一个迭代器:

>>> list(zip([1, 3, 5], [2, 4, 6]))
[(1, 2), (3, 4), (5, 6)]
>>> tmp = itertools.izip(*[itertools.islice(lst, i, None, 2) for i in range(2)])
>>> tmp
<itertools.izip object at 0x7f958a7b6cf8>
>>> list(tmp)
[(1, 2), (3, 4), (5, 6)]

Hope that helps clarify the steps. 希望这有助于阐明步骤。

Here's an alternate version for 2-tuples (not as general as the active state recipe): 这是2元组的替代版本(不如活动状态配方一般):

>>> x = [1,2,3,4,5,6,7,8]
>>> [ (x.pop(0), x.pop(0)) for item in range(len(x)/2) ]
[(1, 2), (3, 4), (5, 6), (7, 8)]

This forms a list of two-tuples by popping the first two numbers off the list until there are no more pairs left. 通过从列表中弹出前两个数字,直到没有更多的对,形成一个二元组列表。

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

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