如果它们是另一个列表中的超字符串,如何从列表中创建(或删除)项目
How to create (or remove) items from a list if they are superstrings in another list
这里使用Python。我有两个列表,如下所示:
a = ["table_a", "table_b", "table_c", "xyz", "abc"]
b = ["x", "c"]
我想要导出一个输出列表:
c = ["table_a", "table_b"]
如果在a
的元素中找到b
的元素,我不要
几乎所有的列表理解方法都是只向前的——这意味着如果 a 中的元素在 b 中,则删除(或保留)它。但我想要相反的东西。有任何想法吗?谢谢!
尝试列表理解 + all()
:
a = ["table_a", "table_b", "table_c", "xyz", "abc"]
b = ["x", "c"]
c = [v for v in a if all(l not in v for l in b)]
print(c)
打印:
['table_a', 'table_b']
这里使用Python。我有两个列表,如下所示:
a = ["table_a", "table_b", "table_c", "xyz", "abc"]
b = ["x", "c"]
我想要导出一个输出列表:
c = ["table_a", "table_b"]
如果在a
的元素中找到b
的元素,我不要
几乎所有的列表理解方法都是只向前的——这意味着如果 a 中的元素在 b 中,则删除(或保留)它。但我想要相反的东西。有任何想法吗?谢谢!
尝试列表理解 + all()
:
a = ["table_a", "table_b", "table_c", "xyz", "abc"]
b = ["x", "c"]
c = [v for v in a if all(l not in v for l in b)]
print(c)
打印:
['table_a', 'table_b']