Oli
Oli

Reputation: 43

How to create list of matrices from three lists in python 2.7?

I need to read a txt file in python 2.7 and create list of quadruples from the readings. The elements in the txt file are blocks of 4 lines. Thus every fourth line is a new element. Here is a sample element of the file:

0 3 53
-0.999909 -0.0135226 851.576 
0.0135226 -0.999909 901.481 
0 0 1 
...

Thus from this sample the element of the new list will be l=[('0','3', 53, matrix[[-0.999909 -0.0135226 851.576],[0.0135226 -0.999909 901.481],[0 0 1]])] All those tuples in l well be the read elements from txt file in that form (string, string, integer, matrix). I have developed the code to form lists of the first three items in the tuple, but I have trouble doing the list of matrices for the fourth element of the tuple. Here is what I have so far:

import numpy as np                                                   
import re                                                            
import operator                                                      

file=open('matching.txt','r')                                        
f=file.readlines()                                                   
v1, v2, w, r1, r2, r3 = [],[],[], [], [], []                         
for x, line in enumerate(f):                                         
    if x % 4 is 0:                                                   
         v1.append(line[:1])                                         
         v2.append(line[2:3])                                        
         str = re.search(' ([0-9]*)\r\n', line)                      
         if str:                                                     
             found = int(str.group(1))                               
         w.append(found)                                             
    elif x % 4 is 1:                                                 
        r1.append(line)                                              
    elif x % 4 is 2:                                                 
        r2.append(line)                                              
    else:                                                            
        r3.append(line)        

I need to create list m of matrices from the three above lists r1,r2,r3 whose elements will be the building blocks for the matrices.Elements of r1 are the first row of the matrices in m following the same index, r2 are the secon row of the matrices in m following the same index, r3 are the third row of matrices in m following the same index. The list m should look something like this: m=[matrix[[r1[0]][r2[0]][r3[0]]],matrix[[r1[1]][r2[1]][r3[1]]]...] How do I create list m from the r1,r2,r3 from the above code?

Thank you in advace for your assistance.

Upvotes: 1

Views: 2584

Answers (4)

O.rka
O.rka

Reputation: 30687

Building off of @MaxU , I usually use pandas DataFrames if the columns and rows are labeled

import pandas as pd
import numpy as np

DF = pd.read_table("a.data", sep=" ") 

DF
          0         1        2
0  0.000000  3.000000   53.000
1 -0.999909 -0.013523  851.576
2  0.013523 -0.999909  901.481
3  0.000000  0.000000    1.000
4  0.000000  4.000000   54.000
5 -0.999909 -0.013523  851.576
6  0.013523 -0.999909  901.481
7  1.000000  1.000000    2.000

then you can set the labels by

DF.columns = #list of column labels
DF.index = #list of index labels

btw, indexing an array is faster so if you want to index it doing using this way...store a variable DF.as_matrix() and then index that like A[i,j]. If you wanted to actually index the DataFrame then you do .iloc[i,j]

Upvotes: 1

prashkr
prashkr

Reputation: 398

I suppose this is what you are looking for.

import numpy as np                                                   
import re                                                            
import operator                                                      

final_list = []
with open('data.txt','r') as f:
    line_1 = [float(num) for num in f.readline().split()]
    line_2 = [float(num) for num in f.readline().split()]
    line_3 = [float(num) for num in f.readline().split()]
    line_4 = [float(num) for num in f.readline().split()]

    #creating matrix from the 3 lines
    mat = np.matrix([line_2, line_3, line_4])

    line_1.append(mat)
    final_list.append(tuple(line_1))

print final_list

Upvotes: 1

Alexis Clarembeau
Alexis Clarembeau

Reputation: 2964

Your code seems very complicated. Why don't you some something like:

import numpy as np                                                   
import re                                                            
import operator                                                      

# reading lines 
file=open('matching.txt','r')  
i = 0 
temp = [] # temp is a buffer used to remember the previous lines
for line in file: # for each line 
    res = list(map(int, line.split())) # res = [v1, v2, v3]
    temp.append(res)                   # adding res, so temp = [..... [v1, v2, v3]]

    if i % 4 == 3:  
        # each fourth line (the algorithm will do 0 1 2 3 4 5 6 7 8 ...) 
        # and i%4 = 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 ...
        # now, temp= [[v1, v2, v3], [v4, v5, v6], ...]
        print(np.matrix(temp)) # convert array of array to matrix
        temp= []


    i += 1 

Upvotes: 1

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210852

try this:

file a.data:

0 3 53
-0.999909 -0.0135226 851.576 
0.0135226 -0.999909 901.481 
0 0 1 
0 4 54
-0.999909 -0.0135226 851.576 
0.0135226 -0.999909 901.481 
1 1 2

Code:

In [47]: fn = 'a.data'
In [49]: np.loadtxt(fn)
Out[49]:
array([[  0.00000000e+00,   3.00000000e+00,   5.30000000e+01],
       [ -9.99909000e-01,  -1.35226000e-02,   8.51576000e+02],
       [  1.35226000e-02,  -9.99909000e-01,   9.01481000e+02],
       [  0.00000000e+00,   0.00000000e+00,   1.00000000e+00],
       [  0.00000000e+00,   4.00000000e+00,   5.40000000e+01],
       [ -9.99909000e-01,  -1.35226000e-02,   8.51576000e+02],
       [  1.35226000e-02,  -9.99909000e-01,   9.01481000e+02],
       [  1.00000000e+00,   1.00000000e+00,   2.00000000e+00]])

Upvotes: 1

Related Questions