[英]Creating List From File In Python
该文件包含:
1 19 15 36 23 18 39
2 36 23 4 18 26 9
3 35 6 16 11
从那以后我想提取如下列表:
L = [1,19,15,36,23,18,19,2,36........... ect.]
最有效的方法是什么?
您可以使用itertools.chain,拆分每一行并映射到整数:
from itertools import chain
with open("in.txt") as f:
print(list((map(int,chain.from_iterable(line.split() for line in f)))))
[1, 19, 15, 36, 23, 18, 39, 2, 36, 23, 4, 18, 26, 9, 3, 35, 6, 16, 11]
对于python2,使用itertools.imap
而不是map。 使用带有map的链和itertools.chain可以避免一次将所有文件读入内存,这就是.read
会做的事情。
python3在文件上的某些时间与输入* 1000相同:
In [5]: %%timeit
with open("ints.txt","r") as f:
list(map(int,re.split(r"\s+",f.read())))
...:
100 loops, best of 3: 8.55 ms per loop
In [6]: %%timeit
with open("ints.txt","r") as f:
list((map(int, chain.from_iterable(line.split() for line in f))))
...:
100 loops, best of 3: 5.76 ms per loop
In [7]: %%timeit
...: with open("ints.txt","r") as f:
...: [int(i) for i in f.read().split()]
...:
100 loops, best of 3: 5.82 ms per loop
所以itertools匹配list comp但使用的内存要少得多。
对于python2:
In [3]: %%timeit
with open("ints.txt","r") as f:
[int(i) for i in f.read().split()]
...:
100 loops, best of 3: 7.79 ms per loop
In [4]: %%timeit
with open("ints.txt","r") as f:
list(imap(int, chain.from_iterable(line.split() for line in f)))
...:
100 loops, best of 3: 8.03 ms per loop
In [5]: %%timeit
with open("ints.txt","r") as f:
list(imap(int,re.split(r"\s+",f.read())))
...:
100 loops, best of 3: 10.6 ms per loop
列表comp稍微快一些,但是再次使用更多内存,如果你要用读取分割方法读取所有内存,imap再次是最快的:
In [6]: %%timeit
...: with open("ints.txt","r") as f:
...: list(imap(int, f.read().split()))
...:
100 loops, best of 3: 6.85 ms per loop
对于python3和map也是如此:
In [4]: %%timeit
with open("ints.txt","r") as f:
list(map(int,f.read().split()))
...:
100 loops, best of 3: 4.41 ms per loop
因此,如果速度是你关心的所有使用list(map(int,f.read().split()))
或list(imap(int,f.read().split()))
方法。
如果记忆也是一个问题,请将它与链条结合起来。 链接方法的另一个优点是,如果您将内存传递给函数或迭代,您可以直接传递链对象,因此您根本不需要将所有数据保存在内存中。
最后一个小优化是在文件对象上映射str.split:
In [5]: %%timeit
with open("ints.txt", "r") as f:
list((map(int, chain.from_iterable(map(str.split, f)))))
...:
100 loops, best of 3: 5.32 ms per loop
with open('yourfile.txt') as f:
your_list = f.read().split()
将它强制转换为整数。 您可以使用列表compregension:
your_list = [int(i) for i in f.read().split()]
当无法输出值时,这可能会导致异常。
f=open("output.txt","r")
import re
print map(int,re.split(r"\s+",f.read()))
f.close()
您可以使用re.split
,它将返回一个列表并map
到int
。
如果您可以使用numpy
库,另一种方法是使用np.fromstring()
将文件的.read()
作为输入,示例 -
import numpy as np
with open('file.txt','r') as f:
lst = np.fromstring(f.read(),sep=' ',dtype=int)
最后lst
将是一个numpy数组,如果你想要一个python列表,使用list(lst)
numpy.fromstring()
始终返回一维数组,当您将空格作为分隔符时,它将忽略额外的空格,其中包括换行符。
示例/演示 -
In [39]: import numpy as np
In [40]: with open('a.txt','r') as f:
....: lst = np.fromstring(f.read(),sep=' ',dtype=int)
....:
In [41]: lst
Out[41]:
array([ 1, 19, 15, 36, 23, 18, 39, 2, 36, 23, 4, 18, 26, 9, 3, 35, 6,
16, 11])
In [42]: list(lst)
Out[42]: [1, 19, 15, 36, 23, 18, 39, 2, 36, 23, 4, 18, 26, 9, 3, 35, 6, 16, 11]
性能测试 -
In [47]: def func1():
....: with open('a.txt','r') as f:
....: lst = np.fromstring(f.read(),sep=' ',dtype=int)
....: return list(lst)
....:
In [37]: def func2():
....: with open('a.txt','r') as f:
....: return list((map(int,chain.from_iterable(line.split() for line in f))))
....:
In [54]: def func3():
....: with open('a.txt','r') as f:
....: return np.fromstring(f.read(),sep=' ',dtype=int)
....:
In [55]: %timeit func3()
10000 loops, best of 3: 183 µs per loop
In [56]: %timeit func1()
10000 loops, best of 3: 194 µs per loop
In [57]: %timeit func2()
10000 loops, best of 3: 212 µs per loop
如果你对numpy.ndarray
(与列表没有什么不同)没问题,那会更快。
你可以使用re.findall
。
import re
with open(file) as f:
print map(int, re.findall(r'\d+', f.read()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.