在Python中检查项目是否在集合中?
编写程序以检查用户输入的单词是否在预先存在的集合中;无论我输入什么单词,程序都返回“False”(即使我知道该单词在集合中)。代码如下:在Python中检查项目是否在集合中?,python,python-3.x,set,Python,Python 3.x,Set,编写程序以检查用户输入的单词是否在预先存在的集合中;无论我输入什么单词,程序都返回“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("tru
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)
假设文件中每行有一个单词,例如
asd
asdf
您可以使用它,它将每一行添加到单词中:
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")
假设您的文件如下所示:
banana
apple
apple
orange
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? ")
让我们创建该文件:
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"
正确的方法是使用发电机:
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))
这里有一些事情我不确定你是否完全清楚:
首先,F是一个文件。我猜你在这里的目的是想检查一个单词是否在一个单词文件中(比如字典)。但是,要执行此操作,您需要执行以下操作:
banana
apple
apple
orange
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)
首先,最好使用
with open(filepath, 'r') as input_file:
你可以在这里读到
您还尝试将文件添加到集合中,但需要添加单词。
因此,这是一个有效的(更具python风格的)代码:
您没有将文件内容添加到集合
。在创建setSo之前,需要先读取内容,F
是一个文件处理程序对象。我猜你是在用某种文字文件工作。你必须解析该文件并将字符串添加到你的集合
。。您需要向我们展示您的文件的外观。如果我想检查文件中是否有单词的一部分,该怎么办?假设单词“cup”在一行,如果我要求单词“up”@Parker,我希望它打印为真,那么现在你要求更多的东西:)。您只需循环单词并检查项目是否在其中(我在回答中对此进行了描述)嗨@abccd,谢谢您的评论。链接的目的是提供发电机概念的参考,但我想你说的是对的,这是误导,所以我删除了它。关于代码示例,您是否尝试运行它?如果您在代码末尾运行print(word\u存在)
,我相信您会确信它确实是一个生成器