类型错误:“zip"对象在 Python 3.x 中不可调用 [英] TypeError: 'zip' object is not callable in Python 3.x

查看:53
本文介绍了类型错误:“zip"对象在 Python 3.x 中不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Python 3.5 中压缩 2 个简单列表,但不知道如何访问这些内容.我现在读到 zip() 是一个生成器,如果我想存储内容,我必须调用 list(zip()) .但是,我在执行此操作时遇到错误.

例如:

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

当我调用z"时,我看到以下内容:

<预><代码>>>>z<zip 对象在 0x0283C8F0>

但是当我调用 list(z) 时,出现以下错误:

<预><代码>>>>列表(z)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:zip"对象不可调用

我在尝试将列表分配给变量时也遇到同样的错误,例如:

l = list(zip(row, col))

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

解决方案

当你将 list 重新定义为 zip() 时会发生这种情况(这可能是你做了但没有不给我们看):

<预><代码>>>>列表 = zip()

现在 list 是一个 zip 对象(不是 zip 类)

<预><代码>>>>列表(z)

现在您正在尝试调用 zip 对象

回溯(最近一次调用最后一次):运行代码中的文件",第 301 行文件<交互式输入>",第 1 行,在 <module> 中.类型错误:zip"对象不可调用>>>

只是

del 列表

(或从一个新的解释器开始)

一切都恢复正常

I'm trying to zip 2 simple lists in Python 3.5, and can't figure out how to access the contents. I've read now that zip() is a generator, and I have to call list(zip()) if I want to store the contents. However, I'm experiencing an error when doing so.

For example:

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

When I call 'z', I see the following:

>>> z
<zip object at 0x0283C8F0>

But when I call list(z), I get the following error:

>>> list(z)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'zip' object is not callable

I also get the same error when attempting to assign the list to a variable, such as:

l = list(zip(row, col))

Apologies if this has already been answered, but I cannot for the life of me find a solution via search. Any ideas would be greatly appreciated!!

解决方案

that happens when you redefine list as zip() (which is probably what you did but didn't show us):

>>> list = zip()

now list is a zip object (not the zip class)

>>> list(z)

now you're attempting to call a zip object

Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
TypeError: 'zip' object is not callable
>>> 

just

del list

(or start from a fresh interpreter)

and everything's back to normal

这篇关于类型错误:“zip"对象在 Python 3.x 中不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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