negative stride list slices
Shalabh Chaturvedi
shalabh at cafepy.com
Wed Sep 1 17:57:57 EDT 2004
Shalabh Chaturvedi wrote:
> I'd like to add:
>
> +---+---+---+---+---+
> | H | e | l | p | A |
> +---+---+---+---+---+
> 0 1 2 3 4 5
> -5 -4 -3 -2 -1
> | |
> start end (defaults for +ve step)
> end start (defaults for -ve step)
>
> (Is this correct?)
Apparently not.
>>> "HelpA"[-1:-2:-1]
'A'
I would expect 'p'. Also,
>>> "HelpA"[:-1:-1]
''
And I definitely expected 'A'. Now I find it confusing too.
Does anyone else find it intuitive to expect that S[b:a:-1] be exactly
reverse of S[a:b:1]?
This is only true for S[::1] and S[::-1]. In other cases, for -ve step,
the -ve indices seem to indicate a shifted position. I'm using Python 2.3.3.
--
Shalabh
More information about the Python-list
mailing list