gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:57:01 27 4
gpt4 key购买 nike

假设我有一个元组 t = (1,2,3,4)。把它改成Array的简单方法是什么?

我可以做这样的事情,

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

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

最佳答案

如果您想将元组转换为 list(如您所愿),请使用:

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

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

关于python - 如何在 Python 中将元组更改为数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3687702/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com