【发布时间】:2015-10-23 10:51:01
【问题描述】:
我目前遇到了 Numpy 数组的问题。如果这个问题已经在别处问过,我很抱歉,但我觉得我到处都看过。
我最初的问题是我试图创建一个数组并用多组不同大小的站数据填充它。由于我无法用大小不同的数据集填充同一个数组,因此我决定需要通过在我用来遍历每个站数据集的 for 循环内定义数组来为每个站数据集创建一个新数组。这样做的问题是,在循环时,每个数据集都会覆盖之前的数据集,只返回 for 循环的最终实例。
然后,我尝试使用 + 和连接操作为每个数组连接一个新标题,但在定义数组时发现这是非法的。这是程序的实例,其中每个数据数组都覆盖前一个数据数组。请注意,并非所有代码都包含在内,这是定义的一部分。
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 还是很陌生(对在这里发帖也是新手),任何想法都将不胜感激。
【问题讨论】:
-
感谢您的想法,但问题不在于填充数组,而在于在迭代 for 循环后保留每个数组(而不是覆盖它)。跨度>
标签: python arrays for-loop numpy