And*_*way 3 python arrays io numpy file
我在文件中有一个数据集,如下所示
A 1
B 2
C 3
A 4
B 5
C 6
Run Code Online (Sandbox Code Playgroud)
我想只提取包含的行'A'.我该怎么做numpy.genfromtxt?numpy中是否有其他功能可以帮助我做同样的事情?
numpy.genfromtxt接受生成器,所以你可以链接genfromtext和ifilter:
from itertools import ifilter
with open(fname, 'rb') as inp:
filtered_inp = ifilter(lambda x: x.startswith('A'), inp)
a = np.genfromtxt(filtered_inp)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3207 次 |
| 最近记录: |