【问题标题】:In a numpy record array how to access certain element value in python在一个numpy记录数组中如何访问python中的某些元素值
【发布时间】:2015-10-27 14:00:38
【问题描述】:

我有以下 numpy 记录数组。如何访问数组中特定元素的偏移量?换句话说,对于这个例子,我只想打印 array[1] 元素的偏移量。

伪例子

print(array[1,offset])

#I want the output to be 
02

实际代码

dtype={
    'names' : ('name','offset','bit_span'),
    'formats' : ('U20','U20','U20')}

instance= np.zeros(3,dtype)

array[0]=('register','00','0')
array[1]=('register_1','02','0')
array[2]=('register_2','04','0')

【问题讨论】:

    标签: python numpy record


    【解决方案1】:

    您可以按名称访问记录,例如

    dtype={
        'names' : ('name','offset','bit_span'),
        'formats' : ('U20','U20','U20')}
    
    instance= np.zeros(3,dtype)
    
    instance[0]=('register','00','0')
    instance[1]=('register_1','02','0')
    instance[2]=('register_2','04','0')
    
    instance[1]['offset']
    # '02'
    

    【讨论】:

      猜你喜欢
      • 2021-10-20
      • 2020-07-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-13
      相关资源
      最近更新 更多