Python中最长的运行/岛数

时间:2016-06-30 20:17:42

标签: arrays python-2.7 numpy

我有一个0' s重复多次的数组,但我想找到最长的0。例如:

myArray= [[1,0],[2,0],[3,0][4,0][5,1][6,0][7,0][8,0][9,0][10,0][11,0][12,0][13,1][14,2][15,0][16,0][17,0][18,0][19,1][20,0]]

所以Y坐标有零连续重复几次,但我需要找到最长的运行。在这种情况下,最长的运行是从X坐标6到X坐标12的7个重复的零。我喜欢程序告诉我最长的零运行在哪里(在这种情况下从X = 6到X = 12) )。

谢谢!

2 个答案:

答案 0 :(得分:0)

将零视为我们追求的零。因此,我们将尝试将它们分配为布尔数组中的True元素。这可以通过myArray[:,1]==0来实现。接下来,让我们找到0s这些区域的上升和下降边缘。我们可能0s触及数组的极限/边界,因此在这些情况下,我们可能会错过这样的上升和下降边缘。因此,为了覆盖这些边界情况,我们可以在两侧填充False's0's,然后分别查找正和负微分值。对应于上升沿和下降沿的索引必须分别是0s间隔的开始和停止,最后这些索引的最大值将是所需的输出。

我们有两个版本来实现下面列出的这个想法。

def max_interval_len_app1(myArray):
    # Construct a grouped differentiated array where 1s indicate start and -1 
    # as stop indices. Then  store such indices.
    grp_diffs = np.diff(np.hstack(([0],(myArray[:,1]==0).astype(int),[0])))
    grp_start_idx = np.where(grp_diffs==1)[0]
    grp_stop_idx = np.where(grp_diffs==-1)[0]

    if len(grp_start_idx)==0:
        return 0 # No zeros found
    else:    
        # Get 0's interval lens by subtracting start from each correspondin
        # stop indices. Return the max length 
        return (grp_stop_idx - grp_start_idx).max()

def max_interval_len_app2(myArray):
    # Get indices at which rising and falling edges occur.
    idx = np.where(np.diff(np.hstack(([False],myArray[:,1]==0,[False]))))[0]

    if len(idx)==0:
        return 0 # No zeros found
    else:        
        # Since the rising and falling edges happen in pairs we are guaranteed
        # to have an even sized idx . So reshape into Nx2 array and then do 
        # differentiation along the columns, which would be 0s interval lens. 
        # Max of the lengths should be the desired output.
        return np.diff(idx.reshape(-1,2),axis=1).max()

答案 1 :(得分:0)

您可以从for循环中的连续元素开始编号,然后只需获取最大值的索引。

>>> cv, a = [], 0
>>> for x in myArray:
>>>     if x[1] == 0:
>>>         a += 1
>>>     else:
>>>         a = 0
>>>     cv.append(a)
>>> print(a)
[1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7, 0, 0, 1, 2, 3, 4, 0, 1]

>>> mi = cv.index(max(cv))  # finds index of *first* maximum
>>> run = [mi - cv[mi] + 1, mi]
>>> print(run)
[5, 11]