Python convert tuple to array
本问题已经有最佳答案,请猛点这里访问。
如何将三维元组转换为数组
1 2 3  | a = [] a.append((1,2,4)) a.append((2,3,4))  | 
在数组中,如:
1  | b = [1,2,4,2,3,4]  | 
使用列表的理解:
1 2 3 4 5  | >>> 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]  | 
使用
1 2 3  | >>> import itertools >>> list(itertools.chain.from_iterable(a)) [1, 2, 4, 2, 3, 4]  | 
简单的方式,使用扩展方法。
1 2 3  | x = [] for item in a: x.extend(item)  | 
如果你的意思是在NumPy数组数组,你可以这样做:
1 2 3 4 5  | a = [] a.append((1,2,4)) a.append((2,3,4)) a = np.array(a) a.flatten()  |