Python 按特定顺序测试多个字符串
是否有一种方法可以按特定顺序测试多个字符串?大概是这样的:Python 按特定顺序测试多个字符串,python,string,Python,String,是否有一种方法可以按特定顺序测试多个字符串?大概是这样的: if str.find(["who", "are", "you"], "who the heck are you") != -1: Print("I AM JOE") reduce(lambda x, y: x[x.find(y):], ["who", "are", "you"], "who the heck are you") 可能最简单的编码方法是使用正则表达式。代码如下: import re if re.search
if str.find(["who", "are", "you"], "who the heck are you") != -1:
Print("I AM JOE")
reduce(lambda x, y: x[x.find(y):], ["who", "are", "you"], "who the heck are you")
可能最简单的编码方法是使用正则表达式。代码如下:
import re
if re.search("who\s.*are\s.*you","who the heck are you"):
print("I AM JOE")
在模式
who\s.*are\s.*中,您
,*
表示匹配任何字符串,\s
表示匹配空格。可能最简单的编码方法是使用正则表达式。代码如下:
import re
if re.search("who\s.*are\s.*you","who the heck are you"):
print("I AM JOE")
在模式
who\s.*are\s.*中,你
,*
表示匹配任何字符串,\s
表示匹配空白。这个问题可能有很多种情况,首先你需要指定文本中每个单词的频率,例如,如果你有一个如下句子:
s = "you who the heck are you"
任何方式如果您假设示例句子中的单词重复了一次,您可以使用list.index
方法并检查索引是否已排序,那么顺序与单词列表类似:
>>> s = "who the heck are you"
>>>
>>> w = ["who", "are", "you"]
>>> splitted_text = s.split()
>>> indices = [splitted_text.index(i) for i in w]
>>> sorted(indices) == indices
True
请注意,由于str.find()
在整个字符串中搜索一个模式,这不是获取单词索引的正确选择,因为您可能有一个类似于Areyou
的单词,如果您使用str.find
查找单词you
的索引,它将返回单词开头的索引,直到you
不是一个单独的单词
或者,如果您想使用正则表达式,您可以使用str.join()
函数根据单词顺序创建正则表达式:
>>> pattern = r'\b.*\b'.join(w)
>>> regex = re.compile(pattern)
>>>
>>> bool(regex.search(s))
True
>>> s = " you who the heck are"
>>> bool(regex.search(s))
False
这个问题可能有很多场景,首先你需要指定文本中每个单词的频率,例如,如果你有一个如下的句子怎么办:
s = "you who the heck are you"
任何方式如果您假设示例句子中的单词重复了一次,您可以使用list.index
方法并检查索引是否已排序,那么顺序与单词列表类似:
>>> s = "who the heck are you"
>>>
>>> w = ["who", "are", "you"]
>>> splitted_text = s.split()
>>> indices = [splitted_text.index(i) for i in w]
>>> sorted(indices) == indices
True
请注意,由于str.find()
在整个字符串中搜索一个模式,这不是获取单词索引的正确选择,因为您可能有一个类似于Areyou
的单词,如果您使用str.find
查找单词you
的索引,它将返回单词开头的索引,直到you
不是一个单独的单词
或者,如果您想使用正则表达式,您可以使用str.join()
函数根据单词顺序创建正则表达式:
>>> pattern = r'\b.*\b'.join(w)
>>> regex = re.compile(pattern)
>>>
>>> bool(regex.search(s))
True
>>> s = " you who the heck are"
>>> bool(regex.search(s))
False
这将逐字测试,检查每个关键字是否包含在前一个关键字之后
def find_in_order(text, words):
tokens = text.split()
start = 0
for word in words:
try:
start = tokens.index(word, start) + 1
except:
return False
return True
测试:
>>> find_in_order("who the hell are you", ["who", "are", "you"])
True
>>> find_in_order("who the hell is you", ["who", "are", "you"])
False
>>> find_in_order("you who the hell are you", ["who", "are", "you"])
True
>>> find_in_order("who bare bayou", ["who", "are", "you"])
False
>>> find_in_order("who are you", ["who", "are", "are", "you"])
False
或者使用不在空格处拆分的变体,因此,谁将通过您
:
def find_in_order(text, words):
start = 0
for word in words:
try:
start = text.index(word, start) + len(word)
except:
return False
return True
这将逐字测试,检查每个关键字是否包含在前一个关键字之后
def find_in_order(text, words):
tokens = text.split()
start = 0
for word in words:
try:
start = tokens.index(word, start) + 1
except:
return False
return True
测试:
>>> find_in_order("who the hell are you", ["who", "are", "you"])
True
>>> find_in_order("who the hell is you", ["who", "are", "you"])
False
>>> find_in_order("you who the hell are you", ["who", "are", "you"])
True
>>> find_in_order("who bare bayou", ["who", "are", "you"])
False
>>> find_in_order("who are you", ["who", "are", "are", "you"])
False
或者使用不在空格处拆分的变体,因此,谁将通过您
:
def find_in_order(text, words):
start = 0
for word in words:
try:
start = text.index(word, start) + len(word)
except:
return False
return True
另一种不使用正则表达式的方法是使用您的原始直觉,不断地将find按顺序应用于同一个句子。这里您将使用“reduce”函数。它看起来像这样:
if str.find(["who", "are", "you"], "who the heck are you") != -1:
Print("I AM JOE")
reduce(lambda x, y: x[x.find(y):], ["who", "are", "you"], "who the heck are you")
这将通过搜索词列表不断减少句子。如果最后一个单词存在,则最后一个值将是句子的剩余部分;如果不存在,则最后一个字符将是句子的剩余部分。所以你可以这样写一个函数:
def find_in_order(sentence, word_list):
return reduce(lambda x, y: x[x.find(y):], word_list, sentence).startswith(word_list[-1])
>>> find_in_order('who the heck are you', ['who', 'are', 'you'])
True
有一个问题是,如果最后一个单词是一个字符,而这个字符恰好是句子的最后一个字符,那么它将始终返回True。如果这是一个问题,您可以简单地在句子末尾添加与最后一个搜索项不匹配的内容。另一种不使用正则表达式的方法是使用您最初的直觉,按顺序对同一句子连续应用find。这里您将使用“reduce”函数。它看起来像这样:
if str.find(["who", "are", "you"], "who the heck are you") != -1:
Print("I AM JOE")
reduce(lambda x, y: x[x.find(y):], ["who", "are", "you"], "who the heck are you")
这将通过搜索词列表不断减少句子。如果最后一个单词存在,则最后一个值将是句子的剩余部分;如果不存在,则最后一个字符将是句子的剩余部分。所以你可以这样写一个函数:
def find_in_order(sentence, word_list):
return reduce(lambda x, y: x[x.find(y):], word_list, sentence).startswith(word_list[-1])
>>> find_in_order('who the heck are you', ['who', 'are', 'you'])
True
有一个问题是,如果最后一个单词是一个字符,而这个字符恰好是句子的最后一个字符,那么它将始终返回True。如果这是一个问题,你可以简单地在句子末尾添加与最后一个搜索项不匹配的内容。你已经非常接近了。请记住find()函数为索引提供了一个正整数值。因此,如果谁是5岁,谁是9岁,那么他们是有序的!你很接近。请记住find()函数为索引提供了一个正整数值。因此,如果谁是5岁,谁是9岁,那么他们是有序的!讨厌的Regex!谁将是你,打印消息。我相信你可以做一些可怕的调整来让regex正常工作,though@FirebladeDan:您说得对,编辑了原始帖子以包含空格。现在,对于
“您是谁”
(模式要求目标词之间至少有两个空格)而言,此操作失败。Regex不是解决这个问题的好办法。@tdelaney:这也有同样的问题:它会抓住“谁是河口的人”
。或者,拿起你的螺丝刀,而不是试图用胶带把一个菲利普斯钻头粘到锤子上。讨厌的Regex!谁将是你,打印消息。我相信你可以做一些可怕的调整来让regex正常工作,though@FirebladeDan:您说得对,编辑了原始帖子以包含空格。现在,对于“您是谁”
(模式要求目标词之间至少有两个空格)而言,此操作失败。Regex不是解决这个问题的好方法。@tdelaney:这也有同样的问题:它捕捉到了“who bare bayou”
。或者,拿起你的螺丝刀,而不是试图用胶带将phillips钻头粘到锤子上。这也与who bare bayou
匹配最初的问题是按一定顺序查找多个字符串,不是由空格或标点符号按一定顺序分隔的多个单词。能够解析语言是一个非常不同的问题。这也符合who bare bayou
or