简体   繁体   English

如何从一组现有数组构造一个新的numpy数组?

[英]How to construct a new numpy array from a set of existing arrays?

I have 12 numpy arrays, 5 of size (3, 121) and 7 of size (3, 120), ordered 0-11; 我有12个numpy数组,大小为5(3,121),大小为7(3,120),顺序为0-11; call them a0, a1, ..., a11. 称它们为a0,a1,...,a11。 I would like to construct a single new array built specifically in the following way: 我想通过以下方式构造一个新的数组:

newArray = [a0_00, a1_00, a2_00, ..., a11_00, a0_01, a1_01, ..., a11_01, a0_02...]

that is, I want to take the first column from each of the 12 arrays and add them, in order, to my new array, then take the second column of each of the 12 arrays and and those, and so on... 也就是说,我想从12个数组中的每一个中提取第一列,然后按顺序将它们添加到我的新数组中,然后再获取12个数组中的每个以及它们之间的第二列,依此类推...

what I've most recently tried just repeats the first 12 values from each array through the entire new array, timedata... 我最近尝试过的只是在整个新数组,时间数据中重复每个数组的前12个值...

for i in range(len(files)):
    data = loadtxt(files[i], skiprows=4, delimiter=',').T[0:,:]
    timedata[i::12] = data[0,0]

I've tried nested for loops and indexing the arrays in different ways but have not gotten anything to work so far... Any ideas would be greatly appreciated. 我已经尝试过嵌套嵌套并以不同的方式对数组建立索引,但是到目前为止还没有任何可用的方法……任何想法将不胜感激。

thank you 谢谢

You basically have a jagged array that is 12 x 3 x (120 or 121). 您的锯齿阵列基本上是12 x 3 x(120或121)。 If the last column of the a05 through a11 were filled this would be a bit easier. 如果将a05到a11的最后一栏填满,这会容易一些。 Instead, you can iterate through the columns from 0 to 120; 相反,您可以在0到120的列之间进行迭代; and iterate through the arrays; 并遍历数组; and add the column to the new array only if it exists. 并将该列添加到新数组(如果存在)。

Here is some sample code. 这是一些示例代码。 Note that I used lengths of 11 and 12 instead of 120 and 121, but the idea is the same. 请注意,我使用11和12的长度而不是120和121的长度,但是想法是相同的。

import numpy as np

np.random.seed(1000)
a01 = np.random.randint(0,10, (3,12))
a02 = np.random.randint(0,10, (3,12))
a03 = np.random.randint(0,10, (3,12))
a04 = np.random.randint(0,10, (3,12))
a05 = np.random.randint(0,10, (3,12))
a06 = np.random.randint(0,10, (3,11))
a07 = np.random.randint(0,10, (3,11))
a08 = np.random.randint(0,10, (3,11))
a09 = np.random.randint(0,10, (3,11))
a10 = np.random.randint(0,10, (3,11))
a11 = np.random.randint(0,10, (3,11))
arrayList = [a01, a02, a03, a04, a05, a06, a07, a08, a09, a10, a11]
cols = np.sum([a.shape[1] for a in arrayList])
newArray = np.zeros((3,cols))

arrIndex = 0
for i in range(12):
    for a in arrayList:
        try: 
            newArray[:,arrIndex] = a[:,i]
            arrIndex = arrIndex + 1
        except IndexError:
            pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM