473,387 Members | 1,863 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,387 software developers and data experts.

slice lists

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
2 1284
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Roberto A. F. De Almeida | last post by:
I found that when using negative indices, the slice object passed to __getitem__ depends on the number of slices. An example to clarify: class a: def __getitem__(self, index): return index ...
3
by: Thomas Covello | last post by:
Why does the following code give this error: >>> TypeError: sequence index must be integer
40
by: Ron Adam | last post by:
After considering several alternatives and trying out a few ideas with a modified list object Bengt Richter posted, (Thank You), I think I've found a way to make slice operation (especially far end...
0
by: Travis Oliphant | last post by:
This post is to gather feedback from the wider community on PEP 357. It is nearing the acceptance stage and has previously been discussed on python-dev. This is a chance for the wider Python...
19
by: George Sakkis | last post by:
It would be useful if list.sort() accepted two more optional parameters, start and stop, so that you can sort a slice in place. In other words, x = range(1000000) x.sort(start=3, stop=-1) ...
7
by: Alexandre Guimond | last post by:
Hi all, i'm trying to deepcopy a slice object but i get the following error. Does anyone know a workaround? ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on Python 2.4.3 (#69,...
2
by: smichr | last post by:
It seems to me that the indices() method for slices is could be improved. Right now it gives back concrete indices for a range of length n. That is, it does not return any None values. Using an...
3
by: Bas | last post by:
Hi, stupid question, but would it be possible to somehow merge xrange (which is supposed to replace range in py3k) and slice? Both have very similar start, stop and step arguments and both are...
8
by: John Salerno | last post by:
Hey all. I've decided I let my Python skills (minor though they were) slip away so I started reading the new edition of Learning Python to brush up. I just read about lists again and I'm wondering...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.