【问题标题】:Selecting arrays in python under conditions在条件下在python中选择数组
【发布时间】:2018-03-19 10:33:57
【问题描述】:

我有这个数组:

[[0, 1, 0, 1, 0, 1],
 [0, 0, 0, 1, 0, 0],
 [0, 0, 0, 0, 1, 0],
 [1, 0, 1, 0, 1, 0],
 [0, 1, 1, 1, 0, 1],
 [0, 1, 0, 0, 1, 1],
 [1, 1, 1, 0, 0, 0],
 [1, 1, 1, 1, 0, 1],
 [0, 1, 1, 0, 1, 0],
 [1, 1, 0, 0, 0, 1],
 [1, 0, 0, 0, 1, 0]]

我希望创建一个新数组,即只有第 1 列中为 0 的行。

如何在不自己编写函数的情况下在 python 中构建这样的数组。我尝试了太复杂的东西,我只需要简单的选择方法就能得到​​结果。

@EDIT 我忘了提到我正在使用 numpy.array([])

【问题讨论】:

    标签: python arrays python-3.x matrix


    【解决方案1】:

    既然你说你用的是numpy,那么这里是numpy.where的好地方:

    import numpy as np
    
    a = np.array([[0, 1, 0, 1, 0, 1], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [1, 0, 1, 0, 1, 0], [0, 1, 1, 1, 0, 1], [0, 1, 0, 0, 1, 1], [1, 1, 1, 0, 0, 0], [1, 1, 1, 1, 0, 1], [0, 1, 1, 0, 1, 0], [1, 1, 0, 0, 0, 1], [1, 0, 0, 0, 1, 0]])
    a_new = a[np.where(a[:,1] == 0)]
    print(a_new)
    # array([[0, 0, 0, 1, 0, 0],
    #        [0, 0, 0, 0, 1, 0],
    #        [1, 0, 1, 0, 1, 0],
    #        [1, 0, 0, 0, 1, 0]])
    

    【讨论】:

    • 这正是我所需要的。我尝试了选择、搜索、查找。这是“哪里”的方法。谢谢。
    【解决方案2】:

    你可以这样做:

    a = [[0, 1, 0, 1, 0, 1],[0, 0, 0, 1, 0, 0],[0, 0, 0, 0, 1, 0],[1, 0, 1, 0, 1, 0],[0, 1, 1, 1, 0, 1],[0, 1, 0, 0, 1, 1],[1, 1, 1, 0, 0, 0],[1, 1, 1, 1, 0, 1],[0, 1, 1, 0, 1, 0],[1, 1, 0, 0, 0, 1],[1, 0, 0, 0, 1, 0]]
    
    b = [l for l in a if len(l) > 1 and l[1] == 0]
    print(b)
    

    输出将是:

    [[0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [1, 0, 1, 0, 1, 0], [1, 0, 0, 0, 1, 0]]
    

    【讨论】:

    • 这个答案检查数组的长度,这是一个很好的做法。
    【解决方案3】:

    您可以使用list comprehensions

    list = [[0, 1, 0, 1, 0, 1],[0, 0, 0, 1, 0, 0],[0, 0, 0, 0, 1, 0],[1, 0, 1, 0, 1, 0],[0, 1, 1, 1, 0, 1],[0, 1, 0, 0, 1, 1],[1, 1, 1, 0, 0, 0],[1, 1, 1, 1, 0, 1],[0, 1, 1, 0, 1, 0],[1, 1, 0, 0, 0, 1],[1, 0, 0, 0, 1, 0]]
    list = [item for item in list if item[1] == 0]
    

    输出:

    [[0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [1, 0, 1, 0, 1, 0], [1, 0, 0, 0, 1, 0]]
    

    如果您使用numpy 数组,第一步是使用tolist 方法将您的numpy 数组转换为列表。

    import numpy
    array = numpy.array([[0, 1, 0, 1, 0, 1],[0, 0, 0, 1, 0, 0],[0, 0, 0, 0, 1, 0],[1, 0, 1, 0, 1, 0],[0, 1, 1, 1, 0, 1],[0, 1, 0, 0, 1, 1],[1, 1, 1, 0, 0, 0],[1, 1, 1, 1, 0, 1],[0, 1, 1, 0, 1, 0],[1, 1, 0, 0, 0, 1],[1, 0, 0, 0, 1, 0]])
    list = [item for item in array.tolist() if item[1] == 0]
    array = numpy.array(list)
    

    【讨论】:

    • 是的,我正是需要这个。但我实际上忘了提到我正在使用 numpy.array([])。所以它是一个对象,所以我不能做这个操作。抱歉,我在 python 中不准确。
    • 在 OP 的情况下(numpy)似乎不是最有效的事情。我建议改用np.where
    【解决方案4】:

    你应该使用list comp

    l = [[0, 1, 0, 1, 0, 1],[0, 0, 0, 1, 0, 0],[0, 0, 0, 0, 1, 0],[1, 0, 1, 0, 1, 0],[0, 1, 1, 1, 0, 1],[0, 1, 0, 0, 1, 1],[1, 1, 1, 0, 0, 0],[1, 1, 1, 1, 0, 1],[0, 1, 1, 0, 1, 0],[1, 1, 0, 0, 0, 1],[1, 0, 0, 0, 1, 0]]
    l1 = [item for item in l if item[1] == 0]
    

    【讨论】:

      【解决方案5】:

      试试这个:

      new_list = []
      for i in list:
          has_zero = i[1]
          if has_zero==0:
              new_list.append(i)
      print(new_list)
      

      【讨论】:

        【解决方案6】:

        这可以通过其他答案中建议的列表理解以及filter 来完成,这在您的情况下可能在语义上更清晰:

        >>> a = [[0, 1, 0, 1, 0, 1],
        ...  [0, 0, 0, 1, 0, 0],
        ...  [0, 0, 0, 0, 1, 0],
        ...  [1, 0, 1, 0, 1, 0],
        ...  [0, 1, 1, 1, 0, 1],
        ...  [0, 1, 0, 0, 1, 1],
        ...  [1, 1, 1, 0, 0, 0],
        ...  [1, 1, 1, 1, 0, 1],
        ...  [0, 1, 1, 0, 1, 0],
        ...  [1, 1, 0, 0, 0, 1],
        ...  [1, 0, 0, 0, 1, 0]]
        >>> filter(lambda row: row[1] == 0, a)
        [[0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0], [1, 0, 1, 0, 1, 0], [1, 0, 0, 0, 1, 0]]
        

        【讨论】:

        • OP 正在使用一个 numpy 数组
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-03
        • 2021-10-26
        • 1970-01-01
        • 2017-09-18
        • 2015-03-29
        • 2016-10-12
        相关资源
        最近更新 更多