快速而干净的查找Numpy数组索引的方法

2024-10-04 03:20:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我尽量说清楚。问题是:

我有两个数组,ELEM和LAMI。你知道吗

ELEM数组包含有限元模型的信息,其类型为:

ndtype= [('conn','i4',3),('sets','a12',2)] 

所以ELEM的典型数据是:

array([ ([47, 49, 36], ['web', 'gelcoat']),
([48, 30, 43], ['surf', 'balsa']),...])

现在,LAMI包含了层压序列的数据,可以用于ELEM的元素。拉米属于:

ndtype = [('name', 'a12', 1), ('type', 'a12', 1), ('cons', 'float64', 6)]

比如说

In[1]:LAMI
Out[1]:array([ ('udfrp', 'TRISOTROPIC', [37.0, 90.0, 4.0, 4.0, 0.28, 1860.0]),
           ('dbfrp', 'TRISOTROPIC', [10.0, 10.0, 8.0, 8.0, 0.3, 1830.0]),
           ('gelcoat', 'ISOTROPIC', [10.0, 0.3, 1830.0, 0.0, 0.0, 0.0]),
           ('nexus', 'ISOTROPIC', [1.0, 0.3, 1664.0, 0.0, 0.0, 0.0]),
       ('balsa', 'TRISOTROPIC', [10.0, 10.0, 2.0, 2.0, 0.3, 128.0])], 
      dtype=[('name', 'S12'), ('type', 'S12'), ('cons', '<f8', (6,))])

可以看出,ELEM['sets'][1]是元素的材质名称,元素的材质属性存储在LAMI['cons']中。你知道吗

问题是:哪种方法是找到元素的材料属性数组的最佳方法?我试过以下方法:

index = np.where(LAMI['name'][0] == ELEM['sets'][element,1])]
prop_array = LAMI['cons'][index]

但我肯定有更好的办法。谢谢!你知道吗


Tags: 数据方法name元素sets数组arraycons
2条回答

在这种情况下,可能首选dictionary

In [4]: LAMId = {x['name']: LAMI[['type','cons']][i] for i, x in enumerate(LAMI)}

In [5]: LAMId['udfrp']
Out[5]: ('TRISOTROPIC', [37.0, 90.0, 4.0, 4.0, 0.28, 1860.0])

In [6]: LAMId['udfrp']['cons']
Out[6]: 
array([  3.70000000e+01,   9.00000000e+01,   4.00000000e+00,
         4.00000000e+00,   2.80000000e-01,   1.86000000e+03])

将数组转换为字典将稍微简化访问

In [163]: Ldict={}
In [166]: for r in LAMI:
   .....:     Ldict[r['name']]={'type':r['type'],'cons':r['cons']}

In [176]: name=ELEM['sets'][0,1]

In [177]: LAMI[np.where(LAMI['name']==name)]['cons']
Out[177]: 
array([[  1.00000000e+01,   3.00000000e-01,   1.83000000e+03,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00]])

In [178]: Ldict[name]['cons']
Out[178]: 
array([  1.00000000e+01,   3.00000000e-01,   1.83000000e+03,
         0.00000000e+00,   0.00000000e+00,   0.00000000e+00])

另一种方法是定义一个Material类,因此每个material都是一个对象。ELEM数组可以有一个带有这些指针的object字段。你知道吗

你曾经把LAMI['cons']作为一个完整的数组使用过吗?如果不是的话,结构化数组格式可能就没那么有用了。你知道吗


使用材质对象的版本:

class Material(object):
    def __init__(self, name, _type, cons):
        self.name = name
        self.type = _type
        self.cons = cons.tolist() # more compact display
    def __repr__(self):
        return str((self.name, self.type, self.cons))
Ldict = {}
for r in LAMI:
    Ldict[r['name']] = Material(r['name'], r['type'], r['cons'])

dset = np.dtype([('where','a12'),('material','O')])
dtElem = np.dtype([('conn', '<i4', (3,)), ('sets', dset)])
# nested dtypes
# [('conn', '<i4', (3,)), ('sets', [('where', 'S12'), ('material', 'O')])]

ELEM = np.array([ ([47, 49, 36], ('web', Ldict['gelcoat'])),
                  ([48, 30, 43], ('surf', Ldict['balsa']))],
               dtype=dtElem)

print 'ELEM array'
print ELEM
print 'material properties of one element'
print ELEM[0]['sets']['material'].cons
print 'materials of all elements'
print ELEM['sets']['material']

生产:

ELEM array
[ ([47, 49, 36], ('web', ('gelcoat', 'ISOTROPIC', [10.0, 0.3, 1830.0, 0.0, 0.0, 0.0])))
 ([48, 30, 43], ('surf', ('balsa', 'TRISOTROPIC', [10.0, 10.0, 2.0, 2.0, 0.3, 128.0])))]

material properties of one element
[10.0, 0.3, 1830.0, 0.0, 0.0, 0.0]

materials of all elements
[('gelcoat', 'ISOTROPIC', [10.0, 0.3, 1830.0, 0.0, 0.0, 0.0])
 ('balsa', 'TRISOTROPIC', [10.0, 10.0, 2.0, 2.0, 0.3, 128.0])]

但要得到所有元素的cons列表,我必须使用理解:

print [m.cons for m in ELEM['sets']['material']]

相关问题 更多 >