Connecting Tech Pros Worldwide Forums | Help | Site Map

array alice of [:-0] ??

guthrie
Guest
 
Posts: n/a
#1: Jul 18 '06
Beginner question! :-)

x=[1,2,3,4]
for i in range(len(x)):
print x[:-i]
Quote:
Quote:
Quote:
>>[]
>>[1,2,3]
>>[1,2]
>>[1]
1) The x[:-0] result seems inconsistent to me;
I get the idea that -0=0, so it is taken as x[:0] -[]
2) how then should one do this basic left-recursive subsetting (easily).

Thanks.


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

John Machin
Guest
 
Posts: n/a
#2: Jul 18 '06

re: array alice of [:-0] ??


On 18/07/2006 11:17 PM, guthrie wrote:
Quote:
Beginner question! :-)
>
x=[1,2,3,4]
for i in range(len(x)):
print x[:-i]
>
Quote:
Quote:
>>[]
>>[1,2,3]
>>[1,2]
>>[1]
>
1) The x[:-0] result seems inconsistent to me;
I get the idea that -0=0, so it is taken as x[:0] -[]
2) how then should one do this basic left-recursive subsetting (easily).
>
>
|>for i in range(len(x), -1, -1):
.... print x[:i]
....
[1, 2, 3, 4]
[1, 2, 3]
[1, 2]
[1]
[]
Boris Borcic
Guest
 
Posts: n/a
#3: Jul 18 '06

re: array alice of [:-0] ??


guthrie wrote:
Quote:
Beginner question! :-)
>
x=[1,2,3,4]
for i in range(len(x)):
print x[:-i]
>
Quote:
Quote:
>>[]
>>[1,2,3]
>>[1,2]
>>[1]
>
1) The x[:-0] result seems inconsistent to me;
I get the idea that -0=0, so it is taken as x[:0] -[]
that's what you get.
Quote:
2) how then should one do this basic left-recursive subsetting (easily).
I am not sure what you mean, but replacing x[:-i] by x[:len(x)-i] you will get
continuous behavior at i==0.
Antoon Pardon
Guest
 
Posts: n/a
#4: Jul 18 '06

re: array alice of [:-0] ??


On 2006-07-18, guthrie <guthrie@mum.eduwrote:
Quote:
Beginner question! :-)
>
x=[1,2,3,4]
for i in range(len(x)):
print x[:-i]
>
Quote:
Quote:
>[]
>[1,2,3]
>[1,2]
>[1]
>
1) The x[:-0] result seems inconsistent to me;
I get the idea that -0=0, so it is taken as x[:0] -[]
That is correct. Negative indexes are a handy shorthand, but
they can give unexpected/strange results in a number of cases.
Quote:
2) how then should one do this basic left-recursive subsetting (easily).
x=[1,2,3,4]
lng = len(x)
for i in range(lng):
print x[:lng-i]

--
Antoon Pardon
guthrie
Guest
 
Posts: n/a
#5: Jul 18 '06

re: array alice of [:-0] ??


Thanks all!!
---------------------

guthrie wrote:
Quote:
Beginner question! :-)
>
x=[1,2,3,4]
for i in range(len(x)):
print x[:-i]
>
Quote:
Quote:
>>[]
>>[1,2,3]
>>[1,2]
>>[1]
>
1) The x[:-0] result seems inconsistent to me;
I get the idea that -0=0, so it is taken as x[:0] -[]
2) how then should one do this basic left-recursive subsetting (easily).
>
Thanks.
>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Closed Thread