python中column什么意思_如何从Python列表中提取columnes?(How to extract columnes from Python list?)...

My Python code

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)

Couple of lines from 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

Couple of lines from 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

I wanted to read the file as a list,then choose every third row and then finally select first and the third column from that list.It seems that I do not understand how operator works.

If I try with Back2Basics solution import numpy as np

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

I got

File "a12.py", line 4, in

anotherarray = myarray[::3][0,2]

IndexError: too many indices

你可能感兴趣的:(python中column什么意思_如何从Python列表中提取columnes?(How to extract columnes from Python list?)...)