How does zip(*[iter(s)]*n) work in Python?(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 中是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:zip(*[iter(s)]*n) 在 Python 中是如何工作的?


- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- 沿轴计算直方图 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01