Nei*_*o_O 3 python string list startswith
我有一个这样的列表:
['a b d', 'a b e', 'c d j', 'w x y', 'w x z', 'w x k']
Run Code Online (Sandbox Code Playgroud)
我想删除在以与它相同的4个字符开头的字符串之后出现的所有字符串.例如,'a b e'将被删除,因为'a b d'它发生在它之前.
新列表应如下所示:
['a b d', 'c d j', 'w x y']
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
(注意:根据@Martijn Pieters的评论对列表进行排序)
使用生成器函数来记住启动:
def remove_starts(lst):
seen = []
for elem in lst:
if elem.startswith(tuple(seen)):
continue
yield elem
seen.append(elem[:4])
Run Code Online (Sandbox Code Playgroud)
因此,该函数会跳过以其中一个字符串开头的任何内容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']
Run Code Online (Sandbox Code Playgroud)
如果您的输入已排序,则可以简化为:
def remove_starts(lst):
seen = ()
for elem in lst:
if elem.startswith(seen):
continue
yield elem
seen = elem[:4]
Run Code Online (Sandbox Code Playgroud)
这通过限制到最后一个来节省前缀测试.