编写程序以检查用户输入的单词是否在预先存在的集合中;无论我输入什么单词,程序都会返回" False" (即使它是我知道的一个词在集合中)。代码如下:
name = input("which file would you like to open? ")
s=input("word? ")
F = open(name, "r")
words = set()
words.add(F)
def contains(s,words):
if s in words:
print("true")
else:
print("false")
contains(s,words)
答案 0 :(得分:2)
假设文件中每行有一个单词,例如
asd
asdf
您可以使用此功能,将每行添加到words
:
name = input("which file would you like to open? ")
s = input("word? ")
F = open(name, "r")
words = set()
for line in F: # add every line to words (assuming one word per line)
words.add(line.strip())
def contains(s, words):
if s in words:
print("true")
else:
print("false")
contains(s, words)
打印输出:
which file would you like to open? file.txt
word? asd
true
编辑:实际任务的缩短方式:
name = input("which file would you like to open? ")
s = input("word? ")
F = open(name, "r")
print("true") if s in F.read() else print("false")
答案 1 :(得分:1)
假设您的文件如下所示:
banana
apple
apple
orange
让我们创建该文件:
with open("test.txt","w") as f:
f.write("banana\napple\napple\norange")
现在让我们运行示例代码:
s= input("word? ")
words = set()
# Using with we don't need to close the file
with open("test.txt") as f:
# here is the difference from your code
for item in f.read().split("\n"):
words.add(item)
def contains(s,words):
for word in words:
if s in word:
print("true")
break
else:
print("false")
contains(s,words)
打字:
apple returns "true"
ap returns "true"
oeoe returns "false"
答案 2 :(得分:1)
正确的方法是使用生成器:
name = input("which file would you like to open? ")
word_to_look_for=input("word? ")
def check(word_to_look_for, word_from_file):
return word_to_look_for == word_from_file
with open(name, "r") as file:
# The code inside the parentheses () returns a generator object
word_exists = (check(word_to_look_for, word_from_file.rstrip("\n")) for word_from_file in file.readlines())
# This will return true if either one of the "check" function results is True
print(any(word_exists))
答案 3 :(得分:0)
这里有一些事情,我不确定你是否完全清楚:
首先,F是一个文件。我猜这里你的意图是你要检查一个单词是否在一个单词文件中(如字典)。要做到这一点,你需要做这样的事情:
filename = "somefilename.txt"
words = set()
# open the file
file = open(filename, "r")
# read the lines into an array
lines = file.readlines()
for line in lines:
words.add(line)
testWord = input("word? ")
第二个问题是你正在使用一个函数,但是你将参数误认为是你在主流中声明的相同变量。例如
# this is a verbose way of doing this
words = set()
testWord = input()
def isWordInSet(word, words):
# words here is not the same as the words
# outside the function
if word in words:
print("True")
else:
print("False")
isWordInSet(testWord, words)
答案 4 :(得分:0)
首先,最好使用
打开文件with open(filepath, 'r') as input_file:
你可以在这里读到这个
https://docs.python.org/3/tutorial/inputoutput.html
此外,您尝试将文件添加到集中,但您需要添加单词。 所以这是有效的(和更多的pythonic)代码:
import os
def load_data(filepath):
if not os.path.exists(filepath):
return None
with open(filepath, 'r') as input_file:
text = input_file.read()
return text
if __name__ == '__main__':
filepath = input("Which file would you like to open?")
word=input("What we want to find?")
text = load_data(filepath)
if not text:
print("File is empty or not exists!")
raise SystemExit
words = set(text.split())
print(word in words)