I have a list of namedtuples which I would like to write to a numpy array. The tuples have attributes 'colors', a set of two colors, and 'number', an integer, and are of the form:
from collections import namedtuple
import numpy as np
NamedTuple = namedtuple('tuple',['colors','number'])
L = [NamedTuple({'violet', 'blue'}, 4),
NamedTuple({'orange', 'blue'}, 1),
NamedTuple({'green', 'blue'}, 3),
NamedTuple({'orange', 'red'}, 2)]
L
>>>[tuple(colors={'blue', 'violet'}, number=4)...]
L[3].colors
>>>{'orange', 'red'}
I would like to write from L, for example, a 2x2 array such that:
Array[1][1].colors
>>>{'orange', 'red'}
Doing
Array = numpy.array(L)
>>>[[{'blue', 'violet'} 4]
[{'blue', 'orange'} 1]
[{'blue', 'green'} 3]
[{'red', 'orange'} 2]
gives me an array of tuples, not namedtuples, which have 'no attribute 'colors''
Worse yet, if I try to reshape Array into a 2x2 I find out that each attribute of my namedtuples has been written as a different object in the array.
numpy.reshape(Array,(2,2))
>>>...error...
>>>'ValueError: cannot reshape array of size 8 into shape (2,2)'
I would have thought the above array was size 4?
How can I get an array of namedtuples without mutating the namedtuples, such that I can call for different attributes from each element in the array?
The reason I would like to use namedtuples as my data structure is so it is easy and readable to call each object's .color or .number attribute.
The reason I would like to use a numpy array rather than a standard nested list is because this array is going to be a dynamic object throughout the project which will often be searched through and changed, and I know how poor python's standard lists are for these things.
For context, I am ultimately trying to build a program which plays a card game of my own invention. The namedtuples represent the cards with their colors and numbers. The Array represents a tableau of cards which players can change and move around. These namedtuples are going to be shuffled around quite a bit and I don't want to have to worry about their data structures getting changed.