所以我在一个文件中有一个样本数据,它的安排是:
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
现在我想读取第一列并将其保存到列表“u”中,将第二列保存到列表“v”中,以此类推,直到“p”为止。这是我到目前为止所拥有的:
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)
我尝试过改变索引,但显然这是错误的,因为我得到了输出
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]]
它只是通过索引增加行号并读取整行,而我希望将每一列的数据写入一个单独的变量,即对应于示例数据中给出的名称 - u = 100 --> 107, v = 200 --> 207 等
关于如何在 Python 中执行此操作的任何想法?(我必须以迭代的方式对非常大的数据集执行此操作,因此快速高效的代码将大有裨益)