gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 02:32:00 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com