从它的单词列表中重新创建句子(不重复)和位置

问题描述:

String = "ask not what your country can do for you ask" 
words = ["ask","not","what","your","country","can","do","for","you","ask"] 
positions = [1,2,3,4,5,6,7,8,9,1] 

我需要用单词和位置重新创建原始字符串。它不能引用回到原来的字符串从它的单词列表中重新创建句子(不重复)和位置

String = "ask not what your country can do for you ask" 
words = ["ask","not","what","your","country","can","do","for","you","ask"] 
positions = [1,2,3,4,5,6,7,8,9,1] 

for word in String(): 
    if not word in words: 
     words.append(word) 
    i = words.index(word) 
    positions.append(i) 

s = "" 
for i in positions: 
    s = s + words[i] + " " 

print(s) 

我可以重新创建判决,但我必须使用原始字符串

+0

它通过独特的词来重新创建和职位 – User999822

+0

你确定你的变量'单词'正确吗?这似乎不符合你说的话。此外,你试图解决这个问题的是什么? – DainDwarf

+0

欢迎来到Stack Overflow!所有发布的内容都是程序说明。但是,我们需要您[提问](// *.com/help/how-to-ask)。堆栈溢出不是免费的代码写入服务。请[编辑]您的帖子以包含我们可以回答的有效问题。提醒:请确保你知道[这里的主题是什么](// *.com/help/on-topic),要求我们为你编写程序,而且建议是无关紧要的。 –

这个答案:

String = "ask not what your country can do for you ask" 
words = String 
list_word = String.split() 
res=[] 
dict_word={} 
for i, j in enumerate(list_word): 
    if j in dict_word: 
     res.append(dict_word[j]) 
    else: 
     dict_word[j]=i 
     res.append(i) 
print words 
print res 


ask not what your country can do for you ask 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 0]