Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Data structure index Start at 1 instead of 0?

I have a weird question: I have this list of 64 numbers that will never change:

(2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128)

I need a data structure in Python that will allow me to accsess these numbers using a 1-64 index as opposed to the standard 0-63. Is this possible? Would the best way to accomplish this be to build a dictionary?

like image 683
NASA Intern Avatar asked Jul 24 '12 02:07

NASA Intern


People also ask

How do you start an index from 1 instead of 0?

You don't have that choice. Python indexing starts at 0, and is not configurable. at which point the next index used will be 1.

Does Python indexing start at 0 or 1?

The list index starts with 0 in Python. So, the index value of 1 is 0, 'two' is 1 and 3 is 2.

Does array indexing start at 1 in Python?

Each item in an array has a specific address. Individual items are accessed by referencing their index number. Indexing in Python, and in all programming languages and computing in general, starts at 0 . It is important to remember that counting starts at 0 and not at 1 .

Why do Python indices start at 0?

The first element of the array is exactly contained in the memory location that array refers (0 elements away), so it should be denoted as array[0]. Most programming languages have been designed this way, so indexing from 0 is pretty much inherent to the language as most of the languages (not all) follow C standards.


4 Answers

Just insert a 0 at the beginning of the structure:

(0, 2, 4, 6, 8, ...)
like image 185
Joel Cornett Avatar answered Oct 27 '22 04:10

Joel Cornett


You could override the item getter and make a specialized tuple:

class BaseOneTuple(tuple):
    __slots__ = () # Space optimization, see: http://stackoverflow.com/questions/472000/python-slots 
    def __new__(cls, *items):
        return tuple.__new__(cls, items) # Creates new instance of tuple
    def __getitem__(self, n):
        return tuple.__getitem__(self, n - 1)


b = BaseOneTuple(*range(2, 129, 2))
b[2] == 4
like image 24
kay Avatar answered Oct 27 '22 06:10

kay


You could use a dictionary, or you could simply subtract one from your index before accessing it.

Also, I note that your 64 numbers are in a simple arithmetic progression. Why store them at all? You can use this:

def my_number(i):
    return 2*i

If the list you showed was actually an example, and the real numbers are more complicated, then use a list with a dummy first element:

my_nums = [0, 2, 4, 6, 8, ....]

Then you can get 2 as my_nums[1].

like image 24
Ned Batchelder Avatar answered Oct 27 '22 04:10

Ned Batchelder


You could use range(2, 129, 2) to generate the numbers in the range 1 - 128 in increments of 2 and convert this list into a tuple if it's not going to change.

t = tuple(range(2, 129, 2))

def numbers(n):
   return t[n-1]

Given the global tuple t, function numbers could retrieve elements using a 1-based (instead of 0-based) index.

like image 34
Levon Avatar answered Oct 27 '22 04:10

Levon