我想知道如何在记事本中重新创建存储在数据文件中的文本,然后重新创建Python。例如[“the”,“man”,“in”,“house”] [1,2,3,1,4]所以“the”= 1等我想打印出来的句子而不是分配的数字它。
答案 0 :(得分:1)
您必须迭代索引列表并从单词列表中访问i-1
(索引从0开始)值。为了将索引转换回句子,您可以将str.join
与生成器表达式一起使用:
>>> word_list = ["the", "man", "in","house"]
>>> word_index = [1, 2, 3, 1, 4]
>>> ' '.join([word_list[i-1] for i in word_index])
'the man in the house'
要知道为什么使用列表理解比在str.join
中使用生成器表达式更好,请检查:List comprehension vs generator expression's weird timeit results?