删除包含多个条件的列表条目
Remove list entries containing multiple conditions
我有字符串形式的路径列表
>>> p = ['/path/one/foo.txt', '/path/two/foo.txt', '/path/three/foo.txt', '/path/four/foo.txt
]
我知道我可以使用类似
的方式从列表中删除包含单词的条目
>>> p = [x for x in p if 'two' not in x]
>>> p
['/path/one/foo.txt', '/path/three/foo.txt', '/path/four/foo.txt']
然而,这似乎不起作用
>>> p = [x for x in p if 'two' or 'three' not in x]
>>> p
['/path/one/foo.txt', '/path/two/foo.txt', '/path/three/foo.txt', '/path/four/foo.txt
]`
如何删除 p
中包含 two
或 three
的所有条目?
请注意,我从字典中的不同键中提取值 two
和 three
,因此创建单个列表可能并不简单。
您可以将 all
与生成器表达式一起使用:
values = ('two', 'three')
p = [x for x in p if all(i not in x for i in values)]
更好的办法是提取特定文件夹并将其与 set
进行比较。您的示例很简单,因为您只对第二个目录感兴趣:
values = {'two', 'three'}
L = ['/path/one/foo.txt', '/path/two/foo.txt',
'/path/three/foo.txt', '/path/four/foo.txt']
res = [i for i in L if i.split('/')[2] not in values]
print(res)
['/path/one/foo.txt', '/path/four/foo.txt']
p = [x for x in p if 'two' not in x and 'three' not in x]
在 Python 中,您有两个独立的布尔语句:'two' in x
和 'three' in x
。您使用的语法不起作用,因为 Python 无法将该语法识别为两个单独的布尔语句。
使用正则表达式。 --> re.search(r"(two|three)", x)
演示:
import re
p = ['/path/one/foo.txt', '/path/two/foo.txt', '/path/three/foo.txt', '/path/four/foo.txt']
p = [x for x in p if not re.search(r"(two|three)", x)]
print(p)
输出:
['/path/one/foo.txt', '/path/four/foo.txt']
试试这个:
p = [x for x in p if 'two' not in x and 'three' not in x]
@afg1997,您也可以继续使用您自己的方法,只需将 or 替换为 and 并在您的代码中稍作修改,如下所示。
>>> p = ['/path/one/foo.txt', '/path/two/foo.txt', '/path/three/foo.txt', '/path/four/foo.txt']
>>>
>>> [x for x in p if not 'two' in x and not 'three' in x]
['/path/one/foo.txt', '/path/four/foo.txt']
>>>
我有字符串形式的路径列表
>>> p = ['/path/one/foo.txt', '/path/two/foo.txt', '/path/three/foo.txt', '/path/four/foo.txt
]
我知道我可以使用类似
的方式从列表中删除包含单词的条目>>> p = [x for x in p if 'two' not in x]
>>> p
['/path/one/foo.txt', '/path/three/foo.txt', '/path/four/foo.txt']
然而,这似乎不起作用
>>> p = [x for x in p if 'two' or 'three' not in x]
>>> p
['/path/one/foo.txt', '/path/two/foo.txt', '/path/three/foo.txt', '/path/four/foo.txt
]`
如何删除 p
中包含 two
或 three
的所有条目?
请注意,我从字典中的不同键中提取值 two
和 three
,因此创建单个列表可能并不简单。
您可以将 all
与生成器表达式一起使用:
values = ('two', 'three')
p = [x for x in p if all(i not in x for i in values)]
更好的办法是提取特定文件夹并将其与 set
进行比较。您的示例很简单,因为您只对第二个目录感兴趣:
values = {'two', 'three'}
L = ['/path/one/foo.txt', '/path/two/foo.txt',
'/path/three/foo.txt', '/path/four/foo.txt']
res = [i for i in L if i.split('/')[2] not in values]
print(res)
['/path/one/foo.txt', '/path/four/foo.txt']
p = [x for x in p if 'two' not in x and 'three' not in x]
在 Python 中,您有两个独立的布尔语句:'two' in x
和 'three' in x
。您使用的语法不起作用,因为 Python 无法将该语法识别为两个单独的布尔语句。
使用正则表达式。 --> re.search(r"(two|three)", x)
演示:
import re
p = ['/path/one/foo.txt', '/path/two/foo.txt', '/path/three/foo.txt', '/path/four/foo.txt']
p = [x for x in p if not re.search(r"(two|three)", x)]
print(p)
输出:
['/path/one/foo.txt', '/path/four/foo.txt']
试试这个:
p = [x for x in p if 'two' not in x and 'three' not in x]
@afg1997,您也可以继续使用您自己的方法,只需将 or 替换为 and 并在您的代码中稍作修改,如下所示。
>>> p = ['/path/one/foo.txt', '/path/two/foo.txt', '/path/three/foo.txt', '/path/four/foo.txt']
>>>
>>> [x for x in p if not 'two' in x and not 'three' in x]
['/path/one/foo.txt', '/path/four/foo.txt']
>>>