enhancing slicing

Rich Harkins rharkins at thinkronize.com
Mon Aug 27 15:24:13 EDT 2001


You could wrap the list in a class that would do this.  I don't know whether
it is a bug but if you do something like this in python:

>>> l=MyListClass()
>>> print l[2,3,4]

Then l.__getitem__ will be called with a tuple (not an integer!) as its sole
argument and from that you could implement what you suggest.

Not quite as good as what you wanted but at least it can be done now.

Rich


> -----Original Message-----
> From: python-list-admin at python.org
> [mailto:python-list-admin at python.org]On Behalf Of Chris Barker
> Sent: Monday, August 27, 2001 2:53 PM
> To: python-list at python.org
> Subject: Re: enhancing slicing
>
>
> "Juan Valiño" wrote:
>
> > My proposal is to introduce a new operator, for instance # (count):
> > v[5 # 2]
> > Extract 2 elements starting at 5: [ v[5], v[6] ]. Equivalent to v[5:5+2]
>
> Frankly, I think this really is just unneccesary sugar(not that I don't
> like sugar, mind you!). What I really would like, and think would be a
> major leap forward, would be to allow sequence indexing:
>
> indexes = [1,4,5,7]
>
> l = ['a','b,'c','d','e','f','g','h']
>
> l[indexes] == ['b', 'e', 'f', 'h']
>
>
> Now you have do do something like:
>
> >>> l2 = []
> >>> for i in indexes:
> ...     l2.append(l[i])
> ...
> >>> l2
> ['b', 'e', 'f', 'h']
>
> or with a list comprehension:
>
> >>> [l[i] for i in range(len(l)) if (i in indexes)]
> ['b', 'e', 'f', 'h']
>
> Both of which are far more wordy and confusing than my suggestion.
>
> By the way you can sort of do this with Numeric:
> >>> from Numeric import take
> >>> take(l,indexes)
> array([b, e, f, h],'c')
>
> but that's still kind of klunky.
>
> -Chris
>
>
>
>
>
> --
> Christopher Barker,
> Ph.D.
> ChrisHBarker at home.net                 ---           ---           ---
> http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
>                                    ------@@@     ------@@@     ------@@@
> Oil Spill Modeling                ------   @    ------   @   ------   @
> Water Resources Engineering       -------      ---------     --------
> Coastal and Fluvial Hydrodynamics --------------------------------------
> ------------------------------------------------------------------------
> --
> http://mail.python.org/mailman/listinfo/python-list
>





More information about the Python-list mailing list