【发布时间】:2017-01-26 23:01:23
【问题描述】:
我正在尝试获取用户输入的单词的位置,但是如果单词在句子中只出现一次,我只能找到位置,如果输入的单词在句子中出现多次,我如何找到位置句子?
if varInput in varwords:
print ("Found word")
#prints total number of words
print("The total number of words is: " + str(len(varwords)))#http://stackoverflow.com/questions/8272358/how-do-i-calculate-the-number-of-times-a-word-occurs-in-a-sentence-python
#finds out how many times the word occurs in the sentence
wordOcc = (varwords.count(varInput))
#if word occurence = 1 then print the position of the word
if wordOcc ==1:
pos = varwords.index(varInput)
pos = pos+1
print("the word "+varInput+" appears at position:")
print(pos)
else if wordOcc =>2:
pos1 = varwords.index(varInput)
【问题讨论】:
标签: python