在 Python 中将列表转换为元组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12836128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:57:35  来源:igfitidea点击:

Convert list to tuple in Python

pythonpython-2.7tuples

提问by LynnH

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

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

Most solutions on Google offer the following code:

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

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?

TypeError: 'tuple' object is not callable 我该如何解决这个问题?

采纳答案by root

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

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

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

回答by unutbu

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

扩展 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)

However, if you've redefined tupleto be a tuple rather than the typetuple:

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

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:

那么你会得到一个 TypeError 因为元组本身是不可调用的:

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

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

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

In [6]: del tuple

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

回答by Rohit Jain

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 tuplevariable to hold a tuple (45, 34)earlier... So, now tupleis an objectof type tuplenow...

问题来了……既然你已经使用了一个tuple变量来保存一个tuple (45, 34)更早的……那么,现在现在tuple是一个object类型tuple……

It is no more a typeand hence, it is no more Callable.

它不再是 a type,因此不再是Callable

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

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

回答by TheExorcist

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

在 python 中有无数种方法可以做到这一点,这里有一些实例
正常方式

>>> 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.

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

回答by Dimitris Fasarakis Hilliard

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

要添加另一种替代方法tuple(l),从 Python >= 开始,3.5您可以执行以下操作:

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

short, a bitfaster but probably suffers from readability.

总之,有点快,但可能是从可读性受到影响。

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

这本质上是将列表解包到l一个元组文字中,该元组文字是由于单个逗号的存在而创建的,



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

Ps:您收到的错误是由于名称被屏蔽,tuple即您分配给某处的名称元组,例如tuple = (1, 2, 3)

Using del tupleyou should be good to go.

使用del tuple你应该很好。