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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3687702/
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 12:15:47  来源:igfitidea点击:

How to change a tuple into array in Python?

pythonarraystuples

提问by prosseek

Let's say I have a tuple t = (1,2,3,4). What's the simple way to change it into Array?

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

I can do something like this,

我可以做这样的事情

array = []
for i in t:
    array.append(i)

But I prefer something like x.toArray() or something.

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

采纳答案by Mark Byers

If you want to convert a tuple to a list(as you seem to want) use this:

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

>>> t = (1, 2, 3, 4)   # t is the tuple (1, 2, 3, 4)
>>> l = list(t)        # l is the list [1, 2, 3, 4]

In addition I would advise against using tupleas the name of a variable.

此外,我建议不要使用tuple作为变量的名称。