简体   繁体   English

在python中创建结构化数组

[英]Create a structured array in python

I would like to create a dictionary in Python using numpy commands. 我想使用numpy命令在Python中创建字典。

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): 当我尝试请求其中一种情况时,出现以下错误(对于情况1):

cannot copy sequence with size 3 to array axis with dimension 1

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. 不管我选择哪种情况。

Here is my 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  

print the initial usgStruct array: 打印初始的usgStruct数组:

In [329]: usgStruct
Out[329]: 
array([(0, 0, 0, '', 0, 0, 0)], 
      dtype=[('satNr', '<i4'), ('satAzimuth', '<i4'), ('satElevation', '<i4'), ('scenarioEnv', '<U'), ('scenarioHead', '<i4'), ('scenarioLen', '<i4'), ('speed', '<i4')])

Its data is 6 numbers and one character ('U' on my py3). 它的数据是6个数字和一个字符(在我的py3中为“ U”)。 That's all it can hold. 这就是它所能容纳的全部。 It can't hold lists. 它不能容纳列表。

Even if you defined it to be size (3,) 即使您将其定义为大小(3,)

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', '<i4'), ('satAzimuth', '<i4'), ('satElevation', '<i4'), ('scenarioEnv', '<U'), ('scenarioHead', '<i4'), ('scenarioLen', '<i4'), ('speed', '<i4')])

individual records are still this 7 element tuple. 个人记录仍然是这7个元素元组。

You case data is entirely different. 您的case数据是完全不同的。 Each case looks like a dictionary with list values. 每个案例看起来像是带有列表值的字典。 Changing case1 to produce and return a dictionary: 更改case1以产生并返回字典:

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]}

Now scenarioGenerator would be a dictionary of dictionaries. 现在, scenarioGenerator将是字典的字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM