In python and/or numpy: I have several 1D vectors (arrays) that I read in from files one at a time. They do not all have the same number of coefficients, i.e. not the same length. i would like to add each vector to a list of vectors and address them as objects, sucking them back out one at a time for processing. I have read any questions/answers that sound like this process: How to build a matrix one column at a time
Vectorized way to arrange vector into matrix (numpy)
and I've tried various python/numpy functions including append, concatenate, stack, appending to an object array using an increasing index, etc. For example:
filenames = pd.read_csv('filenames', header=None)
numFiles = filenames.shape[0]
# read in all files
firstTime = True
x0 = [numFiles]
y0 = [numFiles]
for i in range(numFiles):
fn = filenames.iloc[i, 0]
df = pd.read_csv(fn, delimiter='\t', header=None)
# select first two columns in data frame
nparr = np.array(df)[:,0:2]
# this one line does a loop, discarding entries with zero flux
# THIS IS THE PLACE where all vectors wind up with a different length
nparr = nparr[nparr[:,1] != 0]
x0[i] = nparr[:,0]
y0[i] = nparr[:,1]
# by this point, object array is complete
Any syntax I try either genrates an error, in this case:
Traceback (most recent call last):
File "InterpolateToSameDMValues-26-Jun-2019.py", line 48, in <module>
x0[i] = nparr[:,0]
IndexError: list assignment index out of range
or else numpy simply appends all my vectors into one very long vector.
How can I tell python to not do anything fancy, and simply create an array of objects I can iterate through?
for obj in x0:
# do something with vector
x0 = [10]doesn't make a list 10 items long. It makes a list with one item, the number 10.