无法多次解压缩zip对象

Gab*_*iel -2 python python-3.x

为什么这不起作用?:

t = zip([3],[4],[3])
print("1:",*t)
print("2:",*t)
Run Code Online (Sandbox Code Playgroud)

我们不能在Python中第二次解压缩,为什么会这样?

iCo*_*dez 7

zip返回Python 3.x中的迭代器,而不是像Python 2.x中那样返回列表.这意味着,在您解压缩一次后,它将耗尽并且不再可用:

>>> t = zip([3],[4],[3])
>>> print("1:",*t)
1: (3, 4, 3)
>>> list(t) # t is now empty
[]
>>>
Run Code Online (Sandbox Code Playgroud)

您需要将迭代器显式转换为序列(列表,元组等).如果你想多次打开包装:

>>> t = tuple(zip([3],[4],[3]))
>>> print("1:",*t)
1: (3, 4, 3)
>>> print("2:",*t)
2: (3, 4, 3)
>>>
Run Code Online (Sandbox Code Playgroud)