Python代码不确定文本文件

2024-09-21 01:03:21 发布

您现在位置: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 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]

这样做有效,然后必须将其保存到文本文件中: ['ask'、'not'、'what'、'your'、'country'、'can'、'do'、'for'、'you'、'but'、'what'、'you'、'can'、'do'、'for'、'your'、'country']

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

这是好的,但我要它做的是不要重复这个词,如果它已经提到过一次在文本文件。你知道吗


Tags: orthetoansweryouitprogramlower
3条回答

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

Python有一个set集合,其中包含唯一的值。所以它就像一个list但是不允许添加重复项。您可以使用这个来代替for循环,因为您可以用list初始化set,就像您从split()调用创建的一样。你知道吗

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

编辑:集合不像list那样保持顺序。因此,如果按第一次出现的顺序保存单词很重要,那么这种方法就行不通了。你知道吗

有一个内置函数叫做:sethttps://docs.python.org/3/library/stdtypes.html#set

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()
        # for loop makes sure that if the word is in the list then it wont print it out again
        for word in s:
            if word not in s:
                s.append(word)
        # works out the positions of the words
        positions = [s.index(x) + 1 for x in s]
        print(set(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(set(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(set(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")'

更改检查单词是否在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)

相关问题 更多 >

    热门问题