python [iter(list)] * 2是什么意思? [英] what is meaning of [iter(list)]*2 in python?

查看:468
本文介绍了python [iter(list)] * 2是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上找到了下面的代码,结果是列表中两个元素的元组,如何理解[iter(list)]*2?

I have found below code in web, result is tuple of two elements in list, how to understand [iter(list)]*2?

lst = [1,2,3,4,5,6,7,8]
b=zip(*[iter(lst)]*2)
list(b)

[(1, 2), (3, 4), (5, 6), (7, 8)]

------------
[iter(lst)]*2
[<list_iterator at 0x1aff33917f0>, <list_iterator at 0x1aff33917f0>]

我检查[iter(lst)]*2,上面的迭代器相同,所以iter重复两次, 因此,如果我检查2到3之间的数字,结果应为[(1, 2, 3), (4, 5, 6),(7,8,NaN)] 但删除7,8

I check [iter(lst)]*2, same iterator above, so meaning iter repeat double, so, if i check num from 2 to 3, result should be [(1, 2, 3), (4, 5, 6),(7,8,NaN)] but delete 7,8

lst = [1,2,3,4,5,6,7,8]
b=zip(*[iter(lst)]*3)
list(b)
--------------
[(1, 2, 3), (4, 5, 6)]

推荐答案

解释起来很棘手.我会试一试:

Quite a tricky construct to explain. I'll give it a shot:

[iter(lst)]一起创建一个包含一个项目的列表.该项目是列表上的迭代器.

with [iter(lst)] you create a list with with one item. The item is an iterator over a list.

每当python尝试从此迭代器中获取元素时,都会返回lst的下一个元素,直到没有更多元素可用为止.

whenever python tries to get an element from this iterator, then the next element of lst is returned until no more element is available.

只需尝试以下操作:

i = iter(lst)
next(i)
next(i)

输出应如下所示:

>>> lst = [1,2,3,4,5,6,7,8]  
>>> i = iter(lst)
>>> next(i)
1
>>> next(i)
2
>>> next(i)
3
>>> next(i)
4
>>> next(i)
5
>>> next(i)
6
>>> next(i)
7
>>> next(i)
8
>>> next(i)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

现在,您创建一个包含两次完全相同迭代器的列表. 你这样做 itlst = [iter(lst)] * 2

Now you create a list that contains twice exactly the same iterator. You do this with itlst = [iter(lst)] * 2

尝试以下方法:

itlst1 = [iter(lst)] * 2
itlst2 = [iter(lst), iter(lst)]
print(itlst1)
print(itlst2)

结果将类似于:

>>> itlst1 = [iter(lst)] * 2
>>> itlst2 = [iter(lst), iter(lst)]
>>> print(itlst1)
[<list_iterator object at 0x7f9251172b00>, <list_iterator object at 0x7f9251172b00>]
>>> print(itlst2)
[<list_iterator object at 0x7f9251172b70>, <list_iterator object at 0x7f9251172ba8>]

需要注意的重要一点是,itlst1是一个包含两倍相同迭代器的列表,而itlst2包含两个不同的迭代器.

What is important to notice is, that itlst1 is a list containing twice the same iterator, whereas itlst2 contains two different iterators.

以说明尝试键入的内容:

to illustrate try to type:

next(itlst1[0])
next(itlst1[1])
next(itlst1[0])
next(itlst1[1])

并将其与以下内容进行比较:

and compare it with:

next(itlst2[0])
next(itlst2[1])
next(itlst2[0])
next(itlst2[1])

结果是:

>>> next(itlst1[0])
1
>>> next(itlst1[1])
2
>>> next(itlst1[0])
3
>>> next(itlst1[1])
4
>>> 
>>> next(itlst2[0])
1
>>> next(itlst2[1])
1
>>> next(itlst2[0])
2
>>> next(itlst2[1])
2

现在使用zip()函数( https://docs.python .org/3/library/functions.html#zip ):

尝试以下操作:

i = iter(lst)
list(zip(i, i))

zip(),带有两个参数. 每当您尝试从zip获取下一个元素时,它将执行以下操作:

zip() with two parameters. Whenver you try to get the next element from zip it will do following:

  • 从可迭代对象中获取一个值作为第一个参数
  • 从iterable中获取一个值,它是第二个参数
  • 使用这两个值返回一个元组.

list(zip(xxx))将重复执行此操作并将结果存储在列表中.

list(zip(xxx)) will do this repeatedly and store the result in a list.

结果将是:

>>> i = iter(lst)
>>> list(zip(i, i))
[(1, 2), (3, 4), (5, 6), (7, 8)]

下一个使用的技巧是*,用于将第一个元素用作函数调用的第一个参数,将第二个元素用作第二个参数,依此类推)

The next trick being used is the * that is used to use the first element as first parameter to a function call, the second element as second parameter and so forth) What does ** (double star/asterisk) and * (star/asterisk) do for parameters?

如此写作:

itlst1 = [iter(lst)] * 2
list(zip(*itlst1))

在这种情况下与

i = iter(lst)
itlst1 = [i] * 2
list(zip(itlst1[0], itlst[1]))

list(zip(i, i))

我已经解释过了.

希望这可以解释上述大多数技巧.

Hope this explains most of the above tricks.

这篇关于python [iter(list)] * 2是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