删除列表中的某些连续重复项 [英] Remove certain consecutive duplicates in list

查看:29
本文介绍了删除列表中的某些连续重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串列表:

I have a list of strings like this:

['**', 'foo', '*', 'bar', 'bar', '**', '**', 'baz']

我想用一个'**'替换'**','**',但保留'bar','bar'完好无损.IE.将任何连续数量的'**'替换为一个.我当前的代码如下:

I want to replace the '**', '**' with a single '**', but leave 'bar', 'bar' intact. I.e. replace any consecutive number of '**' with a single one. My current code looks like this:

p = ['**', 'foo', '*', 'bar', 'bar', '**', '**', 'baz']
np = [p[0]]
for pi in range(1,len(p)):
  if p[pi] == '**' and np[-1] == '**':
    continue
  np.append(p[pi])

还有其他pythonic方式可以做到这一点吗?

Is there any more pythonic way to do this?

推荐答案

不确定pythonic,但这应该有效并且更简洁:

Not sure about pythonic, but this should work and is more terse:

star_list = ['**', 'foo', '*', 'bar', 'bar', '**', '**', 'baz']
star_list = [i for i, next_i in zip(star_list, star_list[1:] + [None]) 
             if (i, next_i) != ('**', '**')]

上面复制了两次列表;如果要避免这种情况,请考虑Tom Zych的方法.或者,您可以执行以下操作:

The above copies the list twice; if you want to avoid that, consider Tom Zych's method. Or, you could do as follows:

from itertools import islice, izip, chain

star_list = ['**', 'foo', '*', 'bar', 'bar', '**', '**', 'baz']
sl_shift = chain(islice(star_list, 1, None), [None])
star_list = [i for i, next_i in izip(star_list, sl_shift) 
             if (i, next_i) != ('**', '**')]

可以使用 tools 文档中的 pairwise 配方的变体对此进行通用化并使其易于迭代-更不用说更具可读性了:

This can be generalized and made iterator-friendly -- not to mention more readable -- using a variation on the pairwise recipe from the itertools docs:

from itertools import islice, izip, chain, tee
def compress(seq, x):
    seq, shift = tee(seq)
    shift = chain(islice(shift, 1, None), (object(),))
    return (i for i, j in izip(seq, shift) if (i, j) != (x, x))

已测试:

>>> list(compress(star_list, '**'))
['**', 'foo', '*', 'bar', 'bar', '**', 'baz']

这篇关于删除列表中的某些连续重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