Re: Slicing every element of a list
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: Slicing every element of a list

From: Gary Herron <gherron@islandtraining.com>
Date: Tue Jul 12 2005 - 21:35:51 CEST

Alex Dempsey wrote:

>Recently I tried to slice every element of a list of strings. First I tried:
>
>f = open("export.xls", "r")
>lines = f.readlines()
>
>for line in lines:
> line = line[1:-5]
> line = line.split('\"\t\"')
>
>
This, in fact, did do the operation you expected, but after creating the
new value and assigning it to line, you promptly threw it away. (Because
the loop then went back to the top and (re)assigned the next thing in
lines to line wiping out your nicely sliced computation in lines.) You
need to *do* something with the value in line before you end the loop --
but what?

>This went without returning any errors, but nothing was sliced or
>split. Next I tried:
>
>for i in range(len(lines)):
> lines[i] = lines[i][1:-5]
> lines[i] = lines[i].split('\"\t\"')
>
>This of course worked, but why didn't the first one work. Further why
>didn't the first one return an error?
>
>
Dr. Gary Herron
Digipen Institute of Technology
Received on Thu Sep 29 16:54:44 2005