2017-02-02 6 views
-1

私はすでにこれを開始しました。私が使用した方法にかかわらず1つの単語の位置。私は研究して、これまで何も見つけていない、助けてください。あなたが最後に余分なインデックス位置に追加することができます私は単語を入れて、あなたが作った文章中の単語の位置を見つけることができるPythonスクリプトを作成する必要があります。

print ("Enter your sentence here.") 
#This will ensure that the person knows to input a sentence. 
sentence = input() 
#This will allow the person to input a sentence. 
s = sentence.split() 
#This will allow me to split the sentence. 
positions = [s.index(x)+1 for x in s] 
#This will change the first position from 0 to 1. 
print ("Which word would you like to find in this sentence?") 
#This will allow the person to write a word in their sentence. 
word = input() 
#This allows the person to input the word. 
print (positions) 
+0

http://stackoverflow.com/questions/21842885/python-find-a-substring-in-a-stringを複製します-and-returning-the-substringのインデックス –

+1

[Python:文字列内の単語を見つける]可能な複製(http://stackoverflow.com/questions/24103522/python-find-a-word-within -文字列) – jkalden

答えて

1
print ("Enter your sentence here.") 
#This will ensure that the person knows to input a sentence. 
sentence = input() 
#This will allow the person to input a sentence. 
s = sentence.split() 
#This will allow me to split the sentence. 
print ("Which word would you like to find in this sentence?") 
#This will allow the person to write a word in their sentence. 
word = input() 
#This allows the person to input the word. 
print(s.index(word) + 1) 

0

また、文章中の単語とその位置を印刷するのに列挙使用することができます。

print "enter sentence" 
sentence=raw_input() 
print "enter word" 
word=raw_input() 
s=sentence.split() 
for index, w in enumerate(s): 
    if w==word: 
     print w, index 
関連する問題