python-字符串仅匹配整词
我有两个列表-python-字符串仅匹配整词,python,Python,我有两个列表-查询和行。“我的代码”查找查询,例如: ["president" ,"publicly"] 包含在行(订单事项)中,例如: 这是我目前使用的代码: if ' '.join(query) in ' '.join(line) 问题是,我只想匹配整个单词。因此,下面的查询不会传递条件语句: ["president" ,"pub"] 我该怎么做?您可以使用正则表达式和\b单词边界: import re the_regex = re.compile(r'\b' + r'\b'.join
查询
和行
。“我的代码”查找查询
,例如:
["president" ,"publicly"]
包含在行
(订单事项)中,例如:
这是我目前使用的代码:
if ' '.join(query) in ' '.join(line)
问题是,我只想匹配整个单词。因此,下面的查询不会传递条件语句:
["president" ,"pub"]
我该怎么做?您可以使用正则表达式和
\b
单词边界:
import re
the_regex = re.compile(r'\b' + r'\b'.join(map(re.escape, ['president', 'pub'])) + r'\b')
if the_regex.search(' '.join(line)):
print 'matching'
else:
print 'not matching'
或者,您可以编写一个函数来检查给定列表是否是该行的子列表。比如:
def find_sublist(sub, lst):
if not sub:
return 0
cur_index = 0
while cur_index < len(lst):
try:
cur_index = lst.index(sub[0], cur_index)
except ValueError:
break
if lst[cur_index:cur_index + len(sub)] == sub:
break
lst = lst[cur_index + 1:]
return cur_index
只需使用“in”操作符:
mylist = ['foo', 'bar', 'baz']
mylist中的“foo”返回True
mylist中的“bar”返回True
mylist中的“fo”返回False
mylist中的'ba'返回False以下是一种方法:
re.search(r'\b' + re.escape(' '.join(query)) + r'\b', ' '.join(line)) is not None
您可以使用方法来实现这一点。简单地做:
a = ["president" ,"publicly"]
b = ["president" ,"publicly", "told"]
if set(a).issubset(b):
#bla bla
这将返回两个列表中的匹配项。您可以使用
all
内置的quantor功能:
if all(word in b for word in a):
""" all words in list"""
请注意,对于长列表而言,这可能不具有运行时效率。最好使用
set
类型,而不是a
的列表(要搜索的单词列表)。只是为了好玩,您还可以执行以下操作:
a = ["president" ,"publicly", "told"]
b = ["president" ,"publicly"]
c = ["president" ,"pub"]
d = ["publicly", "president"]
e = ["publicly", "told"]
from itertools import izip
not [l for l,n in izip(a, b) if l != n] ## True
not [l for l,n in izip(a, c) if l != n] ## False
not [l for l,n in izip(a, d) if l != n] ## False
## to support query in the middle of the line:
try:
query_list = a[a.index(e[0]):]
not [l for l,n in izip(query_list, e) if l != n] ## True
expect ValueError:
pass
这里有一个非正则表达式的方法。我相信regex会比这快得多:
>>> query = ['president', 'publicly']
>>> line = ['president', 'publicly', 'told']
>>> any(query == line[i:i+len(query)] for i in range(len(line) - len(query)))
True
>>> query = ["president" ,"pub"]
>>> any(query == line[i:i+len(query)] for i in range(len(line) - len(query)))
False
显式比隐式好。至于订购事宜,我会这样写:
query = ['president','publicly']
query_false = ['president','pub']
line = ['president','publicly','told']
query_len = len(query)
blocks = [line[i:i+query_len] for i in xrange(len(line)-query_len+1)]
块
保存所有相关组合以检查:
[['president', 'publicly'], ['publicly', 'told']]
现在,您只需检查您的查询是否在该列表中:
print query in blocks # -> True
print query_false in blocks # -> False
代码的工作方式可能是您用文字解释直接的解决方案,这对我来说通常是一个好迹象。如果您的行很长,并且性能出现问题,您可以用生成器替换生成的列表。可能值得
re.escape
'ing查询字符串GettingSyntaxError:之后的语法无效None@Tom:在None
之后是什么(在re.search
之前是什么)?@Tom:if
你忘了那句话末尾的冒号了。读这个问题我认为顺序很重要,因此在很多情况下这会给出错误的结果。顺序很重要,所以它可以是一个子集,但不能完全按照我列表中的“foo bar”顺序排列。。。当然,mylist中的“foo”和mylist中的“bar”->True。。。那又怎样?:)
[['president', 'publicly'], ['publicly', 'told']]
print query in blocks # -> True
print query_false in blocks # -> False