Reputation: 239
I spent more time on this issue than I am willing to admit. I have a function called:
def array_funct(filename):
...
data = np.array((array))
return data
which reads in .txt files from a folder and returns a numpy array.
The first row is a list of x coordinates and second row are the cooresponding y coordinates. Hence I use:
array_funct(filename)[:,0]
array_funct(filename)[:,1]
to access the x and y coordinates.
Now all I want to do is to create a for loop which would read in more than 1 file and store them in following way
for i in range(0,number_of_files):
array_funct(file[i])[:,0]
array_funct(file[i])[:,1]
Let's look at the x-lists which I get:
print(array_funct(file[0])[:,0])
[1,2,3,4]
print(array_funct(file[1])[:,0])
[2,4,6,8]
All I want is to take these two numpy like lists and create:
x_tot = [[1,2,3,4], [2,4,6,8]]
such that I can access the single lists element wise like:
x_tot[0] = [1,2,3,4]
Is that so hard? Should I stop using numpy array ? I would like to stay in numpy if possbile.
Also keep in mind that I made this example for just 2 files but it could be more. I just want to create a x_tot and y_tot for a variable amount of files I would read in. Such that:
x_tot = [[1,2,3],[2,3,4],[..],..]
x_tot = [[2,4,6],[4,6,8],[..],..]
Upvotes: 1
Views: 110
Reputation: 4487
Given the following array_funct
function and filenames
list:
def array_funct(filename):
# Fake random data, replace with data read from file
data_read = [[1,2,3,4], [5,6,7,8]] # [random.sample(range(1, 10), 7), random.sample(range(1, 10), 7)]
data = np.array(data_read)
return data
filenames = ['file1.txt', 'file2.txt']
Try this code:
lx = [list(array_funct(file)[0]) for file in filenames]
ly = [list(array_funct(file)[1]) for file in filenames]
Or more efficiently by reading and scrolling the file once:
all_data = [(list(arr[0]),list(arr[1])) for arr in [array_funct(f) for f in filenames]]
lx, ly = list(map(list, zip(*all_data)))
In both cases, the output is as follows:
# lx = [[1, 2, 3, 4], [1, 2, 3, 4]]
# ly = [[5, 6, 7, 8], [5, 6, 7, 8]]
Upvotes: 2
Reputation: 999
Something like:
x_tot = np.array([array_funct(file[i])[:,0] for i in range(0,number_of_files)])
should work.
Upvotes: 1