我试图使一些发现,其中一个字是在列表中,然后告诉你在哪儿。这是我到目前为止有:查找单词在列表
whenAppears = 0
when = []
i = 0
phrase = str(input("What sentence? "))
phrase = phrase.lower()
phrase = phrase.split()
print(phrase)
wordel = str(input("What single word would you like to find? "))
wordel = wordel.lower()
if wordel in phrase:
print ("That word is in the phrase, there are",phrase.count(wordel),wordel+"('s)""in the sentence")
for word in phrase:
whenAppears += 1
if wordel == phrase[i]:
when.append(whenAppears)
print ("The word",wordel,"is in the slot",when)
不管我把它说这个词是在插槽1和任何其他插槽,我不能想到任何方式来这个,请帮助:D
看看在['list.index'](https://docs.python.org/3/library/stdtypes.html #common-sequence-operations)方法来更有效地找到项目列表中项目的位置。 –