3

我有两个列表 -queryline. 我的代码查找是否query为:

["president" ,"publicly"]

包含在一个line(订单事项)中,例如:

["president" ,"publicly", "told"]

这是我目前使用的代码:

if ' '.join(query) in ' '.join(line)

问题是,我只想匹配整个单词。所以下面的查询不会通过条件语句:

["president" ,"pub"]

我怎样才能做到这一点?

4

8 回答 8

1

您可以使用正则表达式和\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

您可以将其用作:

if find_sublist(query, line) >= 0:
    print 'matching'
else:
    print 'not matching'
于 2013-04-03T07:21:49.770 回答
1

只需使用“in”运算符:

mylist = ['foo', 'bar', 'baz']

'foo' in mylist-> 返回 True 'bar' in mylist-> 返回 True 'fo' in mylist-> 返回 False 'ba' in mylist-> 返回 False

于 2013-04-03T07:23:32.270 回答
1

这是一种方法:

re.search(r'\b' + re.escape(' '.join(query)) + r'\b', ' '.join(line)) is not None
于 2013-04-03T07:23:39.950 回答
1

只是为了好玩,您还可以这样做:

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
于 2013-04-03T07:36:25.970 回答
0

您可以使用issubset方法来实现这一点。只需这样做:

a = ["president" ,"publicly"]
b = ["president" ,"publicly", "told"]

if set(a).issubset(b):
    #bla bla

这将返回两个列表中的匹配项。

于 2013-04-03T07:23:45.637 回答
0

您可以使用all内置的 quantor 函数:

if all(word in b for word in a):
    """ all words in list"""

请注意,对于长列表,这可能不会提高运行时间效率。更好地使用settype 而不是 list for a(要搜索的单词列表)。

于 2013-04-03T07:32:55.897 回答
0

这是一种非正则表达式的方法。我确信正则表达式会比这快得多:

>>> 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
于 2013-04-03T07:40:30.103 回答
0

显式优于隐式。由于订购很重要,我会这样写:

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)]

blocks保存所有相关组合以检查:

[['president', 'publicly'], ['publicly', 'told']]

现在您可以简单地检查您的查询是否在该列表中:

print query in blocks # -> True
print query_false in blocks # -> False

代码的工作方式与您可能会用文字解释直截了当的解决方案一样,这对我来说通常是一个好兆头。如果你有很长的行并且性能成为问题,你可以用生成器替换生成的列表。

于 2013-04-03T07:46:49.240 回答