Python 3 - 用索引值

时间:2017-03-05 20:01:47

标签: string list python-3.x file

我正在编写一个程序,它将用户输入的句子作为字符串(str1Input),然后将该句子写入文件。将句子拆分为列表(str1)后,程序会识别唯一的单词并将其写入同一文件。然后我需要用它的索引值替换句子中的每个单词(str1Input)。通过阅读包含句子的文件

例如,如果我“我喜欢在python和代码中编码”这将是(str1Input)然后我将使用“str1Input.split()”将其更改为 {'i','像”, '在', '代码', '', '蟒', '和', '代码', '东西'} 即可。在找到唯一值后,我会得到: {'和','code','like','i','things','python','to','in'}。

我有一个问题,因为我不确定如何从文件中读取以用该单词的索引值替换句子中的每个单词。这是目前为止的代码:

str1Input = input("Please enter a sentence: ")

str1 = str1Input.split()

str1Write = open('str1.txt','w')
str1Write.write(str1Input)
str1Write.close()

print("The words in that sentence are: ",str1)

unique_words = set(str1)

print("Here are the unique words in that sentence: " ,unique_words)

如果有人能帮助我,我将不胜感激,谢谢!

0 个答案:

没有答案