1

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],[..],..]

2 Answers 2

2

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]]
Sign up to request clarification or add additional context in comments.

Comments

1

Something like:

x_tot = np.array([array_funct(file[i])[:,0] for i in range(0,number_of_files)])

should work.

4 Comments

It works but it also includes the word 'array'. Is that a problem? [array([ 16863.2, 23735. , 107915.1, 52643.9, 68688.5, 74618.5, 94618.5]) array([ 16864.3, 23737.3, 107917.4, 52645.7, 68690.8, 74620.6])]
From array_funct(file[i])[:,0] you obtain a numpy array (that is the reason of 'array' word), I made a list comprehension to join the numpy array for every file and, finally, because you said that you would like to stay in numpy I converted the list in numpy array with np.array().
You are right and on paper it should work. But I am getting a 'ValueError: setting an array element with a sequence.' BUT it works if I were to use a regular np list like x_tot = [[1,2,3],[2,3,4]] for example. Something is not quite working
Funny. A file like [list([1, 2, 3, 4, 5, 9, 8, 7]) list([5, 4, 10, 11, 3]) list([3, 6, 7, 12, 7, 9])] which is also a np object can be read in, no problem. But [array([ 16863.2, 23735. , 107915.1, 52643.9, 68688.5, 74618.5, 94618.5]) array([ 16864.3, 23737.3, 107917.4, 52645.7, 68690.8, 74620.6])] CANNOT be read in. Why...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.