Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #48252

Re: Newbie: The philosophy behind list indexes

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.014
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; '(at': 0.04; 'indices': 0.07; 'reason,': 0.07; 'result,': 0.07; 'suppose': 0.07; '[0,': 0.09; 'ascii': 0.09; 'mind,': 0.09; 'omit': 0.09; 'trailing': 0.09; 'python': 0.11; 'random': 0.14; '"to': 0.16; '10:00': 0.16; '[2,': 0.16; 'elements,': 0.16; 'frankly': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'interpreted,': 0.16; 'iterating': 0.16; 'marker': 0.16; 'notations': 0.16; 'range.': 0.16; 'sense,': 0.16; "tomorrow's": 0.16; 'truncate': 0.16; 'zeroes': 0.16; '(0)': 0.16; 'applies': 0.16; 'elements': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'examples': 0.20; 'written': 0.21; '>>>': 0.22; 'instance,': 0.24; 'integer': 0.24; 'stopping': 0.24; 'regardless': 0.24; '(or': 0.24; '15,': 0.26; 'references': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; '[1]': 0.29; 'possibility': 0.29; "doesn't": 0.30; 'errors': 0.30; 'subject:list': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'getting': 0.31; 'easier': 0.31; '>>>>': 0.31; 'index,': 0.31; 'lists?': 0.31; 'slot': 0.31; 'service,': 0.32; 'beginning': 0.33; 'actual': 0.34; 'sense': 0.34; 'could': 0.34; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'useful': 0.36; "i'll": 0.36; 'behind': 0.37; 'changing': 0.37; 'two': 0.37; 'list.': 0.37; 'represent': 0.38; 'convention': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'extremely': 0.39; 'use.': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'even': 0.60; 'negative': 0.60; 'units': 0.60; 'most': 0.60; 'identify': 0.61; 'length': 0.61; 'till': 0.61; 'numbers': 0.61; 'range': 0.61; 'simply': 0.61; "you'll": 0.62; 'show': 0.63; 'real': 0.63; 'such': 0.63; 'subject:The': 0.64; 'more': 0.64; 'taking': 0.65; 'world': 0.66; 'positions': 0.67; 'between': 0.67; 'wish': 0.70; 'to,': 0.72; 'obvious': 0.74; 'ranges': 0.74; 'hand': 0.80; 'glad': 0.83; 'church': 0.84; 'discrete': 0.84; 'editions).': 0.84; 'lasting': 0.84; 'observed': 0.84; 'philippians': 0.84; 'aka': 0.91; 'gaps': 0.93; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=Xpfqseh1l9lPi3/ZoUzuX6TpQ3EIkjZgEkpX4pM8M34=; b=iMtPzDBLj739Qj2iXH8JhIBPPtmHLbe9piiNFFOnxNRp0Xt4ohiq7nUTXIj3hifR+Q 5aYI89IondAve74zfpN+Qh0i5Sc61KR0iH+I9eJJ+MMfJ7E+2IdSufmWJ6T1YTfQxyZr qCUuHVDXKh6ULFSGwpCxLbU78vOBGSfya60+K2HqJ82NhB/tM3o7elupHni0ZYCTbsOL HpqbMC+PyEAVp7LEHd7YnARY2gruuoossA4QC33myghzWdH8AqtCKG3ZKgTKLWwDd5iI y/byVchi20QdPHJ7vOsyW5Sq9XaD9Z65w/VsEtzeyffwKF2DKB3y3pq5Yz0C8c6i9Hie d1Lw==
MIME-Version 1.0
X-Received by 10.220.48.17 with SMTP id p17mr899143vcf.97.1371275373574; Fri, 14 Jun 2013 22:49:33 -0700 (PDT)
In-Reply-To <f0ef08bc-464d-4d36-b285-ebc3c7ffb337@googlegroups.com>
References <f0ef08bc-464d-4d36-b285-ebc3c7ffb337@googlegroups.com>
Date Sat, 15 Jun 2013 15:49:33 +1000
Subject Re: Newbie: The philosophy behind list indexes
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3358.1371275375.3114.python-list@python.org> (permalink)
Lines 84
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1371275375 news.xs4all.nl 15967 [2001:888:2000:d::a6]:32807
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:48252

Show key headers only | View raw


