我的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]
这很好但我想要它做的是如果在文本文件中已经提到过一次,就不要重复这个词。
答案 0 :(得分:3)
内置了一个名为set
https://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")'
答案 1 :(得分:2)
所以这是你的for循环部分没有表现?当我将一个句子分成一个列表['here', 'is', 'my', 'sentence', 'input']
然后循环遍历每个单词并将它们追回到列表中时(如果它们尚未包含在内)中。所以这对s
永远不会有任何影响。
Python有一个set
集合,它包含唯一值。所以它就像list
但不允许你添加重复项。您可以使用此代替for for循环,因为您可以使用set
初始化list
- 就像用户通过split()
调用创建的那样。
s = sentence.split()
s = set(s)
编辑:集合不像list
那样保留顺序。因此,如果按照首次出现的顺序保存单词很重要,那么此方法将无效。
答案 2 :(得分:0)
此:
for word in s:
if word not in s:
s.append(word)
对我没有意义。您是否尝试创建一个独特单词列表?它给你相同的清单。
同样if 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]
答案 3 :(得分:0)
更改检查单词是否在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)