我想编写一个python函数来确定字符串列表中是否存在单词列表。如果没有,则返回该字符串中最长的单词列表。
例如,假设我有这句话:The boy was walking his big dog down the street.
我有一个单词列表[boy, was, his, dog, street, the, down]
。很明显,在这种情况下,boy, was
和dog, street, the down
都会在句子中连续出现。因此,我的函数应按照它们出现的顺序返回这些单词,因此:boy was
和dog down the street
。
有没有人知道如何以有效的方式做到这一点?
由于
评论编辑:
您只需要按顺序返回字符串中出现的单词集,并且也在列表中。当然,他们应该尽可能长。这就是为什么在示例中我返回dog down the street
,因为所有这些单词都在我的列表中,并且在字符串中也显示在彼此旁边。
答案 0 :(得分:-1)
我想出了怎么做:
def order(sentence, wordList):
s_list = sentence(' .', '').replace('. ', '').replace('.', '').split(' ')
returnSentence = ""
returnSentenceLen = 0
previousPos = 0
currentSentence = []
# iterate through all the words in the matched list and find the ones that are together
for i, word in enumerate(s_list):
# this word is in our list of words
if word in wordList:
currentSentence.append(word)
if i == 0:
previousPos = 0
else:
if (i - previousPos) == 1:
# this should now be our new sentence of continuous words
if (len(currentSentence) > returnSentenceLen):
returnSentence = ' '.join(word for word in currentSentence)
returnSentenceLen = len(currentSentence)
else:
currentSentence = []
currentSentence.append(word)
previousPos = i
return returnSentence
print(order('The boy was walking his dog down the street.', ['boy', 'was', 'dog', 'street', 'the', 'down']))