473,396 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Slicing every element of a list

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[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?
Jul 21 '05 #1
4 1531
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 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?


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

--Scott David Daniels
Sc***********@Acm.Org
Jul 21 '05 #2
Alex Dempsey wrote:
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[i] = lines[i][1:-5]
lines[i] = lines[i].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[i] = 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

--
Thomas
Jul 21 '05 #3
Alex Dempsey wrote:
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[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?

Jul 21 '05 #4
Alex Dempsey wrote:
Recently I tried to slice every element of a list of strings. First I tried:

f = open("export.xls", "r")


http://www.python.org/doc/2.4.1/lib/module-csv.html

(snip, see other posts in this thread)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Dave Benjamin | last post by:
Hey all, I just realized you can very easily implement a sequence grouping function using Python 2.3's fancy slicing support: def group(values, size): return map(None, * for i in...
9
by: Jerry Sievers | last post by:
Fellow Pythonists; I am totally puzzled on the use of slicing on mapping types and especially unsure on use of the Ellipsis... and slicing syntax that has two or more groups seperated by comma....
54
by: seberino | last post by:
Many people I know ask why Python does slicing the way it does..... Can anyone /please/ give me a good defense/justification??? I'm referring to why mystring gives me elements 0, 1, 2 and 3...
11
by: John Salerno | last post by:
Given: numbers = can someone explain to me why numbers results in ? I thought the first index, whether going forward or backward, was inclusive. And there is no index of 10 in this...
22
by: bearophileHUGS | last post by:
>From this interesting blog entry by Lawrence Oluyede: http://www.oluyede.org/blog/2006/07/05/europython-day-2/ and the Py3.0 PEPs, I think the people working on Py3.0 are doing a good job, I am...
18
by: John Henry | last post by:
If I have a list of say, 10 elements and I need to slice it into irregular size list, I would have to create a bunch of temporary variables and then regroup them afterwords, like: # Just for...
2
by: David TG | last post by:
Hiya! I would like to take a partial slice of a fairly complex array. The source looks something like array ( ==array ( ==NN
18
by: Nik Coughlin | last post by:
I am halfway through writing a tutorial on image slicing for fluid CSS layouts, I would love some feedback on what I've done up until this point: http://nrkn.com/index.html I am still writing...
16
by: Yves Dorfsman | last post by:
Is there a way to do: x = x That would return: I am surprised this notation is not supported, it seems intuitive. A concrete example of the sort of thing I want to do:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.