How can I delete all zeros except for x of them in every run of consecutive zeros within a list?

陌路散爱 提交于 2019-12-10 16:35:15

问题


For every run of x or more consecutive zeros in a list in Python, I would like to del all zeros in the run except for x of them. If x = 0, then delete all zeros.

I was thinking of a Python function that took a list, L, and a number, x, as inputs.

For example, let L = [7, 0, 12, 0, 0, 2, 0, 0, 0, 27, 10, 0, 0, 0, 0, 8].

  • If x = 0, then return L = [7, 12, 2, 27, 10, 8]
  • If x = 1, then return L = [7, 0, 12, 0, 2, 0, 27, 10, 0, 8]
  • If x = 2, then return L = [7, 0, 12, 0, 0, 2, 0, 0, 27, 10, 0, 0, 8]
  • If x = 3, then return L = [7, 0, 12, 0, 0, 2, 0, 0, 0, 27, 10, 0, 0, 0, 8]
  • If x = 4, then return L = [7, 0, 12, 0, 0, 2, 0, 0, 0, 27, 10, 0, 0, 0, 0, 8] (Same as original L)
  • If x >= 5, then return original L as there are no runs of 5 or more consecutive zeros.

Any help would be sincerely appreciated.


回答1:


This is easy to do as a generator. Wrap your call to it in a list constructor if you want a fresh list with the zero-runs removed.

def compact_zero_runs(iterable, max_zeros):
    zeros = 0
    for i in iterable:
        if i == 0:
            zeros += 1
            if zeros <= max_zeros:
                yield i
        else:
            zeros = 0
            yield i



回答2:


Using groupby:

def del_zeros(lst, n):
    lst = (list(j)[:n] if i else list(j) 
           for i,j in itertools.groupby(lst, key=lambda x:x==0))

    return [item for sublist in lst for item in sublist]

And the tests:

>>> [del_zeros(L, i) for i in range(5)]
[[7, 12, 2, 27, 10, 8],
 [7, 0, 12, 0, 2, 0, 27, 10, 0, 8],
 [7, 0, 12, 0, 0, 2, 0, 0, 27, 10, 0, 0, 8],
 [7, 0, 12, 0, 0, 2, 0, 0, 0, 27, 10, 0, 0, 0, 8],
 [7, 0, 12, 0, 0, 2, 0, 0, 0, 27, 10, 0, 0, 0, 0, 8]]



回答3:


from itertools import groupby, chain, islice
from functools import partial
from operator import eq

def f(L, x):
    groups = groupby(L, partial(eq, 0))
    return list(chain.from_iterable(islice(v, x) if k else v for k,v in groups))


来源:https://stackoverflow.com/questions/11732554/how-can-i-delete-all-zeros-except-for-x-of-them-in-every-run-of-consecutive-zero

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!