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/