Python 找到n个或更少的连续值时替换
这也许是一个非常简单的问题 我有一个如下列表:Python 找到n个或更少的连续值时替换,python,replace,Python,Replace,这也许是一个非常简单的问题 我有一个如下列表: 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或不太连续的1s为0时,该代码将被替换,并用新值创建一个新列表 所以如果 b=[0,0,0,2,3,2,0,2,0,3,4,1,1,1,0,0,0,0,4,5,1,1,1,1,3,2,0,2,0,0,3,4,0] 如果 n=3 b=[0,0,0,2,3,2,0
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或不太连续的1s为0时,该代码将被替换,并用新值创建一个新列表
所以如果
b=[0,0,0,2,3,2,0,2,0,3,4,1,1,1,0,0,0,0,4,5,1,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,0,0,0,0,4,5,0,0,0,0,3,2,0,2,0,0,3,4,0]
我已经在每个示例中突出显示了新的替换值尝试以下操作:
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
def替换器(阵列,n):
i、 conce=0,0
而iimport 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)]]))
使用以下各项的非oneliner:
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,1,3,2,0,2,1,1,3,4,1]
n=2
b=[]
对于groupby(a)中的k,g:
l=列表(g)
如果k==1且len(l)比其他值长,但可以说是简单的:
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)))
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,1,3,2,0,2,1,1,3,4,1]
def抑制连续发生器(连续=2,更换=1,更换=0):
def发电机(l):
长度=长度(l)
i=0
而我<长度:
如果我[我]!=替代:
收益率l[i]
i+=1
持续
j=i
计数=0
而j<长度:
如果l[j]!=替代:
打破
计数+=1
j+=1
i+=计数
如果算上,那太好了!非常感谢!你能解释一下吗?我补充了一些解释。
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, ...]
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]
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)
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)))