python - 如何打印包含另一个列表中所有字符串的子列表?

标签 python

search_terms = ['word','word 1','word 2']

library = [['desk','chair','lamp'],['cow','word','horse','word 2','word 1']]

我只想打印库中包含 search_terms 中所有术语的所有列表。列表 search_terms 不会总是有相同数量的字符串...

感谢所有愿意帮助我的人!

最佳答案

使用set.issubset :

>>> {'word','word 1','word 2'}.issubset(['desk','chair','lamp'])
False
>>> {'word','word 1','word 2'}.issubset(['cow','word','horse','word 2','word 1'])
True

>>> search_terms = ['word','word 1','word 2']
>>> library = [['desk','chair','lamp'],['cow','word','horse','word 2','word 1']]
>>> terms = set(search_terms)
>>> [x for x in library if terms.issubset(x)]
[['cow', 'word', 'horse', 'word 2', 'word 1']]

关于python - 如何打印包含另一个列表中所有字符串的子列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20866443/

相关文章:

python - Pandas - 如何合并两个 DataFrame

python - 如何在matplotlib中向各个方向旋转图形

python - 我如何从 Maven 运行 python 代码,其中 python 可能在也可能不在路径中

python - 循环访问 BeautifulSoup 中的元素,但仅输出该元素的子元素

使用 key=lambda 的 Python 排序得到 TypeError

python - Pandas 迭代行并从另一列中删除一列中的字符串值

python - python 代码没有显示所需的输出,问题出在哪里?

python - matplotlib 散点图 : the more overlapping points the bigger the marker

python - Tensorflow:在 conv2D_transpose 的 output_shape 中使用 None

python - 在 python 列表中删除对象的实例