Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python函数在任何提供的句子中查找单词_Python_Function_Boolean_Boolean Logic - Fatal编程技术网

Python函数在任何提供的句子中查找单词

Python函数在任何提供的句子中查找单词,python,function,boolean,boolean-logic,Python,Function,Boolean,Boolean Logic,我是编程新手,我刚开始通过工作中的在线课程学习Python。我目前的主题是写函数。我认为拥有一个函数会很有用,我可以使用它来搜索提供的任何文本字符串中的单词 def searchword(enterprise, customer): if "enterprise" exist: result = True else: result = False 我不知道如何写的功能,搜索2个字的企业和客户。我不知道如何定义它,它将在一个句子字符串中查找单词 我

我是编程新手,我刚开始通过工作中的在线课程学习Python。我目前的主题是写函数。我认为拥有一个函数会很有用,我可以使用它来搜索提供的任何文本字符串中的单词

def searchword(enterprise, customer):
if "enterprise" exist:
    result = True
else: 
    result = False
    
我不知道如何写的功能,搜索2个字的企业和客户。我不知道如何定义它,它将在一个句子字符串中查找单词

我的导师在工作中给了我以下功能,以验证我的上述功能是否正常工作,但我一直无法让它正常工作

def check_searchword():
assert searchword(True, True), "StarFox is an enterprise client."
print('Correct')

伟大的开始项目-需要注意的几件事。首先,Python是通过缩进工作的,这也是导师功能不起作用的原因之一

另一种方法是使用for循环,它将迭代列表中的每个项目并进行检查。例如:

test ="StarFox is an enterprise client."
searchword = ['enterprise', 'customer']

for i in searchword:
  print(i in test) #please note, this will look for the grouping of letters and not a direct work match. So the loop will see 'enterprise' and 'enterprises' as the same thing
def search_words(text, words):
    text_words = text.split()
    return all(word in text_words for word in words)

print(search_words('this was a test', ['was', 'test']))

最后,这种文本检查最好使用正则表达式。网上有很多关于这方面的教程——这本书《自动化枯燥的东西》是一个很好的资源,还有一些博客文章,如《伟大的开始项目》——有几点需要注意。首先,Python是通过缩进工作的,这也是导师功能不起作用的原因之一

另一种方法是使用for循环,它将迭代列表中的每个项目并进行检查。例如:

test ="StarFox is an enterprise client."
searchword = ['enterprise', 'customer']

for i in searchword:
  print(i in test) #please note, this will look for the grouping of letters and not a direct work match. So the loop will see 'enterprise' and 'enterprises' as the same thing
def search_words(text, words):
    text_words = text.split()
    return all(word in text_words for word in words)

print(search_words('this was a test', ['was', 'test']))

最后,这种文本检查最好使用正则表达式。在线上有很多关于这方面的教程——这本书《自动化枯燥的东西》是一个很好的资源,还有许多博客文章,如《学习如何编程》就是学习如何将问题分解成可以进一步分解的步骤,等等,直到你达到一个命令或语句存在的步骤。为了让每一步都真正具体化,做你自己的魔鬼代言人

如果你正在为一项工作编程,诀窍是了解许多维护良好的库,这些库允许你在一条语句中为更复杂的步骤编写代码,而不是必须自己编写所有代码,但是作为初学者,自己编写代码会很有启发性

如果您正在学习如何使用函数,那么要学习的关键是,如果函数更通用,那么它会更有用,但不会太通用,以至于对于简单的使用来说,它会变得不必要的复杂

您提出了一个在提供的字符串中搜索单词的函数。您给出的示例似乎适用于两个特定的单词,并且仅适用于这两个单词。更一般的是一个函数,它在提供的字符串中搜索任意数量的单词

如果要解决在字符串中查找多个单词的问题,最明显的步骤是需要在字符串中查找一个单词,并为每个单词执行该任务。如果全部找到,则结果应为真,否则应为假

像这样简单的东西在Python中工作:

>>> 'test' in 'this was a test'
True
尽管这可能表明您还需要其他东西:

>>> 'is' in 'this was a test'
True
是其中的一部分,所以这可能不是你想要的。具体一点-您只想匹配整个单词还是部分单词?如果你只想要整词,你现在有一个新任务:在整词中分解一个字符串

大概是这样的:

>>> 'is' in 'this was a test'.split()
False
>>> 'test' in 'this was a test'.split()
True
如果您需要一次又一次地执行多个操作,循环听起来很明显:

>>> text = 'this was a test'
>>> result = True
>>> for word in ['test', 'was']:
...      result = result and word in text.split()
如果您的任务是在函数中捕获它,那么函数应该通过其参数(字符串和要查找的单词)接收完成其工作所需的所有内容,并且它应该以单个布尔值返回您期望的所有内容

def search_words(text, words):
    result = True
    for word in words:
        result = result and word in text.split()
    return result

print(search_words('this was a test', ['was', 'test']))
这是可行的,但效率不是很高,因为它一次又一次地拆分文本,每个单词一次。此外,对于训练有素的Python眼睛来说,它看起来不是很“pythonic”,这通常意味着用清晰易懂的Python编写,它是基于Python作为一种语言的优势而构建的

例如:

test ="StarFox is an enterprise client."
searchword = ['enterprise', 'customer']

for i in searchword:
  print(i in test) #please note, this will look for the grouping of letters and not a direct work match. So the loop will see 'enterprise' and 'enterprises' as the same thing
def search_words(text, words):
    text_words = text.split()
    return all(word in text_words for word in words)

