sequence multiplied by -1
Yingjie Lan
lanyjie at yahoo.com
Sat Sep 25 08:11:39 EDT 2010
--- On Sat, 9/25/10, Thomas Jollans <thomas at jollybox.de> wrote:
> for every list l and integer n >= 0:
> len(l*n) == len(l)*n
Well, this invariance is indeed broken under my proposal.
But it is *already broken* in current python3k.
However, the following invariance is maintained under
my proposal:
len(l*n) == len(l) * abs(n),
which is also broken under current python3k.
if you think len(..) as a mathematical norm,
the above invariance makes perfect sense:
|| a * b || == ||a|| * |b|, b is real
>
> > Simply put, a sequence multiplied by -1 can give a
> reversed sequence.
>
> For that, we have slicing. A negative step value produces a
> reverse slice of
> the list. You can't argue that this makes sense, can you
>
> >>> [1,2,3,4][::-1]
> [4, 3, 2, 1]
> >>> [1,2,3,4][::-2]
> [4, 2]
> >>>
Having more than one way of doing things sometimes is good.
Slicing is a little more complex, what if you want this:
>>> ([1,2,3,4]*2)[::-1]
[4, 3, 2, 1, 4, 3, 2, 1]
under my new proposal, you simply do this:
>>> [1,2,3,4]*-2
[4, 3, 2, 1, 4, 3, 2, 1]
Cheers,
Yingjie
More information about the Python-list
mailing list