久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

python中[iter(list)] * 2的含義是什么?

what is meaning of [iter(list)]*2 in python?(python中[iter(list)] * 2的含義是什么?)
本文介紹了python中[iter(list)] * 2的含義是什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我在網(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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現(xiàn)問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點)
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應(yīng)并將元素值存儲在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標(biāo)記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
主站蜘蛛池模板: 国产精品久久久久久中文字 | h小视频 | 日韩视频在线免费观看 | 成人免费视频观看视频 | 国产精品欧美一区二区三区 | 狠狠干av| 亚洲视频欧美视频 | 国产精品视频一区二区三区, | 日韩午夜电影 | 日一区二区 | 国产一区二区三区久久久久久久久 | 国产污视频在线 | 一级黄在线观看 | 99热视| 国产精品日韩欧美一区二区 | 日韩精品在线网站 | 狠狠色狠狠色综合日日92 | www.蜜桃av | 亚洲精品电影 | 精品久久久久久久久久久下田 | 欧美日韩精选 | a级片网站| 午夜影院在线观看视频 | 久久综合一区二区三区 | 国产1区 | 欧美一区二区三区四区五区无卡码 | 欧美片网站免费 | 天天干,夜夜操 | 久久99久久99精品免视看婷婷 | 欧美视频中文字幕 | 亚洲色图插插插 | 一区二区中文字幕 | 91一区二区在线观看 | 久久精品国产清自在天天线 | 国产一区二区三区四区 | 欧美久久久久久 | 中文字幕二区 | 精品欧美一区二区三区久久久小说 | 国产精品视频一区二区三区不卡 | 国产精品高清在线 | 久久国产综合 |