Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

God doesn't play dice. -- Albert Einstein


devel / comp.lang.python / Re: Negative subscripts

SubjectAuthor
o Re: Negative subscriptsdn

1
Re: Negative subscripts

<mailman.24.1637961891.9869.python-list@python.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=20640&group=comp.lang.python#20640

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: PythonList@DancesWithMice.info (dn)
Newsgroups: comp.lang.python
Subject: Re: Negative subscripts
Date: Sat, 27 Nov 2021 10:24:23 +1300
Organization: DWM
Lines: 164
Message-ID: <mailman.24.1637961891.9869.python-list@python.org>
References: <96741f1a-72b3-6551-39b3-f1862c5a80b8@chagford.com>
<ba4a976c-5829-03ac-2e3f-cf670f00c3f7@DancesWithMice.info>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 4DleZQU4dQeMlWTkgY77KA5/JBWte3adkkPTLKKSDbeQ==
Return-Path: <PythonList@DancesWithMice.info>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=danceswithmice.info header.i=@danceswithmice.info
header.b=gzFHCSTj; dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.011
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '(which': 0.04; 'salt':
0.04; 'fairly': 0.05; "python's": 0.05; '=dn': 0.09; 'computing':
0.09; 'fails': 0.09; 'from:addr:danceswithmice.info': 0.09;
'from:addr:pythonlist': 0.09; 'treated': 0.09; 'values.': 0.09;
'tutorial': 0.12; "'d',": 0.16; '(3)': 0.16; '>>>>': 0.16;
'cater': 0.16; 'interpreted.': 0.16; 'message-
id:@DancesWithMice.info': 0.16; 'millman': 0.16; 'missed': 0.16;
'pertinent': 0.16; 'print(': 0.16; 'received:51.254': 0.16;
'received:51.254.211': 0.16; 'received:51.254.211.219': 0.16;
'received:cloud': 0.16; 'received:rangi.cloud': 0.16; 'somewhat':
0.16; 'whilst': 0.16; 'wrote:': 0.16; 'problem': 0.16; 'python':
0.16; 'reached': 0.17; 'values': 0.17; 'to:addr:python-list':
0.20; 'problem,': 0.22; 'returns': 0.22; 'code': 0.23; 'idea':
0.24; '(and': 0.25; 'actual': 0.25; 'depends': 0.25; 'examples':
0.25; 'cannot': 0.25; 'anyone': 0.25; 'seems': 0.26; '>>>': 0.28;
'example,': 0.28; 'header:User-Agent:1': 0.30; 'default': 0.31;
'program,': 0.31; 'putting': 0.31; 'header:Organization:1': 0.31;
'program': 0.31; 'empty': 0.32; 'nature': 0.32; 'negative': 0.32;
'words,': 0.32; 'zero': 0.32; 'but': 0.32; "i'm": 0.33; 'there':
0.33; 'same': 0.34; 'header:In-Reply-To:1': 0.34; '"the': 0.35;
'item': 0.35; 'yes,': 0.35; 'following': 0.35; 'those': 0.36;
'missing': 0.37; 'received:192.168': 0.37; 'way': 0.38; 'two':
0.39; 'list': 0.39; 'use': 0.39; 'still': 0.40; 'both': 0.40;
'want': 0.40; 'including': 0.60; 'included': 0.61; 'remember':
0.61; 'skip:o 10': 0.61; 'shall': 0.61; 'above': 0.62; 'to:':
0.62; 'skip:z 10': 0.62; 'techniques': 0.62; 'becomes': 0.64;
'received:51': 0.64; 'thus': 0.64; 'look': 0.65;
'received:userid': 0.66; 'now,': 0.67; 'entire': 0.67; 'exactly':
0.68; 'items': 0.68; 'lie': 0.69; 'sequence': 0.69; 'above,':
0.70; 'rules': 0.70; '....': 0.76; 'religious': 0.76; 'attribute':
0.84; 'indices': 0.84; 'lacks': 0.84; 'mysterious': 0.84;
'pressing': 0.84; 'thus,': 0.84; 'thus:': 0.84; 'greater': 0.91;
'stopping': 0.91; 'tend': 0.91; 'boy': 0.93
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on vps517507.ovh.net
X-Spam-Level:
X-Spam-Status: No, score=-5.0 required=5.0 tests=ALL_TRUSTED,BAYES_00,
DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,NICE_REPLY_A autolearn=ham
autolearn_force=no version=3.4.0
DKIM-Filter: OpenDKIM Filter v2.11.0 mail.rangi.cloud A686E732E
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=danceswithmice.info;
s=staff; t=1637961880;
bh=RC2340Baa93qtWLHI22U/L2WKECQOoOKKjLrxQOd7uI=;
h=Date:Subject:To:References:From:In-Reply-To:From;
b=gzFHCSTjuD++URHMdMxtY0TcbvdfWoJXJhshLmiT0/piThVid/mIlJRUMvqkY8D3j
lG7cs0Q7UXRSgJtoyCaVqIxHFf2LlBwNQ3J4CfNuoPJZoDLqtJtSLLxOaxF3y5jK9E
Wm5mEaZw7uOBekwQhBBrhZjwuUJKBu8p3D0clhjRceHfDfAou+9YH5gEf0ymFsCOQF
NArW9oHXivL+rGHNsdiMftR0hTCnVHPJenw8W9QJ7XQqng8mkIOS49xBueLQFR+JdD
KqWKwDIXzWnnx9eWA4xhAsn+UJCwLQSFMlmqx7d4as62pQ0Ah3a2DJqIKiqmqVbzyT
nPA5ABFnk/3MA==
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.3.2
Content-Language: en-GB
In-Reply-To: <96741f1a-72b3-6551-39b3-f1862c5a80b8@chagford.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.37
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <ba4a976c-5829-03ac-2e3f-cf670f00c3f7@DancesWithMice.info>
X-Mailman-Original-References: <96741f1a-72b3-6551-39b3-f1862c5a80b8@chagford.com>
 by: dn - Fri, 26 Nov 2021 21:24 UTC

