简体   繁体   English

在Python中从现有2D数组创建3D数组

[英]Create 3D array from existing 2D array in Python

I am trying to create an array that is with dimensions: 我正在尝试创建一个具有维度的数组:

a(Days,Hours,Station)

I have hourly data for array 'a' for 2 stations over 61 days so currently I have this array with these dimensions: 我在61天内有2个站点的阵列'a'的每小时数据,因此目前我具有以下尺寸的阵列:

a(1464,2)

Where the 1464 is the number of hourly data points per station that I have (24 hours*61 days). 其中1464是我拥有的每个站的小时数据点数(24小时* 61天)。 However I want to break it down even further and add another dimension that has Days so the dimensions would then be: 但是,我想进一步细分它,并添加另一个具有Days的维度,因此维度将是:

a(61 days,24 hours/day, 2 stations)

Any ideas on how I would correctly be able to take the array 'a' that I currently have and change it into these 3 dimensions? 关于如何正确获取当前拥有的数组“ a”并将其更改为这3个维度的任何想法?

You could try to make a 61x24x2 array. 您可以尝试制作一个61x24x2的阵列。 This should work: 这应该工作:

b = []
for i in xrange(61):
    b.append(a[i*61:(i+1)*61])

This will split array a to chunks with maximum length size . 这会将数组a拆分为最大长度为size块。

def chunks( a, size ):
    arr = iter( a )
    for v in arr:
        tmp = [ v ]
        for i,v in zip( range( size - 1 ), arr ):
            tmp.append( v )

        yield tmp

splitted = list( chunks( a, 24 ) )

If you're first field is hours * days , a transformation would simply be: a(x, y) => a(x // 24, x % 24, y) 如果您的第一个字段是hours * days ,则转换将简单地是:a(x,y)=> a(x // 24,x%24,y)

x // 24 is floor division so 1500 // 24 = 62 , the days. x // 24是楼层划分,因此1500 // 24 = 62 (天)。 You did not specify, but I assume the "Hours" field would be the remaining hours; 您没有指定,但我假设“小时数”字段为剩余时间; so x % 24 gets the remaining hours, and 1500 % 25 = 12 , the number of hours. 因此x % 24获得剩余小时数,而1500 % 25 = 12 (小时数)。 Lastly, the station field remains the same. 最后,桩号字段保持不变。

I don't think you can modify the structure of a list/array in Python, so you would need to create a new one. 我认为您无法在Python中修改列表/数组的结构,因此您需要创建一个新的列表/数组。 I'm also not sure if you're actually using the built-in list or the array . 我也不确定您是否实际使用内置列表数组 I'm not too familiar with the array class so this isn't a complete answer, but I hope it points you in the right direction arithmetically. 我对数组类不太熟悉,所以这不是一个完整的答案,但我希望它能在算术上为您指明正确的方向。

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

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