10

How can I make an array start at subscript 1 instead of subscript 0 in python?

Basically to solve this problem in python.

9
  • What's an array? What are you actually trying to do? Write python code to translate matlab to excel? Commented Jul 30, 2012 at 17:58
  • Just subtract 1 from the index whenever you access it? Either that or you could code your own list class that does it for you. Commented Jul 30, 2012 at 17:59
  • 1
    I'm not sure how this is going to solve an interoperability problem between Excel and MATLAB. I suppose it's theoretically possible to subclass list and override stuff... 5 minutes messing round with ipython should help. However how that helps MATLAB/Excel is unclear. Do you really need to write a file conversion script? In that case why not just use the VBA code in the page you linked to? If you do really need to write a python conversion script implementing a 1-based array is not really the right approach. Commented Jul 30, 2012 at 18:00
  • I am writing code to put a Nx1 matrix into a matlab workspace that was started using the COM. So the array will hold the Nx1 matrix numbers. Commented Jul 30, 2012 at 18:01
  • @Wooble an array is a collection of items in which the items are of equal size, and thus their index can be calculated in roughly O(1) time. Contrast with Linked-list Commented Jul 30, 2012 at 18:01

4 Answers 4

15

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.

Sign up to request clarification or add additional context in comments.

Comments

7

+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

Comments

7

You can use enumerate() method

a = ['a', 'c', 'v', 's']
for i,v in enumerate(a, 1):
    print i, v

1 a
2 c
3 v
4 s

1 Comment

Actually, enumerate is a class in module builtins, just to be picky.
1

You can use custom indexes of the fidx module:

import fidx

# create custom type `list1b` from `list`
fidx.add(list, name='list1b')

# add a map for int indexes (i -> i-1)
fidx.list1b.set_index_map(int, lambda _,i: i-1, override=True)

# create your list
b = fidx.list1b([1,2,3])

print(b[1], b[2], b[3]) # (1, 2, 3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.