Re: Assignment to slice

Discussion in 'Python' started by James Henderson, Jan 21, 2004.

  1. On Wednesday 21 January 2004 7:15 pm, Rich Krauter wrote:
    > I see your point. However I could be wrong, but I
    > don't know if what you say is really the case, since
    > negative indexes are just shorthand for
    >
    > actual positive index = i + len(x),
    >
    > where i is the negative index, and len(x) >= abs(i).
    >
    > e.g.
    >
    > x = [1,2,3,4,5]
    > x[-4] is equivalent to x[-4+len(x)], which is x[1]
    > x[-4:-2] is equivalent to x[(-4+len(x)):(-2+len(x))],
    > which is x[1:3]
    >
    > I contend that my restriction may still hold even in
    > the case of negative indices, since negative indices
    > (which are just convenient shorthand), should map to
    > positive indices.
    >
    > In that case,
    > x = [1,2,3]
    > x[-10:-9] = [54] should return an exception too since,
    >
    > x[-10:-9] = x[-10+len(x):-9+len(x)] == x[-7:-6], which
    > is REALLY out of bounds of x.


    Assignment to x[-4:-3] is not same as assignment to x[-1:0]:

    >>> x = [1, 2, 3]
    >>> x[-4:-3] = [56]
    >>> x

    [56, 1, 2, 3]
    >>> x = [1, 2, 3]
    >>> x[-1:0] = [56]
    >>> x

    [1, 2, 56, 3]

    Although x[-4:-3] and x[-1:0] both evaluate to the same thing (an empty list).
    I would be interested to hear what anyone else has to say about this.

    > Instead python will just tack stuff on to the front of
    > the array. Which, I still believe, is totally
    > inconsistent. But what do I know? I have about 5
    > minutes of python experience.
    >
    > I guess I just don't like that behaviour. At least in
    > perl if I assign something to a list slice:
    >
    > @x[2..3] = (4,5);
    >
    > the list grows, so that the things I inserted are at
    > the indices where I think I inserted them.
    > That is, @x now contains (0,0,4,5).


    That's another way to do it, I suppose. :)

    James
    --
    James Henderson, Logical Progression Ltd.
    http://www.logicalprogression.net/
    http://sourceforge.net/projects/mailmanager/
    James Henderson, Jan 21, 2004
    #1
    1. Advertising

  2. James Henderson

    r.e.s. Guest

    "James Henderson" <> wrote ...

    JH Assignment to x[-4:-3] is not same as assignment to x[-1:0]:
    JH
    JH >>> x = [1, 2, 3]
    JH >>> x[-4:-3] = [56]
    JH >>> x
    JH [56, 1, 2, 3]
    JH >>> x = [1, 2, 3]
    JH >>> x[-1:0] = [56]
    JH >>> x
    JH [1, 2, 56, 3]
    JH
    JH Although x[-4:-3] and x[-1:0] both evaluate to the same
    JH thing (an empty list).
    JH I would be interested to hear what anyone else has to say
    JH about this.

    Here's my beginner's take on "slice indices" ...

    Since len(x)==3, *only* the values -1, -2, and -3 are shorthand,
    standing for 2, 1, 0 resp.; i.e., only -1, -2, ..., -len(x) are
    shorthand, and any other negative integer is equivalent to 0;
    any positive integer values > len(x) are equivalent to len(x).

    So, x[-4:-3] stands for x[0:0], while x[-1:0] stands for x[2:0].
    Both x[0:0] and x[2:0] are empty lists, but x[0:0] is located
    "just before x[0]" whereas x[2:0] is located ""just before x[2]",
    and the assignments amount to insertions at the resp. locations.

    I trust someone will correct me if I'm wrong, but it seems to me
    a simpler restatement of what's in the Python Reference Manual.

    --r.e.s.
    r.e.s., Jan 22, 2004
    #2
    1. Advertising

  3. On Thursday 22 January 2004 4:54 am, r.e.s. wrote:
    > "James Henderson" <> wrote ...
    >
    > JH Assignment to x[-4:-3] is not same as assignment to x[-1:0]:


    Since this wasn't made clear in r.e.s.'s post I'd just like to point out that
    the above obervation was in response to the proposal that a negative index
    "i" was simply a shorthand for "i + len(x)". (len(x) was 3.)

    James
    --
    James Henderson, Logical Progression Ltd.
    http://www.logicalprogression.net/
    http://sourceforge.net/projects/mailmanager/
    James Henderson, Jan 22, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rich Krauter

    Assignment to slice

    Rich Krauter, Jan 21, 2004, in forum: Python
    Replies:
    0
    Views:
    341
    Rich Krauter
    Jan 21, 2004
  2. Rich Krauter

    RE: Assignment to slice

    Rich Krauter, Jan 21, 2004, in forum: Python
    Replies:
    6
    Views:
    331
    sambo
    Jan 23, 2004
  3. Jens Lippmann

    Slice assignment for strings?

    Jens Lippmann, May 25, 2004, in forum: Python
    Replies:
    4
    Views:
    446
    Paul Rubin
    May 25, 2004
  4. Andrew
    Replies:
    2
    Views:
    222
    Stephen Horne
    Sep 21, 2008
  5. Daku
    Replies:
    0
    Views:
    695
Loading...

Share This Page