Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Assignment to slice

Reply
Thread Tools

Re: Assignment to slice

 
 
James Henderson
Guest
Posts: n/a
 
      01-21-2004
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/


 
Reply With Quote
 
 
 
 
r.e.s.
Guest
Posts: n/a
 
      01-22-2004
"James Henderson" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
James Henderson
Guest
Posts: n/a
 
      01-22-2004
On Thursday 22 January 2004 4:54 am, r.e.s. wrote:
> "James Henderson" <(E-Mail Removed)> 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/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Slice assignment problem - help requested Daku VHDL 0 11-18-2009 04:54 PM
explain slice assignment to newb Andrew Python 2 09-21-2008 02:23 AM
Slice assignment for strings? Jens Lippmann Python 4 05-25-2004 08:56 PM
RE: Assignment to slice Rich Krauter Python 6 01-23-2004 05:33 PM
Assignment to slice Rich Krauter Python 0 01-21-2004 05:01 PM



Advertisments