I'm having trouble getting my head around structural arrays in numpy.
lets say i have
- two lists of tuples (to use native python types).
foo_listandbar_list. len(foo_list)==len(bar_list)The lists are the same length- for all i,j:
len(foo_list[i])==len(foo_list[j])andlen(bar_list[i])==len(bar_list[j])all the tuples in each list are the same length. But these lengths are not known til runtime (so I can't hard code them into a dtype string) - for all i,j:
len(foo_list[i])!=len(bar_list[j])The tuples in different list has different lengths
How do I zip these two together into as structure array?
It seems like specifying the dtype is going to involve a mass of string manipulation after i do things like examine the structure myself.
I did try this once it was not nice code, so i figure there must be a better way to do it.
Currently I am doing:
Currently my solution is to zip them and pass them to a numpy.asarray
, but that has weird consequences. It makes a 2D array of objects and those objects are arrays. If you slice it you end up with a array of arrays - not a 2D array.
Example data:
foo_list = [(0.0, 1.0, 1.0, 0.0, 1.0),
(1.0, 0.0, 1.0, 0.0, 1.0),
(1.0, 1.0, 1.0, 0.0, 0.0),
(0.0, 0.0, 0.0, 0.0, 1.0),
(0.0, 1.0, 1.0, 1.0, 0.0),
(1.0, 1.0, 1.0, 0.0, 1.0),
(0.0, 0.0, 0.0, 0.0, 0.0),
(0.0, 0.0, 0.0, 1.0, 0.0),
(1.0, 1.0, 1.0, 1.0, 0.0),
(1.0, 0.0, 0.0, 1.0, 0.0)]
bar_list = [(0.56885990540494535, 0.54212235514533669),
(-1.0024727291757354, 0.75636919036826),
(1.0912423038752346, 0.66209493674389353),
(0.52256034116805239, 0.36499434352207855),
(-1.6837689312941191, 0.90001803836488747),
(-3.1590090289110528, -0.3383410738003263),
(1.4080085734609102, -1.6283826051481185),
(1.5037872498731264, 1.5673560444854553),
(-2.271232989935922, 0.24542353558497185),
(-1.9752557923680221, 0.07968567723276497)]
foo_listandbar_listcan individually be made into arrays (size(10,5)and(10,2)). What's the reason for combining them into a structured array? It's not going to speed up anynumpycalculations. If you do combine them, what shape and dtype do you want it have?