私は、入力された単語が文章のどこに現れるかを尋ねられました。私は大文字と小文字を区別しない方法を見つけ出し、どの単語を見つけるかを選択しましたが、単語の位置を印刷するのに問題があります。ここに私が持っているものは次のとおりです。文章中の特定の単語を見つける
enter code here
sntc = input("Please type in a sentence without punctuation:")
print("This is the sentence you input:",sntc)
wrd = input("Please enter a word you want to search for in the sentence:")
print("The word requested to be seacrhed for is:",wrd)
sntcLow = sntc.lower()
wrd1 = wrd.lower()
SenLst = []
SenLst.append(sntcLow)
print(SenLst)
if any(wrd1 in s for s in SenLst):
print("Search successful. The word '",wrd1,"' has been found in position(s):")
else:
print("Search unsuccessful. The word '",wrd1,"' was not found. Please try another word...")
スパニングインデックスの検索例を教えてください。 –
これを見つけると、単語の開始文字位置がわかります。単語がどの位置にあるのかを見たいと思います。例:「太った猫は太ったマウスを食べました」としたら、「太っている」という言葉が第2位と第6位に現れます。これをどうやってやりますか? –