print(search_words('this was a test', ['was', 'test']))
当然,编程的另一个重要方面是满足客户的规范。如果他们坚持想要一个只使用文本作为参数搜索这两个特定单词的函数,您可以使用上述函数快速为他们提供:

def client_search_words(text):
    return search_words(text, ['enterprise', 'customer'])


assert client_search_words('StarFox is an enterprsise client'), True
这将抛出一个断言错误,因为它断言的内容为False,而函数的结果为False


我不认为在这里使用断言是一种很好的方式,但这是Python的一部分,通常远不如您现在要处理的内容重要。

学习如何编程就是学习将问题分解为可以进一步分解的步骤,等,直到您到达存在单个命令或语句的步骤。为了让每一步都真正具体化,做你自己的魔鬼代言人

如果您正在为一项作业编程,诀窍是了解许多维护良好的库,这些库允许您在单个语句中编写更复杂的步骤 必须自己编写所有代码,但作为初学者,自己编写代码可能会很有帮助

如果您正在学习如何使用函数,那么要学习的关键是,如果函数更通用,那么它会更有用,但不会太通用,以至于对于简单的使用来说,它会变得不必要的复杂

您提出了一个在提供的字符串中搜索单词的函数。您给出的示例似乎适用于两个特定的单词,并且仅适用于这两个单词。更一般的是一个函数,它在提供的字符串中搜索任意数量的单词

如果要解决在字符串中查找多个单词的问题,最明显的步骤是需要在字符串中查找一个单词,并为每个单词执行该任务。如果全部找到,则结果应为真,否则应为假

像这样简单的东西在Python中工作:

>>> 'test' in 'this was a test'
True
尽管这可能表明您还需要其他东西:

>>> 'is' in 'this was a test'
True
是其中的一部分,所以这可能不是你想要的。具体一点-您只想匹配整个单词还是部分单词?如果你只想要整词,你现在有一个新任务:在整词中分解一个字符串

大概是这样的:

>>> 'is' in 'this was a test'.split()
False
>>> 'test' in 'this was a test'.split()
True
如果您需要一次又一次地执行多个操作,循环听起来很明显:

>>> text = 'this was a test'
>>> result = True
>>> for word in ['test', 'was']:
...      result = result and word in text.split()
如果您的任务是在函数中捕获它,那么函数应该通过其参数(字符串和要查找的单词)接收完成其工作所需的所有内容,并且它应该以单个布尔值返回您期望的所有内容

def search_words(text, words):
    result = True
    for word in words:
        result = result and word in text.split()
    return result

print(search_words('this was a test', ['was', 'test']))
这是可行的,但效率不是很高,因为它一次又一次地拆分文本,每个单词一次。此外,对于训练有素的Python眼睛来说,它看起来不是很“pythonic”,这通常意味着用清晰易懂的Python编写,它是基于Python作为一种语言的优势而构建的

例如:

test ="StarFox is an enterprise client."
searchword = ['enterprise', 'customer']

for i in searchword:
  print(i in test) #please note, this will look for the grouping of letters and not a direct work match. So the loop will see 'enterprise' and 'enterprises' as the same thing
def search_words(text, words):
    text_words = text.split()
    return all(word in text_words for word in words)

print(search_words('this was a test', ['was', 'test']))
当然,编程的另一个重要方面是满足客户的规范。如果他们坚持想要一个只使用文本作为参数搜索这两个特定单词的函数,您可以使用上述函数快速为他们提供:

def client_search_words(text):
    return search_words(text, ['enterprise', 'customer'])


assert client_search_words('StarFox is an enterprsise client'), True
这将抛出一个断言错误,因为它断言的内容为False,而函数的结果为False


我不认为在这里使用断言是一种很好的方式,但这是Python的一部分,通常远不如您现在要处理的内容重要。

要将字符串拆分为所有单词的列表,我将使用string1.split

在列表1中的python string1中,如果列表1中存在string1,则返回True,否则返回False

# This function is used to search a word in a string.
# it returns True if search_key is present in search_string. 
# else False.

def searchword(search_key, search_string):
    search_list = search_string.split()
    return search_key in search_list 

search_s = "StarFox is an enterprise client."
print( searchword("enterprise", search_s) ) 

要将字符串拆分为所有单词的列表,我将使用string1.split

在列表1中的python string1中,如果列表1中存在string1,则返回True,否则返回False

# This function is used to search a word in a string.
# it returns True if search_key is present in search_string. 
# else False.

def searchword(search_key, search_string):
    search_list = search_string.split()
    return search_key in search_list 

search_s = "StarFox is an enterprise client."
print( searchword("enterprise", search_s) ) 

你是在寻找单词enterprise,还是在寻找单词中可变的enterprise?还有,这个词是什么?你将如何从函数中使用它?你的问题实际上不是关于布尔逻辑或布尔逻辑,而是关于一般的Python语法。你的问题非常基本,这很好——我们都必须从某个地方开始,但你最好从一些有效的东西开始,并展示改变它是如何给你带来麻烦的。您上面提供的代码有太多错误,无法有效回答您的问题,即没有定义exist,结果未返回等。您是在word中查找enterprise一词,还是在word中查找变量enterprise?还有,这个词是什么?你将如何从函数中使用它?你的问题实际上不是关于布尔逻辑或布尔逻辑,而是关于一般的Python语法。你的问题非常基本,这很好——我们都必须从某个地方开始,但你最好从一些有效的东西开始,并展示改变它是如何给你带来麻烦的。您上面提供的代码有太多错误,无法有效回答您的问题,即没有定义exist,结果未返回等。