如何确定单词列表是否在字符串中彼此相邻?

问题描述 投票:-1回答:1

我有一个句子和一个子字符串列表。如何检查该句子中是否出现了确切的子字符串?

例如:

Credit card customers will be allowed interest-free installment plans for all school fee payments as well as grocery purchases with no processing fees for up to six months.

现在我有了一个单词列表

["credit card customers","school fee payment","six months"]. 

如何检查以上字符串中是否存在这些子字符串?

python string substring
1个回答
0
投票

这是您的意思吗?

string = 'Credit card customers will be allowed interest-free installment plans for all school fee payments as well as grocery purchases with no processing fees for up to six months.'
listing = ["credit card customers","school fee payment","six months"]

def adj_or_not(index1,index2):
    if listing[index1] + listing[index2] in string:
        return True
    else:
        return False

adj_or_not(0,1)

输出:

False
© www.soinside.com 2019 - 2024. All rights reserved.