如何从Python列表中提取columnes?

时间:2016-05-09 20:56:45

标签: python list operators

我的Python代码

import operator

with open('index.txt') as f:
    lines = f.read().splitlines()

print type(lines)
print len(lines)

l2=lines[1::3] 
print len(l2)
print l2[0]

list1 = [0,2]
my_items = operator.itemgetter(*list1)
new_list = [ my_items(x) for x in l2 ]

with open('newindex1.txt','w') as thefile:
    for item in l2:
        thefile.write("%s\n" % item)

来自index.txt

的几行
0 0 0
0 1 0
0 2 0
1 0 0
1 1 0
1 2 0
2 0 0
2 1 0
2 2 0
3 0 0

来自newindex1.txt

的几行
0 1 0
1 1 0
2 1 0
3 1 0
4 1 0
5 1 0
6 1 0
7 1 0
8 1 0
9 1 0

我想将文件作为列表读取,然后选择每个第三行,然后最终从该列表中选择第一列和第三列。似乎我不明白运算符是如何工作的。

如果我尝试使用Back2Basics解决方案 导入numpy为np

myarray = np.fromfile(' index.txt',dtype = int,sep ='') anotherarray = myarray [:: 3] [0,2]

我得到了

  File "a12.py", line 4, in <module>
    anotherarray = myarray[::3][0,2]
IndexError: too many indices

3 个答案:

答案 0 :(得分:2)

您根本不需要将所有数据都读入内存,您可以使用itertools.islice来解析所需的行,并使用csv lib来读取和写入数据:

from operator import itemgetter
from itertools import islice
import  csv

with open("in.txt") as f, open('newindex1.txt','w') as out:
    r = csv.reader(f, delimiter=" ")
    wr = csv.writer(out, delimiter=" ")
    for row in iter(lambda: list(islice(r, 0, 3, 3)), []):
        wr.writerow(map(itemgetter(0, 2), row)[0])

答案 1 :(得分:1)

我强烈建议使用numpy。这是因为所有数值数据都非常适合记忆。代码看起来像这样。

import numpy as np
myarray = np.fromfile('index.txt', dtype=int, sep=' ')
anotherarray = myarray[::3,::2]

然后你想写文件

anotherarray.tofile('newfile.txt', sep=" ")

数组切片行[::3,::2]读取的方式是“从0开始每隔3行,从0开始每隔一列”

答案 2 :(得分:1)

我觉得你需要这个吗?

lines = []

with open('index.txt', 'r') as fi:
    lines = fi.read().splitlines()

lines = [line.split() for line in lines]

with open('answer.txt', 'w') as fo:
    for column in range(len(lines)):
        if (column + 1) % 3:
            fo.write('%s %s\n' % (lines[column][0], lines[column][2]))