使用 python 搜索应出现在另一个字符串中单词开头的单词
search word which should present at start of words in another string using python
我正在尝试搜索另一个字符串中的单词列表。
我使用下面的代码
>>> word='abc present'
>>> each='abctotech present'
>>> (all(x in each.split() for x in word.split()))
False
在上面我得到的结果是 "False" 但 "abc" 单词是字符串中一个单词的初始部分,每个单词是 "abctotech"
对于上述情况,我可以使用另一种方法 return "True" 值。
请指教。
您可以检查 each
中 strings
的 any
以 words
中的开始,例如,
>>> each
'abctotech present'
>>> word
'abc present'
>>> all(any(y.startswith(x) for y in each.split()) for x in word.split())
True
>>> each1
'toabctotech present'
>>> all(any(y.startswith(x) for y in each1.split()) for x in word.split())
False
我正在尝试搜索另一个字符串中的单词列表。 我使用下面的代码
>>> word='abc present'
>>> each='abctotech present'
>>> (all(x in each.split() for x in word.split()))
False
在上面我得到的结果是 "False" 但 "abc" 单词是字符串中一个单词的初始部分,每个单词是 "abctotech"
对于上述情况,我可以使用另一种方法 return "True" 值。 请指教。
您可以检查 each
中 strings
的 any
以 words
中的开始,例如,
>>> each
'abctotech present'
>>> word
'abc present'
>>> all(any(y.startswith(x) for y in each.split()) for x in word.split())
True
>>> each1
'toabctotech present'
>>> all(any(y.startswith(x) for y in each1.split()) for x in word.split())
False