当前位置:  开发笔记 > 编程语言 > 正文

从列表python中的某个东西开始删除后面的字符串

如何解决《从列表python中的某个东西开始删除后面的字符串》经验,为你挑选了1个好方法。

我有一个这样的列表:

['a b d', 'a b e', 'c d j', 'w x y', 'w x z', 'w x k']

我想删除在以与它相同的4个字符开头的字符串之后出现的所有字符串.例如,'a b e'将被删除,因为'a b d'它发生在它之前.

新列表应如下所示:

['a b d', 'c d j', 'w x y']

我怎样才能做到这一点?

(注意:根据@Martijn Pieters的评论对列表进行排序)



1> Martijn Piet..:

使用生成器函数来记住启动:

def remove_starts(lst):
    seen = []
    for elem in lst:
        if elem.startswith(tuple(seen)):
            continue
        yield elem
        seen.append(elem[:4])

因此,该函数会跳过以其中一个字符串开头的任何内容seen,将其允许的任何内容的前4个字符添加到该集合中.

演示:

>>> lst = ['a b d', 'a b e', 'c d j', 'w x y', 'w x z', 'w x k']
>>> def remove_starts(lst):
...     seen = []
...     for elem in lst:
...         if elem.startswith(tuple(seen)):
...             continue
...         yield elem
...         seen.append(elem[:4])
...
>>> list(remove_starts(lst))
['a b d', 'c d j', 'w x y']

如果您的输入已排序,则可以简化为:

def remove_starts(lst):
    seen = ()
    for elem in lst:
        if elem.startswith(seen):
            continue
        yield elem
        seen = elem[:4]

这通过限制到最后一个来节省前缀测试.

推荐阅读
牛尾巴2010
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有