假设我有这个代码
text=['Whatsapp app open']
def check(i,text):
if i!=-1:
return i
res=[]
for t in text:
i=t.find('app')
f=check(i,t)
res.append(t[0:f])
print(res)
我希望输出是Whatsapp
,而不仅仅是Whats
。有没有任何函数可以将整个字符串作为一个整体,并返回索引,以便我可以打印Whatsapp
而不是Whats
?
您应该将文本拆分为单词列表,并使用for
-循环检查if "app" in word:
sentences = ['Whatsapp app open', 'Instagram app open']
for text in sentences:
res = []
for word in text.split(' '):
#if 'app' in word.lower():
if 'app' in word:
res.append(word)
print(text, res)
结果:
Whatsapp app open ['Whatsapp', 'app']
Instagram app open ['app']
与列表理解相同
for text in sentences:
res = [word for word in text.split(' ') if 'app' in word]
print(text, res)
或者您应该使用regex
import re
sentences = ['Whatsapp app open', 'Instagram app open']
for text in sentences:
res = re.findall('[^ ]*app[^ ]*', text)
print(text, res)
对于更复杂的东西,您可能需要Natural Language Processing
,即模块nltk