我需要查找用户输入的句子中单词的位置,并且如果该单词出现多次,只会首次打印该单词出现的单词查找单词出现多次的句子中的原始位置
我有代码,以便远
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)
但它打印的句子中各个单词的位置,而不是重复出现不止一次
能任何一个字的原始位置帮我这个?
你的问题不清楚;也许一个或两个例子会有所帮助。 –