zip(*[iter(s)]*n) 在 Python 中是如何工作的?
- 作者: 三爪猫60212662
- 来源: 51数据库
- 2022-10-27
问题描述
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)
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
