2017-01-30 54 views
-1

我想知道如何将存储在记事本中的数据文件中的文本重新创建回Python。例如[“the”,“man”,“in”,“house”] [1,2,3,4],所以“the”= 1等。我只打印出这个句子,而不是指定的数字到它。如何从数据文件重新创建文本到Python

+0

*重新创建python * ...这种“操作”的结果应该是什么? – RomanPerekhrest

+0

请向我们展示一个数据文件的代表性样本,以便我们向您展示如何阅读它。另外,你到目前为止尝试过什么? – inspectorG4dget

+0

你必须更清楚。举一个这样的文本文件的例子,以及“重新创建”的含义。 – Gabriel

回答

1

你必须从单词列表遍历索引列表,访问i-1日(指数从0开始)值。为了将指标转换回一句,你可以使用str.join发电机表达:为什么使用列表理解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' 

要知道,检查:List comprehension vs generator expression's weird timeit results?

+0

嗨,你能进一步解释“””。加入([WORD_LIST [I-1]我在word_index])”。 –

相关问题