You don't have that choice. Python indexing starts at 0, and is not configurable.
You can just subtract 1 from your indices when indexing:
array.insert(i - 1, element) # but better just use array.append(element)
print(i, array[i - 1])
or (more wasteful), start your list with a dummy value at index 0:
array = [None]
at which point the next index used will be 1.
Answer from Martijn Pieters on Stack OverflowIs there a specific reason it was made this way? Personally, it gets confusing when I have to remember that the first character of a string has an index of 0...
No, there's no way to do this. Python is a 0-indexed language. Guido Van Rossum (Python creator) explained his reasons behind selecting 0-indexing over 1-indexing in this blog post:
http://python-history.blogspot.com/2013/10/why-python-uses-0-based-indexing.html
As far as Python is concerned, there cannot be changes in the Indexing part. It always starts with 0 (Zero) only and progresses onwards. Hope this helps you.
To explain it in another way, because -0 is equal to 0, if backward starts from 0, it is ambiguous to the interpreter.
If you are confused about -, and looking for another way to index backwards more understandably, you can try ~, it is a mirror of forward:
arr = ["a", "b", "c", "d"]
print(arr[~0]) # d
print(arr[~1]) # c
The typical usages for ~ are like "swap mirror node" or "find median in a sort list":
"""swap mirror node"""
def reverse(arr: List[int]) -> None:
for i in range(len(arr) // 2):
arr[i], arr[~i] = arr[~i], arr[i]
"""find median in a sort list"""
def median(arr: List[float]) -> float:
mid = len(arr) // 2
return (arr[mid] + arr[~mid]) / 2
"""deal with mirror pairs"""
# verify the number is strobogrammatic, strobogrammatic number looks the same when rotated 180 degrees
def is_strobogrammatic(num: str) -> bool:
return all(num[i] + num[~i] in '696 00 11 88' for i in range(len(num) // 2 + 1))
~ actually is a math trick of inverse code and complement code, and it is more easy to understand in some situations.
Discussion about whether should use python tricks like ~:
In my opinion, if it is a code maintained by yourself, you can use any trick to avoid potential bug or achieve goal easier, because of maybe a high readability and usability. But in team work, avoid using 'too clever' code, may bring troubles to your co-workers.
For example, here is one concise code from Stefan Pochmann to solve this problem. I learned a lot from his code. But some are just for fun, too hackish to use.
# a strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down)
# find all strobogrammatic numbers that are of length = n
def findStrobogrammatic(self, n):
nums = n % 2 * list('018') or ['']
while n > 1:
n -= 2
# n < 2 is so genius here
nums = [a + num + b for a, b in '00 11 88 69 96'.split()[n < 2:] for num in nums]
return nums
I have summarized python tricks like this, in case you are interested.
list[-1]
Is short hand for:
list[len(list)-1]
The len(list) part is implicit. That's why the -1 is the last element. That goes for any negative index - the subtraction from len(list) is always implicit
If you really want to do this, you can create a class that wraps a list, and implement __getitem__ and __setitem__ to be one based. For example:
def __getitem__(self, index):
return self.list[index-1]
def __setitem__(self, index, value):
self.list[index-1] = value
However, to get the complete range of flexibility of Python lists, you would have to implement support for slicing, deleting, etc. If you just want a simple view of the list, one item at a time, this should do it for you.'
See Python Documentation for Data Model for more information on creating custom classes that act like sequences or mappings.
+1 to Max. Here's something else you could try:
Just add a None at index 0 of your list. This should get you the functionality. You'll just need to remember to snip out the leading None when you pass your list off to MATLAB
Just insert a 0 at the beginning of the structure:
(0, 2, 4, 6, 8, ...)
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