enhancing slicing

Chris Barker chrishbarker at home.net
Mon Aug 27 14:52:59 EDT 2001


"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 --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list