![](/img/trans.png)
[英]print a sublist that contains all strings in another list, without necessarily being a direct match
[英]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']]
我只是希望能夠在庫中打印包含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']]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.