到目前为止,我已经得到了这个
UserSen = input("Enter a sentence of your choice, ")
position = {} # word -> position
words = UserSen.split()
for word in words:
if word not in position: # new word
position[word] = len(position) + 1 # store its position
如果你把句子放进去的话,我想把它做成类似的东西"你好,你好吗?它会输出1,2,3,4,1,3,4,1
答案 0 :(得分:0)
非常简单,您需要在目前找到的位置中保存一个列表:
# UserSen = "HI HOW ARE YOU HI ARE YOU HI"
UserSen = input("Enter a sentence of your choice, ")
# word -> positionDictionary
positionDictionary = {}
positionList = []
words = UserSen.split()
for word in words:
# print( word )
# new word
if word not in positionDictionary:
# store its positionDictionary
positionDictionary[word] = len(positionDictionary) + 1
positionList.append( positionDictionary[word] )
# print( positionDictionary )
# print( positionList )
print( positionList )