从列表中删除一些满足条件的元素

问题描述:

我有一个文件名(位置)的列表,我想要做的是我想从列表位置中删除每个元素。从列表中删除一些满足条件的元素

条件:如果文件名以排除列表中的任何字符串开头,则不要打印文件名。

locations = ['/data/mybackup/data/fil1', 
      '/data/mybackup/data/fil2', 
      '/data/mybackup/data/fil3', 
      '/data/mybackup/song/fil1', 
      '/data/mybackup/song/fil2', 
      '/data/mybackup/song/fil3', 
      '/data/archive/song/fil1', 
      '/data/archive/song/fil2', 
      '/data/archive/song/fil3', 
      '/data/archive/data/fil1', 
      '/local/archive/data/fil2', 
      '/local/archive/data/fil3', 
      '/ebboks/wordpress/fil1', 
      '/ebooks/wordpress/fil2', 
      '/ebooks/wordpress/fil3'] 

excludes = [ '/data/archive/', '/data' , '/ebooks/' ] 

for location in locations: 
    for exclude in excludes: 
    if not location.startswith(exclude): 
     print(location) 
    break  

结果:

/data/mybackup/data/fil1 
/data/mybackup/data/fil2 
/data/mybackup/data/fil3 
/data/mybackup/song/fil1 
/data/mybackup/song/fil2 
/data/mybackup/song/fil3 
/local/archive/data/fil2 
/local/archive/data/fil3 
/ebboks/wordpress/fil1 
/ebooks/wordpress/fil2 
/ebooks/wordpress/fil3  

我的结果还是有文件名以 '/数据'

什么是错我的代码?

+1

'str.startswith'需要一个元组...如果你使'excludes'一个元组,而不是一个列表,你可以做'如果不是location.startswith(不包括)'一气呵成,而不必担心订购/或重叠的子循环 –

+0

我想,这真的只是一个逻辑上的错误,而不是一个蟒蛇问题。 'excludes'内的内部循环将始终仅针对'excludes [0]'和'break'进行测试。 –

str.startswith接受的参数核对,所以你避免额外的循环来检查和关注tuple约排序比较,所以你可以使用:

exc = tuple(excludes) 
# Or start with: excludes = ('/data/archive/', '/data' , '/ebooks/') instead 
for location in locations: 
    if not location.startswith(exc): 
     print(location) 

它给你:

/local/archive/data/fil2 
/local/archive/data/fil3 
/ebboks/wordpress/fil1 
+0

感谢你的努力来解决这个问题。你的解决方案确实很好。 –

因为您首先检查/data/archive/;它让所有不以/data/archive/开头的条目基本上跳过对/data的检查。

你可以这样做:

>>> excludes = tuple(excludes) 
>>> filter(lambda x: not x.startswith(excludes), locations) 
['/local/archive/data/fil2', '/local/archive/data/fil3', '/ebboks/wordpress/fil1'] 

您有打印位置之前检查所有排除。

尝试修改此:

for location in locations: 
    for exclude in excludes: 
    if not location.startswith(exclude): 
     print(location) 
    break 

要:

def valid(location): 
    for exclude in excludes: 
    if location.startswith(exclude): 
     return False 
    return True 

for location in locations: 
    if valid(location): 
    print(location) 

对于location是,比方说,"/data/mybackup/data/fil1"exclude"/data/archive",该location变量不"/data/archive"启动。

由于您的excludes列表中有"/data"值,因此您无需再输入以"/data"开头的其他路径。所以如果你定义了excludes = ["/data", "/ebooks"]就没有问题了。

条件:做打印的文件名,如果它在 琴弦的任何的排除列表启动。

随着all()功能:

for location in locations: 
    if all(not location.startswith(e) for e in excludes): 
     print(location) 

输出:

/local/archive/data/fil2 
/local/archive/data/fil3 
/ebboks/wordpress/fil1 
+0

@FujiClado,欢迎您 – RomanPerekhrest

尝试列表解析:

>>> [location for location in locations \ 
if not location.startswith(tuple(excludes))] 

输出:

['/local/archive/data/fil2', '/local/archive/data/fil3', '/ebboks/wordpress/fil1'] 

filter与lambda表达式,如果你喜欢函数式编程中@mshsayem

的答案