簡體   English   中英

如何從python中的列表中獲取零和非零值的索引范圍?

[英]how to get index range for zero and non-zero value from list in python?

我有一個包含零和非零值的列表。 我想根據列表中的元組找到零和非零值的范圍。 我正在尋找使用pythonic方式的無軟件包解決方案。 例如

a = [ 0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   5,  11,  12,
        12,  12,  13,  13,  17,  17,  17,  17,  17,  17,  17,  17,  17,
        25,  42,  54,  61,  61,  68,  73, 103, 115, 138, 147, 170, 187,
       192, 197, 201, 208, 210, 214, 216, 217, 217, 218, 219, 220, 221,
       222, 222, 219, 220, 220, 221, 220, 216, 216, 217, 217, 217, 217,
       216, 216, 216, 209, 204, 193, 185, 177, 161, 156, 143, 110, 103,
        89,  82,  62,  62,  62,  60,  56,  55,  50,  49,  48,  47,  47,
        45,  44,  43,  42,  40,  37,  23,  22,  14,  12,   6,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
         0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   5,
         6,   6,   6,   7,   7,   7,  13,  29,  31,  32,  33,  41,  42,
        43,  43,  44,  44,  44,  44,  44,  60,  70,  71,  72,  88,  95,
       104, 105, 111, 124, 125, 131, 145, 157, 169, 174, 186, 190, 190,
       191, 192, 192, 193, 193, 193, 194, 198, 201, 202, 203, 202, 203,
       203, 203, 203, 203, 203, 197, 195, 186, 177, 171, 154, 153, 148,
       141, 140, 135, 132, 120, 108,  94,  86,  78,  73,  60,  53,  46,
        46,  45,  44,  43,  37,  35,  29,  26,  19,  11,   0]]

輸出:idx = [(0,9),(10,101),(102,128),...]

在這里,我的建議是沒有外部軟件包,簡短,易讀且易於理解:

# Compute list "b" by replacing any non-zero value of "a" with 1
b = list(map(int,[i != 0 for i in a]))

#Compute ranges of 0 and ranges of 1
idx = []   # result list of tuples
ind = 0    # index of first element of each range of zeros or non-zeros
for n,i in enumerate(b):
    if (n+1 == len(b)) or (b[n] != b[n+1]):
        # Here: EITHER it is the last value of the list 
        #       OR a new range starts at index n+1
        idx.append((ind,n))
        ind = n+1

print(idx)

您可以enumerate()來獲取索引, itertools.groupby()可以將falsy( 0 )和真實值分組在一起,並使用operator.itemgetter(0, -1)提取開始和結束索引:

from operator import truth, itemgetter
from itertools import groupby

[itemgetter(0,-1)([i for i,v in g]) for _, g in groupby(enumerate(a), key = lambda x: truth(x[1]))]
# [(0, 9), (10, 101), (102, 128), (129, 217), (218, 252), (253, 338), (339, 362), (363, 447), (448, 490), (491, 580), (581, 581)]

這是沒有外部庫的答案:

pointer = 0
is_zero = True
result = []


def finder(p, is_z):
    while (a[p] == 0) is is_z:
        p += 1
        if p == len(a):
            return p
    return p


while pointer < len(a):
    tmp = finder(pointer, is_zero)
    result.append((pointer, tmp - 1))
    pointer = tmp
    is_zero = not is_zero
print(result)

無需導入(因此無需搜索庫文檔來確定導入的工作方式:-)並帶有注釋。

# results is the output list, each entry is a list of startindex,stopindex
results = []
# each time round the logical value is remembered in previouslvalue
previouslvalue = None
for i,value in enumerate(a):
    # get the logical value (==0/!=0) from value
    lvalue = True if value != 0 else False
    if previouslvalue is None or lvalue != previouslvalue:
        # this is either the first entry, or the lvalue has changed
        # either way, append a new entry to results, with the current index as start/finish of the run
        results.append([i,i])
    else:
        # same lvalue as previous, extend the last entry to include this index
        results[-1][1] = i
    # save the logical value for next time round the loop
    previouslvalue = lvalue
print results

輸出為:

[[0, 9], [10, 101], [102, 128], [129, 217], [218, 252], [253, 338], [339, 362], [363, 447], [448, 490], [491, 580], [581,581]]

要回應有關在結果列表中包含邏輯值的評論,這很容易:

# results is the output list, each entry is a list of startindex,stopindex
results = []
# each time round the 
previouslvalue = None
for i,value in enumerate(a):
    # get the logical value (==0/!=0) from value
    lvalue = True if value != 0 else False
    if previouslvalue is None or lvalue != previouslvalue:
        # this is either the first entry, or the lvalue has changed
        # either way, append a new entry to results, with the current index as start/finish of the run
        # include the logical value in the list of results
        results.append([i,i,lvalue])
    else:
        # same lvalue as previous, extend the last entry to include this index
        results[-1][1] = i
    # save the logical value for next time round the loop
    previouslvalue = lvalue
print results

現在的輸出是:

[[0, 9, False], [10, 101, True], [102, 128, False], [129, 217, True], [218, 252, False], [253, 338, True], [339, 362, False], [363, 447, True], [448, 490, False], [491, 580, True], [581, 581, False]]
import numpy as np 

unique, counts = np.unique(a, return_counts=True)
idx = tuple(zip(unique, counts))

我認為這對您有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM