Richard Rublev
Richard Rublev

Reputation: 8164

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 <module>
    anotherarray = myarray[::3][0,2]
IndexError: too many indices

Upvotes: 1

Views: 90

Answers (3)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

You don't need to read all the data into memory at all, you can use itertools.islice to parse the rows you want and the csv lib to read and write the data:

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])

Upvotes: 2

cmashinho
cmashinho

Reputation: 615

I think you need something this?

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]))

Upvotes: 1

Back2Basics
Back2Basics

Reputation: 7806

I'd highly suggest using numpy for this. The reason being this is all numerical data that fits so nicely into memory. The code looks like this.

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

and then you want to write the file

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

The way the array slicing line [::3,::2] reads is "take every 3rd row starting from 0, and take every other column starting from 0"

Upvotes: 1

Related Questions