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中第二次解压缩,为什么会这样?
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)
| 归档时间: |
|
| 查看次数: |
1941 次 |
| 最近记录: |