python list index - an easy question

Gregory Ewing greg.ewing at canterbury.ac.nz
Mon Dec 19 16:43:35 EST 2016


BartC wrote:
> But if you needed a table of the frequencies of letters A to Z...
> An N-based array can simply have bounds of ord('A') to ord('Z') 
> inclusive.

That's fine if your language lets you have arrays with
arbitrary lower bounds.

But if the language only allows a fixed lower bound, and
furthermore insists that the lower bound be 1, then your
indexing expression becomes:

    table[ord(letter) - ord('A') + 1]

If a language is only going to allow me a single lower
bound, I'd rather it be 0, because I can easily shift
that by whatever offset I need. But if it's 1, often
I need to cancel out an unwanted 1 first, leading to code
that's harder to reason about.

-- 
Greg


More information about the Python-list mailing list