問題描述
我在網(wǎng)上找到了下面的代碼,結(jié)果是列表中兩個元素的元組,如何理解[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
重復(fù)雙倍,所以,如果我檢查 num 從 2 到 3,結(jié)果應(yīng)該是 [(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)]
推薦答案
解釋起來相當(dāng)棘手.我試一試:
Quite a tricky construct to explain. I'll give it a shot:
使用 [iter(lst)]
您可以創(chuàng)建一個包含一個項目的列表.該項目是列表的迭代器.
with [iter(lst)]
you create a list with with one item. The item is an iterator over a list.
每當(dāng) python 試圖從這個迭代器中獲取一個元素時,就會返回 lst
的下一個元素,直到?jīng)]有更多元素可用為止.
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)
輸出應(yīng)如下所示:
>>> 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
現(xiàn)在您創(chuàng)建一個包含兩次完全相同迭代器的列表.你這樣做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)
結(jié)果將類似于:
>>> 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.
為了說明嘗試輸入:
next(itlst1[0])
next(itlst1[1])
next(itlst1[0])
next(itlst1[1])
并將其與:
next(itlst2[0])
next(itlst2[1])
next(itlst2[0])
next(itlst2[1])
結(jié)果是:
>>> 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
現(xiàn)在到 zip()
函數(shù)( https://docs.python.org/3/library/functions.html#zip):
Now to the zip()
function ( https://docs.python.org/3/library/functions.html#zip ):
嘗試以下操作:
i = iter(lst)
list(zip(i, i))
zip()
有兩個參數(shù).當(dāng)您嘗試從 zip
獲取下一個元素時,它將執(zhí)行以下操作:
zip()
with two parameters.
Whenver you try to get the next element from zip
it will do following:
- 從作為第一個參數(shù)的可迭代對象中獲取一個值
- 從可迭代的第二個參數(shù)中獲取一個值
- 返回一個包含這兩個值的元組.
list(zip(xxx))
將重復(fù)執(zhí)行此操作并將結(jié)果存儲在列表中.
list(zip(xxx))
will do this repeatedly and store the result in a list.
結(jié)果將是:
>>> i = iter(lst)
>>> list(zip(i, i))
[(1, 2), (3, 4), (5, 6), (7, 8)]
使用的下一個技巧是 *
,用于將第一個元素用作函數(shù)調(diào)用的第一個參數(shù),將第二個元素用作第二個參數(shù),依此類推)什么是**(雙星/星號)和*(星號/星號)) 為參數(shù)做些什么?
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], itlst1[1]))
與
list(zip(i, i))
我已經(jīng)解釋過了.
希望這可以解釋上述大部分技巧.
Hope this explains most of the above tricks.
這篇關(guān)于python中[iter(list)] * 2的含義是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!