python - TypeError: 'zip' 对象在 Python 3.x 中不可调用

标签 python python-3.x typeerror

我正在尝试在 Python 3.5 中压缩 2 个简单列表,但不知道如何访问内容。我现在读到 zip()是发电机,我必须调用list(zip())如果我想存储内容。但是,这样做时我遇到了错误。

例如:

row = [1, 2, 3]
col = ['A', 'B', 'C']
z = zip(row, col)

当我调用“z”时,我看到以下内容:
>>> z
<zip object at 0x0283C8F0>

但是当我调用 list(z) ,我收到以下错误:
>>> list(z)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'zip' object is not callable

尝试将列表分配给变量时,我也会遇到相同的错误,例如:
l = list(zip(row, col))

抱歉,如果这已经得到解答,但我终生无法通过搜索找到解决方案。任何想法将不胜感激!

最佳答案

当您重新定义 list 时会发生这种情况如zip() (这可能是您所做但没有向我们展示的):

>>> list = zip()

现在listzip对象(不是 zip 类)
>>> list(z)

现在您正尝试调用 zip目的
Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
TypeError: 'zip' object is not callable
>>> 

只是
del list

(或从新的口译员开始)

一切恢复正常

关于python - TypeError: 'zip' 对象在 Python 3.x 中不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47583259/

相关文章:

python - PinoroEnviro+ 类型错误 : argument should be integer or bytes-like object, 而不是 'str'

python - 使用 Python API 创建 GCE 实例

python - Shutil.copyfile 不应该用 follow_symlinks 设置替换 dst 中的链接吗?

python - 如何创建 5 个新列来反射(reflect)每个客户过去的 5 笔交易?

Python 数学运算顺序

javascript - 无法导入外部 NPM 包来制作 Angular 库

python - `namedtuple` 在 3.5.1 中有什么变化吗?

python - 通过字典进行复杂的迭代

python - Python return 总是必要的吗

python多变量if条件