【问题标题】:Convert list to tuple in Python在 Python 中将列表转换为元组
【发布时间】:2012-10-01 21:57:41
【问题描述】:

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

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

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

但是,当我运行该代码时,它会导致一条错误消息:

TypeError: 'tuple' 对象不可调用

我该如何解决这个问题?

【问题讨论】:

  • 您之前是否在其他地方分配了变量名tuple
  • 不要过于挑剔,但你可能也不应该使用小写的“el”作为变量名,因为它与 1 相似。大写字母“oh”也是如此,因为它与零。相比之下,即使是“li”也更具可读性。

标签: python python-2.7 tuples


【解决方案1】:
  1. 您是否将名称“元组”指定为变量名? 它应该可以正常工作。

L 是一个列表,我们想把它转换成一个元组。

L = [1, 2, 3]

元组(L)

通过调用元组,您将列表 (L) 转换为元组。如上所述。

>> (1, 2, 3)

您可以继续使用方括号访问元组中的任何项目。 L[0]

1

【讨论】:

  • 也可以查看L的数据类型。
  • 通过使用 type(L) 然后打印出来。谢谢
【解决方案2】:

它应该可以正常工作。请勿使用tuplelist 或其他特殊名称作为变量名。这可能是导致您的问题的原因。

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

>>> tuple = 'whoops'   # Don't do this
>>> tuple(l)
TypeError: 'tuple' object is not callable

【讨论】:

  • 当列表中只有一个元素时,这是检索一个额外的值 >>> taskid = ["10030"] >>> t = tuple(l) >>> t ('10030' ,)
  • @Rafa,当只有一个元素时,逗号是元组表示法的一部分,如果这就是你的意思的话。
【解决方案3】:
l1 = []   #Empty list is given

l1 = tuple(l1)   #Through the type casting method we can convert list into tuple

print(type(l1))   #Now this show class of tuple

【讨论】:

    【解决方案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')
    

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

    【讨论】:

    • 第二种方式有什么好处?
    • @GrannyAching 在这个现代世界中对智能的定义不断变化,因为它是在一行中完成的懒惰方法,我想这是聪明的方法。
    • tuple(l)(第一种方式)比tuple(item for item in l)(第二种方式)短。
    【解决方案5】:

    要为tuple(l) 添加另一个替代方案,从 Python >= 3.5 开始,您可以这样做:

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

    short,bit 更快,但可能会受到可读性的影响。

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


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

    使用 del tuple 你应该很高兴。

    【讨论】:

      【解决方案6】:

      你可能做过这样的事情:

      >>> 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 使用任何内置类型作为变量名...您确实可以使用任何其他名称。改为为您的变量使用任意名称...

      【讨论】:

        【解决方案7】:

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

        【讨论】:

        • 复杂的琐事:即使tuple 被遮蔽,实际类型仍可作为().__class__ 使用,如:().__class__(range(10)) :)
        猜你喜欢
        • 2010-12-25
        • 1970-01-01
        • 2011-07-27
        • 2021-09-15
        • 1970-01-01
        • 2017-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多