【发布时间】:2015-08-19 18:37:32
【问题描述】:
我是 python 新手。我已经问了另一个问题How to arrange three lists in such a way that the sum of corresponding elements if greater then appear first? 现在问题如下:
我正在处理一个大型文本文件,其中有 419040 行和 6 列包含浮点数。其中,我使用前 3 列来生成这三个列表。所以我实际使用的列表每个都有 419040 个条目。当我运行 python 代码将三列提取到三个列表中时,python shell 没有响应,我怀疑有大量的条目,我使用了这个代码:
file=open("file_location","r")
a=[]
b=[]
c=[]
for lines in file:
x=lines.split(" ")
a.append(float(x[0]))
b.append(float(x[1]))
c.append(float(x[2]))
注意:对于小文件,此代码运行良好。 为避免此问题,我使用以下代码:
import numpy as np
a = []
b = []
c = []
a,b,c = np.genfromtxt('file_location',usecols = [0,1,2], unpack=True)
因此,当我运行上一个问题的答案中给出的代码时,同样的问题正在发生。那么使用numpy的对应代码是什么?或者,还有其他解决方案吗?
【问题讨论】:
标签: arrays list file python-3.x numpy