tungwaiyip.info

 

home

about me

links

my software

Media

Yucatán Photos

St Lucia Photos

Photo Album

Videos

Blog

< June 2005 >
SuMoTuWeThFrSa
    1 2 3 4
5 6 7 8 91011
12131415161718
19202122232425
2627282930  

past articles »

Click for San Francisco, California Forecast

San Francisco, USA

 

Python's half open index notation

Beginner programmers often wonder about Python's sequence indexing and slicing notation. Array index starts from 0. Slicing uses half open notations, where L[a:b] is a subsequence with index x where a <= x < b.

Why is the endpoint excluded? Isn't it more intuitive if array index starts from 1 and the endpoint is included, so that a 3 elements array is referenced as L[1:3] with items L[1], L[2], L[3]?

It turns out this notation is an elegant and deliberate design and it has some excellent properties.

We write programs to operate on arrays, to find their length, traverse the subsequences, split them or join them. The half open notation always show a simple pattern. But the inclusive notation often requires adding 1 or substracting 1 to the indexes in many operations. Thus it is more vulnerable to off-by-one-error. This article One True Way of array indexing discuss this at length. I have reproduce its example (with corrections) below:

Operation Half open Inclusive
length of a slice L[a:b] b-a (b-a+1)
first n characters of L L[:n] L[1:n]
last n characters of L L[-n:] L[len(L)-n+1:]
The identity slice L == L[0:len(L)] == L[:] L[1:len(L)]
The empty slice L[a:a] is empty for any a. perhaps L[a:a-1]?
A slice of length n, from point a L[a:a+n] L[a:a+n-1]
Split L[a:b] at index c L[a:b] == L[a:c]+L[c:b] L[a:c-1]+L[c:b]

Another important property is an empty sequence can be expressed by L[a:a], while there is no natural way to express an empty sequence with the inclusive notation. But do we really need to care about a special case? Absolutely! In fact failure to account for empty input is one of the most common error. Just like zero is a fundamental concept in mathematics, always think how you program can handle null input. An inferior approach is to represent empty sequence by None or null pointer. This creates a special case so that a variable need to be tested before dereferencing. Failure to do so contributes to unexpected exceptions. It is an elegant design that L[a:b] can also represent sequences with 0 length.

C++'s STL also choose this notation to represent a range. According to the literature this is crucial because "algorithms that operate on n things frequently require n+1 positions. Linear search, for example (find) must be able to return some value to indicate that the search was unsuccessful." I have seen so many people flunked link list or data structure exercises because they have trouble dealing with the end of a list. Often a good solution is shift the focus beyond the n concrete objects to the n+1 positions around them. I hope this help to make sense of the half open notation.

2005.06.16 [, ] - comments (0)

 

 

Comments (0)

past articles »

 

BBC News

 

Obama set for key Berlin speech

 

Libya 'halts Swiss oil shipments'

 

Arctic 'has 90bn barrels of oil'

 

US seeks boost to Pakistan F-16s

 

Zimbabweans 'start crisis talks'

 

Hunt begins for Karadzic helpers

 

Concern over French nuclear leaks

 

S African named UN rights chief

Thu, 24 July, 2008, 17:11 GMT 18:11 UK

more »

 

Slashdot News for nerds, stuff that matters

 

Two-Episode Watchmen Series Set as a Prequel (2008-07-24T17:26:00+00:00)

 

Apollo 14 Moonwalker Claims Aliens Exist (2008-07-24T16:48:00+00:00)

 

Mars In 3D (2008-07-24T16:05:00+00:00)

 

The Death of Nearly All Software Patents? (2008-07-24T15:25:00+00:00)

 

Researchers Face Jail Risk For Tor Snooping Study (2008-07-24T14:50:00+00:00)

 

Spam King Escapes From Federal Prison (2008-07-24T14:06:00+00:00)

 

Big Six UK ISPs Capitulate To Music Industry (2008-07-24T13:27:00+00:00)

 

Most Bank Websites Are Insecure (2008-07-24T12:46:00+00:00)

more »

 

SF Gate

 

Serbia seeks key to Karadzic's false identity (24 Jul 2008)

 

Cosco Busan operator accused of fabricating records to hinder probe (24 Jul 2008)

 

SF. tech stays jailed; prosecutors say he rigged network to implode (24 Jul 2008)

 

Obama tells Israel he's committed to its security (23 Jul 2008)

 

Pennsylvania infant cut from womb leaves hospital (23 Jul 2008)

 

Beijing to set up special Olympic protest zones (23 Jul 2008)

 

100 employees at French nuke site contaminated (23 Jul 2008)

 

Obama tries to reassure Israelis and Palestinians (23 Jul 2008)

 

Stocks fall after sales of existing homes decline (24 Jul 2008)

 

Southwest 2Q profit up, sales rise 11 percent (24 Jul 2008)

 

Existing home sales fall 2.6 percent in June (24 Jul 2008)

 

Vast oil, natural gas reserves estimated in Arctic (24 Jul 2008)

 

Report: Grand jury investigating subprime lenders (24 Jul 2008)

 

Occidental Petroleum 2Q profit sets record (24 Jul 2008)

more »

 

Asia Times Online

 

CHAN AKYA : A Turkish theater for World War III (24 Jul 2008)

 

Al-Qaeda hostage plot suspected (24 Jul 2008)

 

Taking the high ground at Preah Vihear (24 Jul 2008)

 

Step by step to democracy in China (24 Jul 2008)

 

COMMENT: For Iran, respect above all else (24 Jul 2008)

 

A glimmer of hope for Nepal (24 Jul 2008)

 

Sri Lanka marks a dark anniversary (24 Jul 2008)

 

Protectionism goes into reverse (24 Jul 2008)

 

Bernanke blighted by tunnel vision (24 Jul 2008)

 

COMMENT : No bottom for flailing financials (24 Jul 2008)

 

Pitfalls open in Philippine mining (24 Jul 2008)

more »

 


Site feed Updated: 2008-Jul-24 10:15