zip(* [iter(s)] * n)如何在Python中运行? [英] How does zip(*[iter(s)]*n) work in Python?

查看:170
本文介绍了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( ) ,每次都从迭代器中提取一个项目。

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中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