【发布时间】:2016-07-05 17:10:28
【问题描述】:
我需要找到用户输入的句子中单词的位置,如果单词出现不止一次,则只打印第一次出现的单词
到目前为止我有代码-
sentence=input("Enter a sentence: ")
sentence=sentence.lower()
words=sentence.split()
place=[]
for c,a in enumerate(words):
if words.count(a)>2 :
place.append(words.index(a+1))
else:
place.append(c+1)
print(sentence)
print(place)
但它会打印句子中单个单词的位置,而不是重复出现多次的单词的原始位置
谁能帮我解决这个问题???
【问题讨论】:
-
你的问题不清楚;也许一两个例子会有所帮助。
标签: python