简体   繁体   English

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

[英]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.

相关问题 打印包含另一个列表中所有字符串的子列表,而不必直接匹配 - print a sublist that contains all strings in another list, without necessarily being a direct match 如何检查列表是否包含来自另一个列表子列表的项目? [等候接听] - How to check if a list contains items from another lists sublist? [on hold] 如何删除嵌套列表中另一个子列表中的子列表? - How to remove a sublist in nested list that are in another sublist? 字符串列表转换为字符串子列表 - List of strings into sublist of strings 如果子列表中的字符串包含 substring,则删除子列表(所有子列表中的所有值都是字符串) - removing a sublist if a string in the sublist contains a substring (all values within all sublists are strings) 检查列表是否包含子列表 - Checking if a list contains a sublist 如何获取基于另一个列表的列表的子列表? - How to get a sublist of a list based on another list? 如何测试哪个子列表包含特定元素,然后打印该子列表中的元素 - How to test which sublist contains a specific element, then print elements from that Sublist python 字符串列表,替换所有不属于特定子列表的出现 - python list of strings, replace all occurences not belonging to a specific sublist 如果列表中的字符串包含另一个字符串 python,如何返回字符串列表 - How to return list of strings if a string in a list contains another string python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM