如果出现在字符串中,该函数将列出要返回的单词,并以“”分隔。但是,现在它们将按照出现在传递给函数的列表中的出现顺序返回。如何修改函数,以便按出现顺序返回字符串?
我发现的唯一类似的帖子是,该帖子返回python 2.x中的第一个单词及其单词: 抓取在字符串中找到的列表中的第一个单词。( Python )
def ifExiste(set):
count_tweet_adding = 0
tempvalue = []
value = ""
x=0
old_count = count_tweet_adding
for element in set:
if (word_tweet.find(element) >= 0):
tempvalue.append(element.strip())
count_tweet_adding +=1
value = tempvalue[0]
if (old_count == count_tweet_adding):
value = "NaN"
while x < len(tempvalue)-1:
x += 1
value = value + " " + tempvalue[x]
return value
编辑:这是我的方法:
我添加了一个循环来过滤字符串和我的单词列表中的单词,然后将此过滤后的列表与“蛮力”方法一起使用来逐个字母地检查我的字符串。我还添加了一个替换符号,以将我从字符串中拾取的单词取出,因此,如果它在字符串中出现两次,则可以捕获两次。
def ifExiste(text, input_list):
count_tweet_adding = 0
tempvalue = []
value = ""
old_count = count_tweet_adding
filtered_input_list = []
for word in input_list:
if word in text:
filtered_input_list.append(word)
for length in range(len(text)):
for word in filtered_input_list:
if word in text[:length+1]:
tempvalue.append(word)
text = text[:length+1].replace(word,'')+text[length+2:]
count_tweet_adding +=1
tempvalue = map(str.strip, tempvalue)
value = " ".join(tempvalue)
if (old_count == count_tweet_adding):
value = "NaN"
return value
忽然笑
吃鸡游戏
相关分类