Create a structured array in python
<p>I would like to create a dictionary in Python using numpy commands.   </p>

<p>First I tried to define the structure and then to populate the array according to a number/case selected by the user. When I try to request one of the cases I get the following error (for case 1):</p>

<pre><code>cannot copy sequence with size 3 to array axis with dimension 1
</code></pre>

<p>How can I fix my code in order to be able to store the data I want in my structure? Regardless of the case I select. </p>

<p>Here is my code:</p>

<pre><code># defining the structure
usgStruct = np.zeros(1,dtype = [("satNr",np.int),
                             ("satAzimuth", np.int),
                             ("satElevation", np.int),
                             ("scenarioEnv", np.str),
                             ("scenarioHead", np.int),
                             ("scenarioLen", np.int), 
                             ("speed", np.int)])

def case1(): 
   usgStruct["satNr"]        = 3
   usgStruct["satAzimuth"]   = [180, 200, 235]
   usgStruct["satElevation"] = [35, 25, 25]
   usgStruct["scenarioEnv"]  = ["S", "S", "S", "U", "U"]
   usgStruct["scenarioHead"] = [45, 280, 45, 120, 200]
   usgStruct["scenarioLen"]  = [2000, 500, 3000, 2000, 500]
   usgStruct["speed"]        = [15, 15, 15, 10, 10]
   return usgStruct


def case2(): 
   usgStruct["satNr"]          = 2
   usgStruct["satAzimuth"]     = [180, 225]
   usgStruct["satElevation"]   = [45, 30]
   usgStruct["scenarioEnv"]    = ["U", "U", "O", "O", "S", "S", "S"]
   usgStruct["scenarioHead"]   = [30, 65, 65, 80, 80, 60, 130]
   usgStruct["scenarioLen"]    = [300, 800, 2000, 1000, 700, 700, 300]
   usgStruct["speed"]          = [10, 10, 15, 15, 15, 15, 15]
   return usgStruct


def case3(): 
   usgStruct["satNr"]        = 2         
   usgStruct["satAzimuth"]   = [180, 225]
   usgStruct["satElevation"] = [35, 30]  
   usgStruct["scenarioEnv"]  = ['C', 'C', 'C', 'C', 'O']                 
   usgStruct["scenarioHead"] = [90, 45, 120, 70, 45]       
   usgStruct["scenarioLen"]  = [1500, 500, 300, 2000, 3000] 
   usgStruct["speed"]        = [15, 15, 15, 15, 20]
   return usgStruct

# set up a dictionary of actions

scenarioGenerator = {
   "1": case1,
   "2": case2,
   "3": case3}

runscenGen = raw_input("Please enter a number from 1 to 7\n ")
scenarioGenerator.get(runscenGen,case3)()                       #  specify a   default: case3       

 print usgStruct  
</code></pre>

<p>print the initial <code>usgStruct</code> array:</p>

<pre><code>In [329]: usgStruct
Out[329]: 
array([(0, 0, 0, '', 0, 0, 0)], 
      dtype=[('satNr', '&lt;i4'), ('satAzimuth', '&lt;i4'), ('satElevation', '&lt;i4'), ('scenarioEnv', '&lt;U'), ('scenarioHead', '&lt;i4'), ('scenarioLen', '&lt;i4'), ('speed', '&lt;i4')])
</code></pre>

<p>Its data is 6 numbers and one character ('U' on my py3).  That's all it can hold.  It can't hold lists.</p>

<p>Even if you defined it to be size (3,) </p>

<pre><code>In [331]: usgStruct
Out[331]: 
array([(0, 0, 0, '', 0, 0, 0), (0, 0, 0, '', 0, 0, 0),
       (0, 0, 0, '', 0, 0, 0)], 
      dtype=[('satNr', '&lt;i4'), ('satAzimuth', '&lt;i4'), ('satElevation', '&lt;i4'), ('scenarioEnv', '&lt;U'), ('scenarioHead', '&lt;i4'), ('scenarioLen', '&lt;i4'), ('speed', '&lt;i4')])
</code></pre>

<p>individual records are still this 7 element tuple. </p>

<p>You <code>case</code> data is entirely different.  Each case looks like a dictionary with list values.  Changing <code>case1</code> to produce and return a dictionary:</p>

<pre><code>In [334]: def case1():
     ...:    usgStruct={} 
     ...:    usgStruct["satNr"]        = 3
     ...:    usgStruct["satAzimuth"]   = [180, 200, 235]
     ...:    usgStruct["satElevation"] = [35, 25, 25]
     ...:    usgStruct["scenarioEnv"]  = ["S", "S", "S", "U", "U"]
     ...:    usgStruct["scenarioHead"] = [45, 280, 45, 120, 200]
     ...:    usgStruct["scenarioLen"]  = [2000, 500, 3000, 2000, 500]
     ...:    usgStruct["speed"]        = [15, 15, 15, 10, 10]
     ...:    return usgStruct
     ...: 
In [335]: case1()
Out[335]: 
{'satAzimuth': [180, 200, 235],
 'satElevation': [35, 25, 25],
 'satNr': 3,
 'scenarioEnv': ['S', 'S', 'S', 'U', 'U'],
 'scenarioHead': [45, 280, 45, 120, 200],
 'scenarioLen': [2000, 500, 3000, 2000, 500],
 'speed': [15, 15, 15, 10, 10]}
</code></pre>

<p>Now <code>scenarioGenerator</code> would be a dictionary of dictionaries.</p>

python arrays numpy structure