enhancing slicing
Juan Valiño
juanv at posta.unizar.es
Thu Aug 23 08:24:40 EDT 2001
Many times I want to extract a sublist and I have the initial position p
plus the length n.
So I do:
v[p:p+n]
Many times the initial position p is an expression. Hence, I have to
repeat it tediously (and with a possible lost of performance) or assign it
previously to a variable. The last is boring and is a little trick out of
the logic of the program
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]
v[5 # -2]
Extract 2 elements ending at (but not including the) 5: [ v[3], v[4] ].
Equivalent to v[5-2:5]
I think that this little syntactic sugar allows programs more compact and
readable. Also, I had less "start and end point paranoias". I suppose that
the implementation is easy and the current programs are not affected at all.
More information about the Python-list
mailing list