在Python中将列表转换为元组

本文翻译自:Convert list to tuple in Python

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:“元组”对象不可调用如何解决此问题?


#1楼

参考:https://stackoom.com/question/rrGK/在Python中将列表转换为元组


#2楼

It should work fine. 它应该工作正常。 Don't use tuple , list or other special names as a variable name. 不要将tuplelist或其他特殊名称用作变量名。 It's probably what's causing your problem. 这可能是导致您出现问题的原因。

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

#3楼

Expanding on eumiro's comment, normally tuple(l) will convert a list l into 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 tuple to be a tuple rather than the type tuple : 但是,如果将tuple重新定义为tuple组而不是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: 然后您会收到TypeError,因为元组本身不可调用:

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): 您可以通过退出并重新启动解释器来恢复tuple的原始定义,或者(由于@glglgl):

In [6]: del tuple

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

#4楼

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... 这就是问题所在...由于您之前使用过tuple变量保存了tuple (45, 34) ,所以,现在tuple现在是tuple类型的object ...

It is no more a type and hence, it is no more Callable . 它不再是type ,因此不再是Callable

Never use any built-in types as your variable name... You do have any other name to use. Never使用任何内置类型作为变量名...您确实有其他名称要使用。 Use any arbitrary name for your variable instead... 请为变量使用任意名称...


#5楼

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 在python中有无限的方法可以做到这一点,这里有一些实例
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. 明智地使用它,还可以使您的程序高效。


#6楼

To add another alternative to tuple(l) , as of Python >= 3.5 you can do: 要为tuple(l)添加另一种选择,从Python> = 3.5您可以执行以下操作:

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 , . 这实际上是将列表l拆包到元组文字中,该元组文字是由于存在单个逗号,


Ps: The error you are receiving is due to masking of the name tuple ie you assigned to the name tuple somewhere eg tuple = (1, 2, 3) . Ps:您收到的错误是由于名称tuple屏蔽所致,即您在某处为名称元组分配了名称,例如tuple = (1, 2, 3)

Using del tuple you should be good to go. 使用del tuple应该很好。

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值