On Sat, Jun 15, 2013 at 3:21 PM,  <ian.l.cameron@gmail.com> wrote:
> What is the thinking behind stopping 'one short' when slicing or iterating through lists?
>
> By example;
>
>>>> a=[0,1,2,3,4,5,6]
>>>> a
> [0, 1, 2, 3, 4, 5, 6]
>>>> a[2:5]
> [2, 3, 4]
>
> To my mind, it makes more sense to go to 5. I'm sure there's a good reason, but I'm worried it will result in a lot of 'one-off' errors for me, so I need to get my head around the philosophy of this behaviour, and where else it is observed (or not observed.)


There are two equally plausible ways to identify positions in a
list/string/whatever. One is to number the elements, the other to
number the gaps between them. I'll try my hand at some ASCII art... be
sure to view this in a monospaced font.


[  0  ,  1  ,  2  ,  3  ,  4  ,  5  ,  6  ]
|     |     |     |     |     |     |     |
0     1     2     3     4     5     6     7
-7   -6    -5    -4    -3    -2    -1    (0)

When you ask for the slice from 2 to 5, you get the elements between
those slot markers. That's [2,3,4].

When you ask for negative indices, the same applies, only there's no
parallel way to ask for negative 0 aka end of list. [1]

>>> a[2:-2]
[2, 3, 4]

There are a number of reasons for working this way. For instance, the
length of the range a[x:y] is simply y-x, negative indices aside. It's
even more significant when you look at something that doesn't have
discrete units - such as times.

Suppose you invent a data type to represent a time range. You might
describe a TV show as lasting from 10:00 till 10:30; but what do you
really mean by those times? Do you mean from the start of 10:00 until
the end of 10:30? When is the end of 10:30? Is it the end of the
minute 10:30, the end of the second 10:30:00, the end of the
millisecond 10:30:00.000? Easier to describe it as the beginning of
that moment, because that has the same meaning regardless of your
resolution. You can always add more trailing zeroes to either the
start time or the end time, without changing the meaning of the range.

Same applies to generation of random numbers. If you have a function
that generates a random number uniformly in the range [0,1) - that is,
including 0 but not including 1 - and you multiply it by an integer
and truncate the decimal, you get a random integer uniformly in the
range [0,x), which is an extremely useful thing. You don't even need
to care what the actual range in the RNG is (does it produce 0.000
through 0.999, or 0.000000 through 0.999999?), as long as it's
significantly more than your target range. But if the RNG could return
1.0, then you need to deal with that possibility in your result, which
frankly isn't much use.

It takes some getting used to, perhaps, given that most people in the
real world work with closed ranges; but ultimately it makes far more
sense. And if it weren't for a huge case of lock-in, I would wish we
could change the way Scripture references are interpreted, for the
same reasons. Taking examples from tomorrow's church service, two of
the readings are Matthew 18:15-20 and Philippians 3:8-10. When you
look in a Bible, you'll find verse numbers preceding the verses (at
least, that's the convention in most editions). If the ranges were
written as half-open (eg Matt 18:15-21), it would be simply from verse
marker 15 to verse marker 21; and "to the end of the chapter" or "to
the end of the book" would have obvious notations (eg Matt 18:15-19:1
or Matt 27-Mark 1). Of course, this would make for a huge amount of
confusion, since the present system has been around for centuries...
but it would make more sense, so I'm very much glad it's the way
Python chose to do it :)

ChrisA

[1] You can use None or omit the index, but there's no "negative 0"
integer to use.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Newbie: The philosophy behind list indexes ian.l.cameron@gmail.com - 2013-06-14 22:21 -0700
  Re: Newbie: The philosophy behind list indexes Chris Rebert <clp2@rebertia.com> - 2013-06-14 22:35 -0700
  Re: Newbie: The philosophy behind list indexes Chris Angelico <rosuav@gmail.com> - 2013-06-15 15:49 +1000
  Re: Newbie: The philosophy behind list indexes Peter Otten <__peter__@web.de> - 2013-06-15 08:42 +0200
  Re: Newbie: The philosophy behind list indexes John Ladasky <john_ladasky@sbcglobal.net> - 2013-06-15 00:19 -0700
  Re: Newbie: The philosophy behind list indexes Olive <not0read0765@yopmail.com> - 2013-06-15 09:44 +0200
  Re: Newbie: The philosophy behind list indexes ian.l.cameron@gmail.com - 2013-06-19 13:46 -0700

csiph-web