[Python-3000] indexing wart

Michael Urman murman at gmail.com
Wed Mar 5 15:16:11 CET 2008


On Wed, Mar 5, 2008 at 5:33 AM, Neal Becker <ndbecker2 at gmail.com> wrote:
> It is a bit unfortunate that slicing has a singularity.
>
> samples_to_trim = (some calculation yielding an integer >= 0)
>
>
> trimmed_vector = vector[:-samples_to_trim] if samples_to_trim != 0 else
> vector[:]
>
> A bit unfortunate that the case of samples_to_trim == 0 has to be handled
> differently.
>
> I don't really see any solution to this.

Just transform the problem away. If you can guarantee that
samples_to_trim is <= len(vector), then it's trivial:
trimmed_vector = vector[:len(vector) - samples_to_trim]

If not, it gets more complicated, but you can wrap the upper bound in
a max(0, ...).

-- 
Michael Urman


More information about the Python-3000 mailing list