By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,985 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,985 IT Pros & Developers. It's quick & easy.

slice lists

P: n/a
Reading:

http://docs.python.org/ref/slicings.html

it would seem to indicate that the ff will work:

L=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

however, you get:
l[3:4:,5:8:]

Traceback (most recent call last):
File "<pyshell#153>", line 1, in ?
l[3:4:,5:8:]
TypeError: list indices must be integers

in Python 2.3... are they only available in 2.4?
Also,

http://www.python.org/doc/2.3/whatsn...on-slices.html

mentions that:

"Ever since Python 1.4, the slicing syntax
has supported an optional third 'step' or 'stride'
argument. For example, these are all legal Python syntax:
L[1:10:2], L[:-1:1], L[::-1]."

and yet, we see in:

http://pyds.muensterland.org/weblog/2004/12/25.html

that something as simple as:

l = range(0,10)
print l[1:5] # this works
print l[1:5:2] # this barfs

fails in Python 2.2. What gives?

Sep 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
jb********@yahoo.com wrote:
Reading:

http://docs.python.org/ref/slicings.html

it would seem to indicate that the ff will work:

L=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

however, you get:
>>> l[3:4:,5:8:]
Traceback (most recent call last):
File "<pyshell#153>", line 1, in ?
l[3:4:,5:8:]
TypeError: list indices must be integers

in Python 2.3... are they only available in 2.4?


Lists are one-dimensional. They can only take one slice, not two.
Also,

http://www.python.org/doc/2.3/whatsn...on-slices.html

mentions that:

"Ever since Python 1.4, the slicing syntax
has supported an optional third 'step' or 'stride'
argument. For example, these are all legal Python syntax:
L[1:10:2], L[:-1:1], L[::-1]."

and yet, we see in:

http://pyds.muensterland.org/weblog/2004/12/25.html

that something as simple as:

l = range(0,10)
print l[1:5] # this works
print l[1:5:2] # this barfs

fails in Python 2.2. What gives?


The syntax was there since 1.4 for the Numeric module[1], but the list
object itself wasn't updated to utilize that syntax until later.

[1] http://numeric.scipy.org

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Sep 19 '05 #2

P: n/a
> Lists are one-dimensional. They can only take one slice, not two.

Ah ok, I thought

l[3:4,5:8]

would return

[3,5,6,7]

Sep 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.