[英]How to print a sublist that contains all of the strings in another list?
search_terms = ['word','word 1','word 2']
library = [['desk','chair','lamp'],['cow','word','horse','word 2','word 1']]
I just want to be able to print all list(s) in library that contain ALL of the terms in search_terms. 我只是希望能够在库中打印包含search_terms中所有术语的所有列表。 the list search_terms will not always have the same number of strings...
列表search_terms并不总是具有相同数量的字符串...
Thanks for anyone who is willing to help me! 感谢愿意帮助我的人!
Use set.issubset
: 使用
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']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.