Python Numpy append array without flattening
The fastest way would be vstack
data = np.vstack((get_data() for i in range(datalen)))
vstack requires a tuple/iterable
data = np.vstack((data1, data2, data3))
or you can do this by appending with axis=0
data = np.empty(shape=(0, 3))data = np.append(data, datai.reshape((-1, 3)), axis=0) # -1 will make the rows automatic
Solution 1 using list comprehensions:
data = []datalen = 4datai = range(1,4)data = [list(datai) for _ in range(datalen)]print (data)
Output
[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
Solution 2 (just a bit lengthy)
data = []datalen = 4for i in range(datalen): datai = range(1,4) data.append(list(datai))print (data)
with the same output as above. In the second method you can also just simply use data.append(list(range(1,4)))
. You can choose if you want to convert datai
to list or not. If you want the final output as an array, you can just use np.array()