Python将元组转换为数组

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

Python convert tuple to array

pythonlist

提问by Samy

How can I convert at 3-Dimensinal tuple into an array

如何将 3-Dimensinal 元组转换为数组

a = []
a.append((1,2,4))
a.append((2,3,4))

in a array like:

在一个数组中,如:

b = [1,2,4,2,3,4]

采纳答案by falsetru

Using list comprehension:

使用列表理解

>>> a = []
>>> a.append((1,2,4))
>>> a.append((2,3,4))
>>> [x for xs in a for x in xs]
[1, 2, 4, 2, 3, 4]

Using itertools.chain.from_iterable:

使用itertools.chain.from_iterable

>>> import itertools
>>> list(itertools.chain.from_iterable(a))
[1, 2, 4, 2, 3, 4]

回答by user1654183

If you mean array as in numpy array, you can also do:

如果您的意思是 numpy 数组中的数组,您还可以执行以下操作:

a = []
a.append((1,2,4))
a.append((2,3,4))
a = np.array(a)
a.flatten()

回答by MGP

The simple way, use extendmethod.

最简单的方法,使用扩展方法。

x = []
for item in a:
    x.extend(item)