![](/img/trans.png)
[英]Check if a string contains all words of another string in the same order python?
[英]Python check if string contains all words in Python
我想檢查是否在沒有任何循環或迭代的另一個字符串中找到了所有單詞:
a = ['god', 'this', 'a']
sentence = "this is a god damn sentence in python"
all(a in sentence)
應該返回TRUE
。
您可以根據實際需要使用一組,如下所示:
a = ['god', 'this', 'a']
sentence = "this is a god damn sentence in python"
print set(a) <= set(sentence.split())
這將顯示True
,其中<=
is issubset
。
它應該是:
all(x in sentence for x in a)
要么:
>>> chk = list(filter(lambda x: x not in sentence, a)) #Python3, for Python2 no need to convert to list
[] #Will return empty if all words from a are in sentence
>>> if not chk:
print('All words are in sentence')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.