【问题标题】:looping through complicated nested dictionary遍历复杂的嵌套字典
【发布时间】:2019-11-25 16:01:59
【问题描述】:

我有一个相当复杂的字典列表,其中包含嵌套字典和数组。我正在尝试找出一种方法,

  1. 使数据列表不那么复杂,然后循环遍历 栅格点或,
  2. 找到一种按原样循环遍历栅格点数组的方法。

我最终要做的是遍历每个多边形内的所有栅格点,对分配给该栅格点的值执行简单的大于或小于(值是高程值)。如果大于给定值分配 1,如果小于给定值分配 0。然后我将创建一个由这些 1 和 0 组成的单独数组,然后我可以获得平均值。

我已经找到了所有这些点(pts 中的所有点),但它们至少在一个列表(所有多边形的)列表中的另一个字典中的字典中的数组中,我认为我在组织中可能是错误的,因为字典相当对我来说是新的。

以下是我的代码:

import numpy as np

def mystat(x):

    mystat = dict()
    mystat['allpoints'] = x
    return mystat

stats = zonal_stats('acp.shp','myGeoTIFF.tif')

pts = zonal_stats('acp.shp','myGeoTIFF.tif', add_stats={'mystat':mystat})

Link 到我的文件。任何帮助或方向将不胜感激!

【问题讨论】:

    标签: python dictionary


    【解决方案1】:

    我假设您使用的是rasterstats 包。你可以试试这样的:

    threshold_value = 15  # You may change this threshold value to yours
    
    for o_idx in range(0, len(pts)):
        data = pts[o_idx]['mystat']['allpoints'].data
        for d_idx in range(0, len(data)):
            for p_idx in range(0, len(data[d_idx])):
                # You may change the conditions below as you want
                if data[d_idx][p_idx] > threshold_value:
                    data[d_idx][p_idx] = 1
                elif data[d_idx][p_idx] <= threshold_value:
                    data[d_idx][p_idx] = 0;
    

    它将更新pts列表中的数据

    【讨论】:

      猜你喜欢
      • 2017-07-25
      • 2013-07-21
      • 1970-01-01
      相关资源
      最近更新 更多