Custom data type in a matrix.

Diez B. Roggisch deets at nospam.web.de
Sun Apr 23 05:46:29 EDT 2006


Gaz schrieb:
> Hi guys. I've been lookig for this in the numpy pdf manual, in this
> group and on google, but i could not get an answer...
> 
> Is there a way to create a custom data type (eg: Name: string(30), Age:
> int(2), married: boolean, etc) and then use that custom data in a
> matrix? Actually, this is a two question question :P
> 
> Im doing a simple hex based game and i need a way to store every hex
> property (color, owner,x, y, etc) in a matrix's "cell", representing
> each cell a hex.

You don't want numpy - you want ordinary lists in lists. Consider this:

class Hex(object):
     def __init__(self, x, y):
         self.x, self.y = x, y
         self.terrain_type = "unknown"


map = [[Hex(x, y) for y in xrange(height)] for x in xrange(width)]


There is no advantage of using numpy whatsoever - your fields aren't 
subject to mathematical operations or other transformations/selections.

Diez



More information about the Python-list mailing list