So I have a sample data in a file, which is of the arrangement:
u v w p
100 200 300 400
101 201 301 401
102 202 302 402
103 203 303 403
104 204 304 404
105 205 305 405
106 206 306 406
107 207 307 407
Now I want to read the 1st column and save it into a list 'u' , 2nd column into a list 'v' and so on for every column till 'p'. This is what I have so far:
import numpy as np
u = []
v = []
w = []
p = []
with open('testdata.dat') as f:
for line in f:
for x in line.split():
u.append([int(x)])
v.append([int(x)+1])
w.append([int(x)+2])
p.append([int(x)+3])
print 'u is'
print(u)
print 'v is'
print(v)
print 'w is'
print(w)
print 'p is'
print(p)
I have tried varying the indices, but obviously it is wrong since I get the output
u is
[[100], [200], [300], [400], [101], [201], [301], [401], [102], [202], [302],
[402], [103], [203], [303], [403], [104], [204], [304], [404], [105], [205],
[305], [405], [106], [206], [306], [406], [107], [207], [307], [407]]
v is
[[101], [201], [301], [401], [102], [202], [302], [402], [103], [203], [303],
[403], [104], [204], [304], [404], [105], [205], [305], [405], [106], [206],
[306], [406], [107], [207], [307], [407], [108], [208], [308], [408]]
w is
[[102], [202], [302], [402], [103], [203], [303], [403], [104], [204], [304],
[404], [105], [205], [305], [405], [106], [206], [306], [406], [107], [207],
[307], [407], [108], [208], [308], [408], [109], [209], [309], [409]]
p is
[[103], [203], [303], [403], [104], [204], [304], [404], [105], [205], [305],
[405], [106], [206], [306], [406], [107], [207], [307], [407], [108], [208],
[308], [408], [109], [209], [309], [409], [110], [210], [310], [410]]
It just increments the row number by the index and reads the entire row, whereas I want data from every column written to a separate variable,i.e corresponding to the names given in the sample data - u = 100 --> 107, v = 200 --> 207 etc.
Any ideas on how to do this in Python ? ( I have to perform this operation on really large datasets in an iterative manner,So a fast and efficient code would be of great benefit)
timeit
. Would be cool if you share your results! – Sylvain Leroux 18 hours ago