Slicing every element of a list

A

Alex Dempsey

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 went without returning any errors, but nothing was sliced or
split. Next I tried:

for i in range(len(lines)):
lines = lines[1:-5]
lines = lines.split('\"\t\"')

This of course worked, but why didn't the first one work. Further why
didn't the first one return an error?
 
S

Scott David Daniels

Alex said:
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 went without returning any errors, but nothing was sliced or
split. Next I tried:

for i in range(len(lines)):
lines = lines[1:-5]
lines = lines.split('\"\t\"')

This of course worked, but why didn't the first one work. Further why
didn't the first one return an error?


result = [line[1 : -5].split('\"\t\"') for line in lines]

--Scott David Daniels
(e-mail address removed)
 
T

Thomas Lotze

Alex said:
for line in lines:
line = line[1:-5]
line = line.split('\"\t\"')

This went without returning any errors, but nothing was sliced or split.
Next I tried:

for i in range(len(lines)):
lines = lines[1:-5]
lines = lines.split('\"\t\"')

This of course worked, but why didn't the first one work.


Because when assigning to line the second time, you just make the
identifier reference a new object, you don't touch the list. This is how
one might do it without ranging over the length of the list and having
to get the lines out by element access:

for i, line in enumerate(lines):
line = line[1:-5]
lines = line.split('\"\t\"')

Probably there are even better ways, this is just off the top of my
head.
Further why
didn't the first one return an error?

Because you didn't make any. You just discarded your results; why should
anyone stop you from burning cycles? *g
 
J

John Machin

Alex said:
Recently I tried to slice every element of a list of strings. First I tried:

"slice"? Interesting terminology. Next problem you have, try posting an
example of your input, and your expected output.

E.g.
repr(input_string): '"foo"\t"barre"\t"zot"\t"X"\n'
repr(output_list): ['foo', 'barre', 'zot']

Then folk would be able to point out that the csv module might be a tad
more appropriate -- it gives you a general solution.

I'm presuming that the hard-coded number "5" means that you have a
single-character field at the end that you want to ignore. Your code
would be more robust in the face of change were you to ignore the last
field after splitting instead of the last 5 chars before splitting.
f = open("export.xls", "r")


Aarrgghh!! a file with a ".xls" extension that you can read as a text file??

lines = f.readlines()

for line in lines:
line = line[1:-5]
line = line.split('\"\t\"')

This went without returning any errors, but nothing was sliced or
split. Next I tried:

for i in range(len(lines)):
lines = lines[1:-5]
lines = lines.split('\"\t\"')

This of course worked, but why didn't the first one work. Further why
didn't the first one return an error?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top