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

 

Labour MPs call for hacking probe (06 Sep 2010)

 

Bipolar 'not linked to violence' (06 Sep 2010)

 

Housing group 'near administration' (07 Sep 2010)

 

MPs back AV referendum bill (06 Sep 2010)

 

Tube workers begin 24-hour strike (06 Sep 2010)

 

Scam fear over electricity credit (06 Sep 2010)

 

Iran 'hampers IAEA investigation' (06 Sep 2010)

 

Obama unveils building plan (06 Sep 2010)

 

France faces pension plan strike (06 Sep 2010)

 

MoD names two killed UK soldiers (06 Sep 2010)

more »

 

Slashdot News for nerds, stuff that matters

 

Google Says Microsoft Is Driving Antitrust Review (2010-09-07T00:20:00Z)

 

Aging Star System Leaves Strange Death Spiral (2010-09-06T23:25:00Z)

 

American Business Embraces 'Gamification' (2010-09-06T22:36:00Z)

 

Plagiarizing a Takedown Notice (2010-09-06T21:40:00Z)

 

Sony Has Lost the PS3 Hacking War (2010-09-06T20:53:00Z)

 

UK's Royal Mail Launches First Intelligent Stamps (2010-09-06T20:01:00Z)

 

WikiLeaks Calls For Assange To Step Down (2010-09-06T19:10:00Z)

 

Programming Things I Wish I Knew Earlier (2010-09-06T18:14:00Z)

more »

 

TechPsychic Tech Rumors and Invented News

 

TechPsychic: AT&T: more money, says it's disruptive in funding from. (08 May 2010)

 

TechPsychic: I know that Apple is close to Apple Dominates, Hires ex-Googler - Yes, Android phones. (08 May 2010)

 

TechPsychic: AT&T says: Facebook Connect. (08 May 2010)

 

TechPsychic: Google's Nexus One of Google Chrome Release Adds Support subscriptions accounted for Amazon: Apple. (08 May 2010)

 

TechPsychic: Another stat: Twitter's Design of this is giving rise of BlackBerry Foursquare Map App store end. (07 May 2010)

 

TechPsychic: Like educational sales Up around Apple iPad makes money Plan costs half an Apple. (07 May 2010)

 

TechPsychic: Instead added extensions, social Networks than double, everyone jumps in Silicon Valley? (07 May 2010)

 

TechPsychic: So why iTunes App lets Social Networks Verizon Wireless Internet. (07 May 2010)

more »

 

SF Gate

 

Lawyer says Iranian woman could be stoned soon (2010-09-06T19:54:30UTC)

 

No body found in Pittsburg dump after 3 days (2010-09-06T07:42:40UTC)

 

Tight California races head into final frenzy (2010-09-06T12:39:06UTC)

 

Economy blamed for drop in births (2010-09-06T13:37:54UTC)

 

Obama assails GOP, promotes new jobs program (2010-09-06T20:26:24UTC)

 

US won't say if blowout preventer on way to shore (2010-09-06T20:25:23UTC)

 

Foster City's Hillbarn Theatre gets bell back (2010-09-06T17:38:14UTC)

 

Hassle in Haight over McDonald's menu change (2010-09-06T14:38:38UTC)

 

Air Products boosts Airgas acquisition offer (2010-09-06T21:33:46UTC)

 

UK regulators want Avandia diabetes pill pulled (2010-09-06T21:11:46UTC)

 

Revis returns to Jets after agreeing to deal (2010-09-06T21:10:44UTC)

 

Obama assails GOP, promotes new jobs program (2010-09-06T20:57:31UTC)

 

Matusz wins 4th straight, leads O's over Yanks 4-3 (2010-09-06T20:54:32UTC)

 

Presented By: (06 Sep 2010)

more »

 

Asia Times Online

 

Taiwan in a rice wine stew (3 Sep 2010)

 

Inspectors miss the flight to Kyrgyzstan (3 Sep 2010)

 

LIFE IN TALIBANISTAN : Married to the mob (3 Sep 2010)

 

Old Korea hand points new finger of blame (3 Sep 2010)

 

New case for US reparations in Laos (3 Sep 2010)

 

BOOK REVIEW : Al-Qaeda and counter-terrorism (3 Sep 2010)

 

Deripaska on US mission (3 Sep 2010)

 

IMF stumps up Pakistan aid (3 Sep 2010)

 

MARKET RAP : Confused - but forward (3 Sep 2010)

 

IT WORLD : Delhi targets Google, Skype (3 Sep 2010)

 

THE MOGAMBO GURU : Bernanke blows his cover (3 Sep 2010)

more »

 


Site feed Updated: 2010-Sep-06 19:00