slice lists

J

jbperez808

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:
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/whatsnew/section-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?
 
R

Robert Kern

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:
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/whatsnew/section-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
(e-mail address removed)

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

jbperez808

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]
 

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
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top