Python将元组转换为数组 [英] Python convert tuple to array
本文介绍了Python将元组转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将3维元组转换为数组
How can I convert at 3-Dimensinal tuple into an array
a = []
a.append((1,2,4))
a.append((2,3,4))
以类似数组的形式出现:
in a array like:
b = [1,2,4,2,3,4]
推荐答案
使用列表理解:
>>> 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]
使用 itertools.chain.from_iterable
:
>>> import itertools
>>> list(itertools.chain.from_iterable(a))
[1, 2, 4, 2, 3, 4]
这篇关于Python将元组转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文