On 26/11/2021 22.17, Frank Millman wrote:
> In my program I have a for-loop like this -
>
>>>> for item in x[:-y]:
> ...    [do stuff]
>
> 'y' may or may not be 0. If it is 0 I want to process the entire list
> 'x', but of course -0 equals 0, so it returns an empty list.
....

> But in my actual program, both x and y are fairly long expressions, so
> the result is pretty ugly.
>
> Are there any other techniques anyone can suggest, or is the only
> alternative to use if...then...else to cater for y = 0?

Here's hoping that this good-looking boy has not missed the point about
"ugly".

Is this the problem, re-stated to be fully explicit, with (almost)
zero-complexity? [Zen of Python]

>>> x = [ "a", "b", "c", "d", "e" ]
>>> for y in [ 1, 2, 3, 4, 5 ]:
.... print( y, x[ :-y ] )
....
1 ['a', 'b', 'c', 'd']
2 ['a', 'b', 'c']
3 ['a', 'b']
4 ['a']
5 []

Whilst the code 'works', it lacks the 'zero-case' of what is not to be
included - which is an overly-complicated way of saying: misses the
'all-case'.

As observed, if we add a zero to the slice/loop-control that fails
logically (and we get more religion: "the first shall be last, and the
last shall be first". [Christian Bible])

>>> for y in [ 0, 1, 2, 3, 4, 5 ]:
.... print( y, x[ :-y ] )
....
0 []
1 ['a', 'b', 'c', 'd']
2 ['a', 'b', 'c']
3 ['a', 'b']
4 ['a']
5 []

The problem, as expressed, is that whilst there is a negative of 1, ie
-1, and other positive-integers, there is no (material) negative of 0,
ie 0 == -0 == +0.

(if the following seems pedantic and 'talking down', it is because I'm
filleting a tutorial prepared for Python-Apprentices)

Remember that 'salvation' may lie in the somewhat mysterious rules of
slicing's default values. According to the Python rule-book (not exactly
a religious text, but we must tend it with some fervor... [Built-in
Types] https://docs.python.org/3/library/stdtypes.html?highlight=slice)

«
s[i] ith item of s, origin 0 (3)

s[i:j] slice of s from i to j (3)(4)

s[i:j:k] slice of s from i to j with step k (3)(5)

....

(3) If i or j is negative, the index is relative to the end of sequence
s: len(s) + i or len(s) + j is substituted. But note that -0 is still 0.
»

Which anticipates the problem 'here', and only rubs salt into the wound.

Pressing on...

«
(4) The slice of s from i to j is defined as the sequence of items with
index k such that i <= k < j. If i or j is greater than len(s), use
len(s). If i is omitted or None, use 0. If j is omitted or None, use
len(s). If i is greater than or equal to j, the slice is empty.

(5) The slice of s from i to j with step k is defined as the sequence of
items with index x = i + n*k such that 0 <= n < (j-i)/k. In other words,
the indices are i, i+k, i+2*k, i+3*k and so on, stopping when j is
reached (but never including j). When k is positive, i and j are reduced
to len(s) if they are greater. When k is negative, i and j are reduced
to len(s) - 1 if they are greater. If i or j are omitted or None, they
become “end” values (which end depends on the sign of k). Note, k cannot
be zero. If k is None, it is treated like 1.
»

....and so we see the open-closed nature of Python's ranges and
range-alike objects!

The 'secret' lies in the 'sins of omission'. As above, if either the
'start' or the 'stop' attribute is omitted, then it is treated as None, ie

x[ :-y ] becomes x[ None:-y ]

Thereafter we look at how None is interpreted. In the above example, the
'start' None becomes zero, thus:

x[ :-y ] becomes x[ None:-y ] and thus x[ 0:-y ]

The pertinent topic is the other end of the range. A 'stop' None will
become len( x ), thus putting it together with the 'start' points, the
short-hand form of a copy-operation is:

x[ : ] which becomes x[ None:None ] and thus x[ 0:len( x ) ]

Let's combine that with the idea of negative indexes/indices.
Referring-back to (3) (above).

If all of x is to be included: x[ :] is the way to go - but another way
to express that is:

x[ :len( x ) ]

If the last item in x is to be excluded: x[ :-1 ]

If the last two items ... excluded: x[ :-2 ]

(etc)

and another way to write those two specific examples is

x[ :len( x ) - 1 ] and x[ :len( x ) - 2 ]

and while we are taking things 'away' from the end of the iterable, the
missing x[ :len( x ) ]

is the same as x[ :len( x ) - 0 ]

Thus, by now, you've leaped ahead of me, to:

>>> for y in [ 0, 1, 2, 3, 4, 5 ]:
.... print( y, x[ :len( x ) - y ] )
....
0 ['a', 'b', 'c', 'd', 'e']
1 ['a', 'b', 'c', 'd']
2 ['a', 'b', 'c']
3 ['a', 'b']
4 ['a']
5 []

and yes, if computing y is expensive/ugly, for extra-credit, calculate
the 'stop' value outside/prior-to the for-loop!
--
Regards,
=dn


devel / comp.lang.python / Re: Negative subscripts

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor