Reputation: 45
I'm looking for a way to import N
matrices from one file to a single 3D array. I know the number of rows m
and the number of columns n
of each matrix (respectively listed in two lists m[N]
and n[N]
).
For example I have N=3
matrices like these:
1 2 3 4
5 6 7 8
9 10 11 12
1 2 3
4 5 6
7 8 9
10 11 12
1 2 3 4 5 6
7 8 9 10 11 12
They are matrices of dimension 3x4
, 4x3
, 2x6
respectively, so in this case m = [3,4,2]
and n = [4,3,6]
.
I would store them in an object of the type M[i][j][k]
where i,j,k
are the indeces that identify the matrix, the row and the column respectively (in this case M[1][2][0]=7
).
For example in C++ this can be easily done:
ifstream file ("filename.txt");
for(int i = 0; i < N; i++)
for(int j = 0; j < m[i]; j++)
for(int k = 0; k < n[i]; k++)
file >> M[i][j][k];
file.close();
Thanks in advance.
Upvotes: 1
Views: 640
Reputation: 7123
You can use a list comprehension with itertools.islice
to get what you want. islice
allows you to read lines from the file without having to read the whole file into the memory.
from itertools import islice
m = [3, 4, 2]
n = [4, 3, 6]
M = []
with open('matrix_data.txt') as file:
for row_count in m:
rows = islice(file, row_count) # read `row_count` lines from the file
rows = [row.split() for row in rows] # split every line in the file on whitespace
rows = [[int(cell) for cell in row] for row in rows] # convert to int
M.append(rows) # add to `M`
for matrix in M:
print(matrix)
Output
[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]
Upvotes: 1