问题:在Python中将列表转换为元组

我正在尝试将列表转换为元组。

Google上的大多数解决方案都提供以下代码:

l = [4,5,6]
tuple(l)

但是,运行该代码会导致错误消息:

TypeError:“元组”对象不可调用如何解决此问题?

I’m trying to convert a list to a tuple.

Most solutions on Google offer the following code:

l = [4,5,6]
tuple(l)

However, the code results in an error message when I run it:

TypeError: ‘tuple’ object is not callable How can I fix this problem?


回答 0

它应该工作正常。不要使用tuplelist或其他特殊的名字作为变量名。这可能是导致您出现问题的原因。

>>> l = [4,5,6]
>>> tuple(l)
(4, 5, 6)

It should work fine. Don’t use tuple, list or other special names as a variable name. It’s probably what’s causing your problem.

>>> l = [4,5,6]
>>> tuple(l)
(4, 5, 6)

回答 1

扩展eumiro的注释,通常tuple(l)会将列表l转换为元组:

In [1]: l = [4,5,6]

In [2]: tuple
Out[2]: <type 'tuple'>

In [3]: tuple(l)
Out[3]: (4, 5, 6)

但是,如果您已重新定义tuple为元组而不是type tuple

In [4]: tuple = tuple(l)

In [5]: tuple
Out[5]: (4, 5, 6)

那么您会收到TypeError,因为元组本身不可调用:

In [6]: tuple(l)
TypeError: 'tuple' object is not callable

您可以tuple通过退出并重新启动解释器来恢复原始定义,或者(感谢@glglgl):

In [6]: del tuple

In [7]: tuple
Out[7]: <type 'tuple'>

Expanding on eumiro’s comment, normally tuple(l) will convert a list l into a tuple:

In [1]: l = [4,5,6]

In [2]: tuple
Out[2]: <type 'tuple'>

In [3]: tuple(l)
Out[3]: (4, 5, 6)

However, if you’ve redefined tuple to be a tuple rather than the type tuple:

In [4]: tuple = tuple(l)

In [5]: tuple
Out[5]: (4, 5, 6)

then you get a TypeError since the tuple itself is not callable:

In [6]: tuple(l)
TypeError: 'tuple' object is not callable

You can recover the original definition for tuple by quitting and restarting your interpreter, or (thanks to @glglgl):

In [6]: del tuple

In [7]: tuple
Out[7]: <type 'tuple'>

回答 2

您可能已经做过这样的事情:

>>> tuple = 45, 34  # You used `tuple` as a variable here
>>> tuple
(45, 34)
>>> l = [4, 5, 6]
>>> tuple(l)   # Will try to invoke the variable `tuple` rather than tuple type.

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    tuple(l)
TypeError: 'tuple' object is not callable
>>>
>>> del tuple  # You can delete the object tuple created earlier to make it work
>>> tuple(l)
(4, 5, 6)

这是问题所在…由于您使用了tuple变量来保存tuple (45, 34)较早的内容…所以,现在tupleobject类型tuple

它不再是一个type,因此不再Callable

Never使用任何内置类型作为变量名…您确实可以使用任何其他名称。请为变量使用任意名称…

You might have done something like this:

>>> tuple = 45, 34  # You used `tuple` as a variable here
>>> tuple
(45, 34)
>>> l = [4, 5, 6]
>>> tuple(l)   # Will try to invoke the variable `tuple` rather than tuple type.

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    tuple(l)
TypeError: 'tuple' object is not callable
>>>
>>> del tuple  # You can delete the object tuple created earlier to make it work
>>> tuple(l)
(4, 5, 6)

Here’s the problem… Since you have used a tuple variable to hold a tuple (45, 34) earlier… So, now tuple is an object of type tuple now…

It is no more a type and hence, it is no more Callable.

Never use any built-in types as your variable name… You do have any other name to use. Use any arbitrary name for your variable instead…


回答 3

tuple(l)自Python> =起,要向中添加其他替代方法,3.5您可以执行以下操作:

t = *l,  # or t = (*l,) 

简短一点快,但可能是从可读性受到影响。

这实际上将列表解压缩到l元组文字中,该元组文字是由于单个逗号的存在而创建的,


Ps:您收到的错误是由于名称的掩盖所致,tuple即您在某处(例如)分配了名称元组tuple = (1, 2, 3)

使用del tuple您应该很好。

To add another alternative to tuple(l), as of Python >= 3.5 you can do:

t = *l,  # or t = (*l,) 

short, a bit faster but probably suffers from readability.

This essentially unpacks the list l inside a tuple literal which is created due to the presence of the single comma ,.


P.s: The error you are receiving is due to masking of the name tuple i.e you assigned to the name tuple somewhere e.g tuple = (1, 2, 3).

Using del tuple you should be good to go.


回答 4

我找到了许多最新的答案,并且得到了正确答案,但会为答案添加一些新内容。

在Python有无限的方法可以做到这一点,这里有一些情况下,
正常的方式

>>> l= [1,2,"stackoverflow","python"]
>>> l
[1, 2, 'stackoverflow', 'python']
>>> tup = tuple(l)
>>> type(tup)
<type 'tuple'>
>>> tup
(1, 2, 'stackoverflow', 'python')

聪明的方法

>>>tuple(item for item in l)
(1, 2, 'stackoverflow', 'python')

请记住,元组是不变的,用于存储有价值的东西。例如,密码,密钥或哈希存储在元组或字典中。如果需要刀,为什么要用剑切苹果。明智地使用它,还可以使您的程序高效。

I find many answers up to date and properly answered but will add something new to stack of answers.

In python there are infinite ways to do this, here are some instances
Normal way

>>> l= [1,2,"stackoverflow","python"]
>>> l
[1, 2, 'stackoverflow', 'python']
>>> tup = tuple(l)
>>> type(tup)
<type 'tuple'>
>>> tup
(1, 2, 'stackoverflow', 'python')

smart way

>>>tuple(item for item in l)
(1, 2, 'stackoverflow', 'python')

Remember tuple is immutable ,used for storing something valuable. For example password,key or hashes are stored in tuples or dictionaries. If knife is needed why to use sword to cut apples. Use it wisely, it will also make your program efficient.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。