我现在对Numpy数组有问题。如果这个问题已经被问到其他地方,我很抱歉,但我觉得我已经到处找过了。
我最初的问题是试图创建一个数组,并用多组不同大小的站点数据填充它。因为我不能用大小不同的数据集填充同一个数组,所以我决定需要为每个station数据集创建一个新数组,方法是在for循环中定义用于遍历每个station数据集的数组。这样做的问题是,在循环过程中,每个数据集将覆盖以前的数据集,只返回for循环的最终实例。
然后,我尝试使用+和join操作连接每个数组的一个新标题,但在定义数组时发现这是非法的。这是程序的实例,其中每个数据数组都覆盖上一个数组。注意,并不是所有的代码都包含在内,这是定义的一部分。for k in range(len(stat_id)):
## NOTE - more code precedes this final portion of the for loop, but was
## not included as it is unrelated to the issue at hand.
# Bring all the data into one big array.
metar_dat = np.zeros((len(stat_id),len(temp),7), dtype='object')
for i in range(len(temp)):
metar_dat[k,i] = np.dstack((stat_id[k], yr[i], month[i], day[i], time[i], temp[i], dwp[i]))
#print np.shape(metar_dat[k])
#print metar_dat[k]
#print np.shape(metar_dat) # Confirm success with shape read.
return metar_dat
在运行并打印此定义中的数组时,我得到以下结果(两个空数组和一个最终填充数组):[[[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
...,
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]]
[[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
...,
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]]
[[\TZR 2015 7 ..., 2342 58 48]
[\TZR 2015 7 ..., 2300 59 47]
[\TZR 2015 7 ..., 2200 60 48]
...,
[\TZR 2015 7 ..., 0042 56 56]
[\TZR 2015 7 ..., 0022 56 56]
[\TZR 2015 7 ..., 0000 56 56]]]
我的问题是:
如何为每组站数据创建一个数组,以便不覆盖任何以前的数据?
或者
如何创建包含不同行数的数据集的单个数组?
我对Python还不熟悉(也不太熟悉这里的帖子),任何想法都会非常感谢。