I've learned the theory in a computer science class about multi dimensional arrays & records of arrays but I am unsure of how to implement this in Python. I was thinking of using an array of records as it would allow me to have different data types in the one record but at the same time I was thinking that a multi dimensional array would also be good to learn in Python.
I've Googled around a bit & I have found numpy but I'm not sure which function in numpy I'd use. I saw one with tuples but I wasn't keen on using that as tuples can't be modified.
I'm using Python 3.2 but I don't mind using Python 2.7 either.
Depends how you want to interact with them. Consider using Numpy. Specifically, have a look at the N-Dimensional array. It provides a rich API for manipulation and comparison.
If numpy feels like overkill, you can implement a multi-array using simple tuples/lists.
multi_array = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10]
]
>>> multi_array[0][0]
>>> multi_array[1][4]
As an example, to convert multi_array
into a numpy ndarray
:
import numpy as np
np.ndarray((2, 5), buffer=np.array(multi_array), dtype=int)
Notice that ndarray expects as it's first argument a shape
. This means you need to know the dimensions of the array before you create it. Changing the dimensions of the ndarray is known as 'reshaping'.