【问题标题】:How to check and use the dtype of a numpy array in a condition IF statement using Python 2.7如何使用 Python 2.7 在条件 IF 语句中检查和使用 numpy 数组的 dtype
【发布时间】:2019-12-31 18:32:44
【问题描述】:

我在 ArcGIS 中有一个数据表,我将某些属性字段转换为 numpy 数组。由于数组的 dtype 可以是 f8 (float64) 或 i4 (int32),我想使用一个 if 语句来检查 dtype 然后做一些事情。

import arcpy
import numpy as np
sorted_data = arcpy.da.FeatureClassToNumPyArray(feature_class, (volume_field))
sorted_data.dtype

这给出了:

dtype(['OIL_RECOVERABLE_VOL', '<i4)])

我想做的是使用 if 语句读取 dtype,类似于:

if sorted_data.dtype == '<i4':
    'do something'
else: # If dtype = <f8
    'do something else'

【问题讨论】:

    标签: arrays python-2.7 numpy dtype


    【解决方案1】:

    您可以将复合 dtype 分开:

    In [299]: dt=np.dtype([('OIL_RECOVERABLE_VOL', '<i4')])                                                      
    In [300]: dt.descr                                                                                           
    Out[300]: [('OIL_RECOVERABLE_VOL', '<i4')]
    In [301]: dt.descr[0]                                                                                        
    Out[301]: ('OIL_RECOVERABLE_VOL', '<i4')
    In [302]: dt.descr[0][1]                                                                                     
    Out[302]: '<i4'
    

    In [304]: dt['OIL_RECOVERABLE_VOL']                                                                          
    Out[304]: dtype('int32')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-22
      • 1970-01-01
      相关资源
      最近更新 更多