如何在Python中将元组更改为数组?

pro*_*eek 18 python arrays tuples

假设我有一个元组t = (1,2,3,4).将其更改为Array的简单方法是什么?

我可以做这样的事情,

array = []
for i in t:
    array.append(i)
Run Code Online (Sandbox Code Playgroud)

但我更喜欢像x.toArray()之类的东西.

Mar*_*ers 58

如果要将元组转换为列表(如您所愿),请使用以下命令:

>>> t = (1, 2, 3, 4)   # t is the tuple (1, 2, 3, 4)
>>> l = list(t)        # l is the list [1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)

另外,我建议不要使用tuple变量的名称.

  • +1就像这个答案一样简单,对于Python世界中的n00bs来说并不总是显而易见的. (3认同)
  • +1.正确答案和建议不要使用内置函数的名称作为变量名. (2认同)