Python代码-不确定文本文件

Python代码-不确定文本文件,python,Python,我需要python代码方面的帮助。我一直试图将输入的句子保存到文本文件中,而不让它重复文件中的单词。我不知道怎么做 感谢您的帮助 这是我的代码: import sys #user-friendly, informs the user what do to answer = input("What is your name?\n") print("Hello " + answer + " and welcome to this program!\n") print("This program w

我需要python代码方面的帮助。我一直试图将输入的句子保存到文本文件中,而不让它重复文件中的单词。我不知道怎么做

感谢您的帮助

这是我的代码:

import sys

#user-friendly, informs the user what do to
answer = input("What is your name?\n")
print("Hello " + answer + " and welcome to this program!\n")
print("This program will ask you for a sentence and print out the positions of the words instead of the actual words. it will then save it in a file with the sentence.\n")

repeat = True
loop = True
true = True

#Allows the user to decide whether or not they want to use the program
while repeat:
    answer2 = input("Do you want to do run this program?\n")
    #if the answer is 'no' then the program stops
    if answer2.lower() == "No" or answer2.lower() == "nah" or answer2.lower() == "no" or answer2.lower() == "n":
        print ("Okay then ... Bye.")
        sys.exit()
    #if the answer is 'yes' then the code continues
    elif answer2 == "Yes".lower() or answer2.lower() == "yeah" or answer2.lower() == "yes" or answer2.lower() == "y":
        print ("Okay then ... \n")
        while true:
            if loop == True:
                sentence = input("Please enter a sentence:\n").lower()
            #converts the sentence into a list
            s = sentence.split()
            #works out the positions of the words
            positions = [s.index(x)+1 for x in s]
            print(positions)

            #opens a text file
            fi = open("CA Task 2.txt", "w")
            #Allows you to write over  the original content in the file
            fi.write(str(s))
            #it closes the file once you've finished with it
            fi.close()

            #opens a text file
            fi = open("CA Task 2.txt", "a")
            #Allows you to add to the text file instead of writing over it
            fi.write("\n")
            fi.write(str(positions))
            #it closes the file once you've finished with it
            fi.close()
            sys.exit()

    #if the answer is neither 'yes' nor 'no' then the programs jumps to this part and allows the user to try again
    else:
        print("Please enter a valid answer! Try again!\n")
假设输入的句子是“不要问你的国家能为你做什么,而要问你能为你的国家做什么”

应该说: 不要问你的国家能为你做些什么,而要问你能为你的国家做些什么

[1,2,3,4,5,6,7,8,9,10,3,9,6,7,8,4,5]

这样做有效,然后必须将其保存到文本文件中: [‘问’、‘不’、‘什么’、‘你的’、‘国家’、‘能’、‘做’、‘为了’、‘你’、‘但是’、‘什么’、‘你’、‘能’、‘做’、‘为了’、‘你的’、‘国家’]

[1,2,3,4,5,6,7,8,9,10,3,9,6,7,8,4,5]

这很好,但我想让它做的是,如果文本文件中已经提到过一次,就不要重复这个词。

这:

for word in s:
    if word not in s:
        s.append(word)
对我来说没有意义。您是否正在尝试创建一个独特单词的列表?它给出了相同的列表

同样
如果answer2.lower()=“No”
是多余的,因为结果永远不会是“No”

比如说,你有一个句子列表,其中有些单词是唯一的,有些则不是:
s=['foo','bar','foo','foo','spam']
如果你想得到这些独特单词的数字表示,你可以这样得到:

d = []
i = 0
for item in s:
    if item not in s[:i]:
        d.append(i)
        i += 1
    else:
        d.append(s.index(item))
现在,您将得到一个列表,其中每个数字都是s中单词的唯一表示形式:

[0, 1, 0, 0, 2]
这:

对我来说没有意义。您是否正在尝试创建一个独特单词的列表?它给出了相同的列表

同样
如果answer2.lower()=“No”
是多余的,因为结果永远不会是“No”

比如说,你有一个句子列表,其中有些单词是唯一的,有些则不是:
s=['foo','bar','foo','foo','spam']
如果你想得到这些独特单词的数字表示,你可以这样得到:

d = []
i = 0
for item in s:
    if item not in s[:i]:
        d.append(i)
        i += 1
    else:
        d.append(s.index(item))
现在,您将得到一个列表,其中每个数字都是s中单词的唯一表示形式:

[0, 1, 0, 0, 2]

所以是for循环部分没有正常工作?在我看来,您正在将一个句子拆分为一个列表,因此
['here'、'is'、'my'、'句子'、'input']
,然后循环遍历这些单词中的每一个,如果它们还不在列表中,则将它们追加到列表中。因此,这永远不会对
s
产生任何影响

Python有一个
set
集合,它保存唯一的值。因此,它就像一个
列表
,但不允许添加重复项。您可以使用它而不是for循环,因为您可以使用
列表初始化
集合
——就像您通过
split()
调用创建的一个用法一样

s = sentence.split()
s = set(s)

编辑:集合不像
列表那样保留顺序。因此,如果按照第一次出现的顺序保存单词很重要,那么这种方法将不起作用。

那么是for循环部分不起作用了?在我看来,您正在将一个句子拆分为一个列表,因此
['here'、'is'、'my'、'句子'、'input']
,然后循环遍历这些单词中的每一个,如果它们还不在列表中,则将它们追加到列表中。因此,这永远不会对
s
产生任何影响

Python有一个
set
集合,它保存唯一的值。因此,它就像一个
列表
,但不允许添加重复项。您可以使用它而不是for循环,因为您可以使用
列表初始化
集合
——就像您通过
split()
调用创建的一个用法一样

s = sentence.split()
s = set(s)

编辑:集合不像
列表那样保留顺序。因此,如果按第一次出现的顺序保存单词很重要,那么此方法将不起作用。

更改检查单词是否在s中的部分。您应该将您的单词保存在另一个列表中,并检查s单词是否已在另一个列表中。如下面的代码所示:

#for loop makes sure that if the word is in the list then it wont print it out again
    new_s = []
    for word in s:
       if word not in new_s:
           new_s.append(word)

更改检查单词是否在s中的部分。您应该将您的单词保存在另一个列表中,并检查s单词是否已在另一个列表中。如下面的代码所示:

#for loop makes sure that if the word is in the list then it wont print it out again
    new_s = []
    for word in s:
       if word not in new_s:
           new_s.append(word)

有一个内置函数名为:
set


有一个内置函数名为:
set


不太清楚你在这里需要什么帮助。您期望的是什么?也许用户输入的示例以及文件中保存的内容会很方便。基本上,我希望用户输入一个句子(这是可行的),然后我想把它保存在一个带有位置的文本文件中(这也是可行的)。但是,如果句子中的一个词被重复,我不知道该怎么办,你怎么把它从文本文件中排除@freebie@AsifKhan,你能在你的问题中添加你期望的答案吗?它可以是图像或文本,这将有助于理解你的问题。我已经改变了它@RuhulAmin@AsifKhan,现在见我的答案。我不太清楚你在这里需要什么帮助。您期望的是什么?也许用户输入的示例以及文件中保存的内容会很方便。基本上,我希望用户输入一个句子(这是可行的),然后我想把它保存在一个带有位置的文本文件中(这也是可行的)。但是,如果句子中的一个词被重复,我不知道该怎么办,你怎么把它从文本文件中排除@freebie@AsifKhan,你能在你的问题中添加你期望的答案吗?它可以是图像或文本,这将有助于理解你的问题。我已经改变了它@RuhulAmin@AsifKhan,现在看我的答案