简体   繁体   中英

Save data in three arrays

I have this file :

1 2 3
2 1 3
7 8 9
4 5 3

and what I want is to create three arrays which save the data like this :

in the first array the data will be : 1, 2, 7, 4 in the second array the data will be : 2, 1, 8, 5 in the third array the data will be : 3, 3, 9, 3

I imagine, in a first time I have to get the number of columns to do this but I don't know if it is possible...

I think I can read all the lines and save in a array the data line by line and then create three arrays which contains what I want but it is a not the best. So it is the reason I ask you if you have a better idea to do this ?

Thank you very much !

You can read, split and zip (kind of transposes your data), converting to integer after split.

with open("file.txt") as f:
    s = list(zip(*([int(x) for x in l.split()] for l in txt)))

result:

[(1, 2, 7, 4), (2, 1, 8, 5), (3, 3, 9, 3)]

If all rows contain the same amount of columns you can simply use:

with open(filename,'r') as file:
    data = zip(*[[int(x) for x in line.split()] for line in file])

Now data will be:

data == [(1, 2, 7, 4), (2, 1, 8, 5), (3, 3, 9, 3)]

So you can - if you want lists - map them to list s and then use for instance sequence assignment to obtain the colums like:

with open(filename,'r') as file:
    col1,col2,col3 = zip(*[[int(x) for x in line.split()] for line in file])

In that case:

>>> col1
[1, 2, 7, 4]
>>> col2
[2, 1, 8, 5]
>>> col3
[3, 3, 9, 3]

In case there are more columns, and you are not interested in them, you can use a *_ to catch the other columns (and ignore them). Like:

with open(filename,'r') as file:
    col1,col2,col3 = map(list,zip(*[[int(x) for x in line.split()] for line in file]))

Or you can reduce the computational burden, by using islice :



with open(filename,'r') as file:
    col1,col2,col3 = map(list,zip(*[[int(x) for x in line.split()] for line in file]))

If you are going to do any array-based manipulation afterwards, consider using numpy . It has high-level interfaces for many operations

import numpy as np
data = np.loadtxt(fname)
# either separate out copies of the arrays explicitly
col1 = data[:,0]
col2 = data[:,1]
col3 = data[:,2]
# or just operate directly on a column (or row), e.g.
print(data[:,1].max())

Assuming you have opened the file object yourself, how about this?

array = [line.split(" ") for line in file]
x, y, z = zip(*array)

x, y, and z will now hold the first, second and third columns respectively.

First define the array and the path of your file:

path = ...
arr = []

And then iterate over each line of the file:

with open(path) as f:
    for line in f:
        arr.append(line.split(" "))

This is size independent, meaning the array can be as long or as wide as you want.

But, if the size and length of the integers are fixed you can use:

arr.append([line[0], line[2], line[4]]) # For one digit numbers, 3 columns
arr.append([line[0:1], line[3:4], line[5:6]]) # For 2 digit numbers, 3 columns

0, 2 and 4 are the positions of your numbers in the first case, a similar thing I did for the second

Use arr[0] , arr[1] , or arr[2] , to get the numbers of columns. Or arr[x][y] for a specific number.

Maybe try to use Numpy. Read the file as matrix then transpose it.

from numpy import *
a = mat('1, 2, 3; 2, 1, 3; 7, 8, 9; 4, 5, 3')
b = transpose(a)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM