You want to create a class which contains name and place fields.
class Baz():
"Stores name and place pairs"
def __init__(self, name, place):
self.name = name
self.place = place
Then you'd use a list of instances of that class.
my_foos = []
my_foos.append(Baz("foo", "Shop"))
my_foos.append(Baz("bar", "Home"))
See also: classes (from the Python tutorial).
Answer from Matt Ball on Stack OverflowYou want to create a class which contains name and place fields.
class Baz():
"Stores name and place pairs"
def __init__(self, name, place):
self.name = name
self.place = place
Then you'd use a list of instances of that class.
my_foos = []
my_foos.append(Baz("foo", "Shop"))
my_foos.append(Baz("bar", "Home"))
See also: classes (from the Python tutorial).
That's what named tuples are for.
http://docs.python.org/dev/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields
You can use numpy datatypes: http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html
>>> dt = np.dtype([('x', np.int32), ('y', np.int32), ('z', np.int32)])
>>> x = np.array([(1, 2, 3), (3, 2, 1)], dtype = dt)
>>> print x
[(1, 2, 3) (3, 2, 1)]
>>> print x['x'], x['y'], x['z']
[1 3] [2 2] [3 1]
>>> print x[0]['x']
1
Extending example to add some numpy/matlab indexing:
>>> x = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)], dtype = dt)
>>> print x[1:]['x']
[4 7]
You can notice that it omits the first element in the X axis (the 1)
EDIT to add some information about how to subclass using custom data type. Using the example in answer from a similar question https://stackoverflow.com/a/5154869/764322 and sightly modifying it:
>>> class Data(np.ndarray):
def __new__(cls, inputarr):
dt = np.dtype([('x', np.int32), ('y', np.int32), ('z', np.int32)])
obj = np.asarray(inputarr, dtype = dt).view(cls)
return obj
def remove_some(self, col, val):
return self[self[col] != val]
>>> a = Data([(1,2,3), (4,5,6), (7,8,9)])
>>> print a
[(1, 2, 3) (4, 5, 6) (7, 8, 9)]
>>> print a.remove_some('x', 4)
[(1, 2, 3) (7, 8, 9)]
I think what you want is numpy.empty:
>>> import numpy as np
>>> a = np.empty((2, 2), dtype=np.object_)
>>> a
array([[None, None],
[None, None]], dtype=object)
This creates an empty array with the specified shape (in this case 2x2) and dtype (in this case, generic objects).