代码之家  ›  专栏  ›  技术社区  ›  shahaf

python中按功能分组的紧凑1-0数组

  •  0
  • shahaf  · 技术社区  · 6 年前

    我有一个输入 1 , 0 阵列, 我想去掉零并将连续的 1个 s、 就像

    输入: [0, 0, 1, 1, 0, 1, 0, 1, 0, 0] 输出: [2,1,1]

    我写了一个基本的函数(不要介意糟糕的代码,只是一个草稿)

    def foo(arr):
        z_flag = False
        s = 0
        a = []
        for i in arr:
            if i != 0:
                s += 1
            else:
                z_flag = True
            if z_flag:
                z_flag = False
                if s > 0:
                    a.append(s)
                s = 0
        if s > 0:
            a.append(s)
    
        return a
    

    我想做得更快,最好用 numpy 内部功能

    如有任何建议,将不胜感激:)

    2 回复  |  直到 6 年前
        1
  •  1
  •   Divakar    6 年前

    a 作为输入数组/列表,我们可以-

    # Compare against 1 to get a mask. Append on either sides with False
    # so that when do consecutive comparison next, we will catch the
    # transitions including leading and trailing islands that might be 
    # starting at the first element of the array or ending as the last one. 
    # These transitions are signal the start and end of each island of 1s.
    m = np.r_[False,np.asarray(a)==1,False]
    idx = np.flatnonzero(m[:-1]!=m[1:])
    
    # After catching those start,end indices, simply subtract between start
    # and end indices to get island lengths. That's our o/p.
    out = idx[1::2]-idx[::2]
    

    如果 已经是一个数组,我们也可以使用 a.astype(bool) 代替 np.asarray(a)==1 .

    样本运行-

    In [81]: a
    Out[81]: [0, 0, 1, 1, 0, 1, 0, 1, 0, 0]
    
    In [82]: m = np.r_[False,np.asarray(a)==1,False]
        ...: idx = np.flatnonzero(m[:-1]!=m[1:])
        ...: out = idx[1::2]-idx[::2]
    
    In [83]: out
    Out[83]: array([2, 1, 1])
    
        2
  •  0
  •   CumminUp07    6 年前

    这里有一个解决方案,我还没有和你的时间对上,但可能更接近你想要的

    a = [0, 0, 1, 1, 0, 1, 0, 1, 0, 0]
    # Convert the array to a string of 1s and 0s
    a = ''.join([str(x) for x in a])
    # Split on the 0s to 'remove them'
    a = a.split('0')
    # Count the length of the arrays that are greater than 0
    b = [len(x) for x in a if len(x) > 0]
    print(b)
    

    输出: [2, 1, 1]