我需要程序的帮助来识别句子中的单个单词,将它们存储在列表中,并将原始句子中的每个单词替换为该单词在名单。这是我到目前为止。列表中的单个词然后打印这些词的位置
例如:
'ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR' COUNTRY
将重建为1,2,3,4,5,6,7,8,9,1,3,9,6,7,8,4,5
from collections import OrderedDict
sentence = input("Please input a sentence without punctuation").upper()
punctuation = ("`1234567890-=¬!£$%^&*()_+\|[];'#,./{}:@~<>?")
FilteredSentence = ("")
for char in sentence:
if char not in punctuation:
FilteredSentence = FilteredSentence+char
FilteredSentence = FilteredSentence.split(" ")
refined = list(OrderedDict.fromkeys(FilteredSentence))
我已成功地识别,但是我的工作如何替换原始列表与的个别单词的位置的话列表中的各个单词。
你的意思是第一个外观指数字? – Obsidian
我认为你需要清单上的'.index'方法。 'help([]。index)' –