[Tutor] index swap?

Switanek, Nick nswitanek at stanford.edu
Fri Feb 23 09:39:44 CET 2007


Hi,

 

I'm trying to subset a large dataset (too many rows for Excel, currently
too ragged for Access), and I am mystified by the behavior of a
seemingly simple script I've written. Please help me understand what I'm
overlooking.

 

data = file(inpath).readlines()

data = [line.rstrip() for line in data]

data = [line.split('\t') for line in data]

 

# I wish to select the elements of a list by using a list of indices:

indices = [0, 1, 28, 29]   # for example

 

dataOut = []

for row in data:

    rowOut = []

    for i in indices:

        rowOut.append(row[i])

    dataOut.append(rowOut)

 

 

The problem is that while the first list in the list of lists 'dataOut',
i.e. my header row, has taken the headers with the appropriate indices
from the index list, all other rows swap the elements with indices 28
and 29. If I include just one of the two indices 28 or 29, the problem
remains: save for the header list, I get element 29 where I want 28, and
element 29 when I want element 28. This swapping doesn't occur for any
other indices that I've tested. 

 

Running the same code interactively doesn't seem to result in the index
swap. I'm sure I'm overlooking something basic, and I'd be grateful for
your help pointing it out.

 

 

Many thanks in advance,

Nick

 

 

 

 

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070223/53414223/attachment.htm 


More information about the Tutor mailing list