这基本上就是我想要做的:
array = np.array() #initialize the array. This is where the error code described below is thrown
for i in xrange(?): #in the full version of this code, this loop goes through the length of a file. I won't know the length until I go through it. The point of the question is to see if you can build the array without knowing its exact size beforehand
A = random.randint(0,10)
B = random.randint(0,10)
C = random.randint(0,10)
D = random.randint(0,10)
row = [A,B,C,D]
array[i:]= row # this is supposed to add a row to the array with A,C,B,D as column values
Run Code Online (Sandbox Code Playgroud)
这段代码不起作用。首先它抱怨: TypeError: required argument 'object' (pos 1) not found。但我不知道数组的最终大小。
其次,我知道最后一行是不正确的,但我不确定如何在 python/numpy 中调用它。那么我该怎么做呢?
必须创建具有固定大小的 numpy 数组。您可以创建一个小的行(例如一行),然后一次追加一行,但这将是低效的。没有办法有效地将 numpy 数组逐渐增长到不确定的大小。您需要提前决定您想要的大小,或者接受您的代码效率低下的事实。根据数据的格式,您可以使用pandasnumpy.loadtxt中的类似函数或各种函数来读取数据。
| 归档时间: |
|
| 查看次数: |
11302 次 |
| 最近记录: |