本文介紹了zip(*[iter(s)]*n) 在 Python 中是如何工作的?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
s = [1,2,3,4,5,6,7,8,9]
n = 3
zip(*[iter(s)]*n) # returns [(1,2,3),(4,5,6),(7,8,9)]
zip(*[iter(s)]*n)
是如何工作的?如果用更冗長的代碼編寫會是什么樣子?
How does zip(*[iter(s)]*n)
work? What would it look like if it was written with more verbose code?
推薦答案
iter()
是一個序列的迭代器.[x] * n
生成一個包含 n
個 x
數量的列表,即長度為 n
的列表,其中每個元素都是x
.*arg
將序列解壓縮為函數調用的參數.因此,您將相同的迭代器 3 次傳遞給 zip()
,每次都會從迭代器中拉取一個item.
iter()
is an iterator over a sequence. [x] * n
produces a list containing n
quantity of x
, i.e. a list of length n
, where each element is x
. *arg
unpacks a sequence into arguments for a function call. Therefore you're passing the same iterator 3 times to zip()
, and it pulls an item from the iterator each time.
x = iter([1,2,3,4,5,6,7,8,9])
print zip(x, x, x)
這篇關于zip(*[iter(s)]*n) 在 Python 中是如何工作的?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!