【问题标题】:Python: How to add two large masked arrays and keep their values (i.e. mask + value = value)?Python:如何添加两个大的掩码数组并保留它们的值(即掩码 + 值 = 值)?
【发布时间】:2018-06-21 02:36:50
【问题描述】:

我想以这样的方式组合两个掩码数组

array_one      = [1,    mask,   mask]

array_two      = [mask, 2,      mask]

combined_array = [1,    2,      mask]

请注意,我有巨大的二维数组!

代码:

面具

SO  = np.ma.masked_where(tmask != 1, tmask)

SO[:,0:x] = np.ma.masked

SO[:,y:-1] = np.ma.masked

大西洋面具

Atl  = np.ma.masked_where(tmask != 2, tmask)

Atl  = Atl/Atl  # To have a value of 1

SO = np.ma.add(Atl,SO) ### This give mask + value = mask

我也尝试过类似的东西

Atl = set(Atl)

SO = set.union(Atl,SO)

【问题讨论】:

  • 请分享您的代码,并说明您的代码的哪一部分不起作用。
  • 请编辑问题而不是发布未制定的代码作为评论

标签: python arrays numpy add mask


【解决方案1】:

试试这个:

combined_array = list(set(array_one+array_two))

【讨论】:

    【解决方案2】:
    array_one = [1, None, None, 3]
    array_two = [None, 2, None, 4]
    
    
    def combine(primary_array, secondary_array):
        """
        Arrays must be of equal length, primary overwrites secondary
        :param primary_array: [1, None, None, 3]
        :param secondary_array: [None, 2, None, 4]
        :return: [1, 2, None, 3]
        """
        result = []
        for x, y in zip(primary_array, secondary_array):
            if x is None and y is None:
                result.append(None)
            elif isinstance(x, (int, float)):
                result.append(x)
            elif isinstance(y, (int, float)):
                result.append(y)
            else:
                # Ignore if both x and y != int or float
                pass
        return result
    
    
    print(combine(array_one, array_two))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-03
      • 1970-01-01
      • 2018-04-09
      • 2013-09-20
      • 2015-12-06
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多