如何在Python中将元组转换为数组?

发布于 2024-09-18 19:51:49 字数 180 浏览 2 评论 0原文

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

我可以做这样的事情,

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

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

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

I can do something like this,

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎的声音 2024-09-25 19:51:49

如果您想将元组转换为列表(正如您所希望的那样),请使用以下命令:

>>> 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作为变量的名称。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文