Ich möchte in der Lage sein, alle Positionen zu identifizieren, an denen ein Wort in einem Satz erscheint.Finden Sie alle Vorkommen eines Wortes in einer Liste?
Zum Beispiel: Hallo mein Name ist Ben und sein Name ist Fred.
Wenn ich Eingabe ‚name‘ es zurückgeben sollte: Dieses Wort kommt in den Orten: 3 und 8
Unten ist mein Code aber es wird nur den ersten Wert zurück.
text = input('Please type your sentence: ')
sentence = text.split()
word= input('Thank-you, now type your word: ')
if word in sentence:
print ('This word occurs in the places:', sentence.index(word)+1)
elif word not in sentence:
print ('Sorry, '+word+' does not appear in the sentence.')
Dies ist ein Duplikat von http://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list –