我有字符串形式的路径列表
>>> 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,因此创建单个列表可能并不简单。
请您参考如下方法:
试试这个:
p = [x for x in p if 'two' not in x and 'three' not in x]
