簡體   English   中英

當找到n個或更少的連續值時替換

[英]Replace when n or less consecutive values are found

這也許是一個非常簡單的問題

我有一個看起來像這樣的列表:

a=[0,1,1,2,3,2,1,2,0,3,4,1,1,1,1,0,0,0,0,4,5,1,1,1,3,2,0,2,1,1,3,4,1]

我正在努力尋找一個簡單的python代碼,該代碼將在n個或更少的連續1被發現為0時替換,並使用新值創建一個新列表

因此,如果

n = 2

B = [0,0,0,2,3,2,0,2,0,3,4,1,1,1,1,0,0,0,0,4,5,1,1,1 ,3,2,0,2,0,0,3,4-,0]

如果

n = 3

B = [0,0,0,2,3,2,0,2,0,3,4,1,1,1,1,0,0,0,0,4,5,0,0,0 ,3,2,0,2,0,0,3,4-,0]

我在每個示例中都強調了新的替換值

您可以嘗試以下方法:

import itertools
a=[0,1,1,2,3,2,1,2,0,3,4,1,1,1,1,0,0,0,0,4,5,1,1,1,3,2,0,2,1,1,3,4,1]
n = 3
new_list = list(itertools.chain(*[[0]*len(b) if a == 1 and len(b) <= n else b for a, b in [(c, list(d)) for c, d in itertools.groupby(a)]]))

輸出:

[0, 0, 0, 2, 3, 2, 0, 2, 0, 3, 4, 1, 1, 1, 1, 0, 0, 0, 0, 4, 5, 0, 0, 0, 3, 2, 0, 2, 0, 0, 3, 4, 0]

“一個”內襯,使用一些itertools

from itertools import groupby, chain

a=[0,1,1,2,3,2,1,2,0,3,4,1,1,1,1,0,0,0,0,4,5,1,1,1,3,2,0,2,1,1,3,4,1]

list(
    chain.from_iterable(
        ([0] * len(lst) if x == 1 and len(lst) <= n else lst 
         for x, lst in ((k, list(g)) for k, g in groupby(a)))
    )
)
# [0,0,0,2,3,2,0,2,0,3,4,1,1,1,1,0,0,0, 0,4,5,1,1,1,3,2,0,2,0,0,3,4,0]

groupby將初始列表分組為相同對象的組。 它的輸出是對(k, g)的迭代器,其中k是作為分組鍵的元素,而g是在組中生成實際元素的迭代器。

由於您不能在迭代器上調用len ,因此將列出組並鏈接結果列表,但長度為1的列表除外。 這些將替換為長度相同的0列表。

一步(使用中間列表而不是生成器):

grouped_lists_by_key = [k, list(g)) for k, g in groupby(a)]
# [(0, [0]), (1, [1, 1]), ...]

grouped_lists = [[0] * len(lst) if x == 1 and len(lst) <= n else lst for x, lst in grouped]
# [[0], [0, 0], [2], [3], ...]

flattened = chain.from_iterable(grouped_lists)
# [0, 0, 0, 2, 3, ...]

使用itertools.groupby()非一體式:

a = [0,1,1,2,3,2,1,2,0,3,4,1,1,1,1,0,0,0,0,4,5,1,1,1,3,2,0,2,1,1,3,4,1]
n = 2
b = []
for k, g in groupby(a):
    l = list(g)
    if k == 1 and len(l) <= n:
        b.extend([0]*len(l))
    else:
        b.extend(l)
print(b)

嘗試這個:

def replacer(array, n):
    i, consec = 0, 0
    while i < len(array):
        if array[i] == 1:
            consec += 1
        else:
            if consec >= n:
                for x in range(i-consec, i):
                    array[x] = 0
            consec = 0
        i += 1
    return array

比其他人更長,但可以說很簡單:

a = [0,1,1,2,3,2,1,2,0,3,4,1,1,1,1,0,0,0,0,4,5,1,1,1,3,2,0,2,1,1,3,4,1]

def suppress_consecutive_generator(consecutive=2, toreplace=1, replacement=0):
    def gen(l):
        length = len(l)
        i = 0
        while i < length:
            if l[i] != toreplace:
                yield l[i]
                i += 1
                continue
            j = i
            count = 0
            while j < length:
                if l[j] != toreplace:
                    break
                count += 1
                j += 1
            i += count
            if count <= consecutive:
                for _ in range(count):
                    yield replacement
            else:
                for _ in range(count):
                    yield toreplace
    return gen

print(list(suppress_consecutive_generator()(a)))

暫無
暫無

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

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