[Tutor] index swap?

Kent Johnson kent37 at tds.net
Fri Feb 23 13:29:04 CET 2007

Switanek, Nick wrote:
> 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]

This could be written more simply using a single list comprehension:
data = [ line.rstrip().split('\t') for line in open(inpath) ]

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

Again I would write this with nested list comps:
dataOut = [ [ row[i] for i in indices ] for row in data ]

> 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.

Are you sure the headers and data are in the order you think they are? I 
suspect that your data is not what you think it is. What do you get if 
you try
for row in data[:3]:
   print repr(row)
   print row[28]
   print row[29]


More information about the Tutor mailing list