Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.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 若字符串等于列表a中的项,后跟列表b中的项_Python - Fatal编程技术网

Python 若字符串等于列表a中的项,后跟列表b中的项

Python 若字符串等于列表a中的项,后跟列表b中的项,python,Python,我想做一个if语句来检测字符串是否可以按顺序由列表中的项组成。例如,如果我想检查一个术语是否与“HelloWorld”具有相同的含义,我会有一个带有[“hello”,“hello”,“Hi”,“hello”,“Hola']的“hello”列表,以及一个带有[“world”,“world”,“Planet”,“Earth']的“world”列表。然后,检查字符串是否等于“hello”列表中的任何项,后跟“world”列表中的任何项。“HelloWorld”、“GreetingsEarth”和“Hi

我想做一个if语句来检测字符串是否可以按顺序由列表中的项组成。例如,如果我想检查一个术语是否与“HelloWorld”具有相同的含义,我会有一个带有
[“hello”,“hello”,“Hi”,“hello”,“Hola']
的“hello”列表,以及一个带有
[“world”,“world”,“Planet”,“Earth']
的“world”列表。然后,检查字符串是否等于“hello”列表中的任何项,后跟“world”列表中的任何项。“HelloWorld”、“GreetingsEarth”和“HiPlanet”都将成功地使if语句跳闸。我该怎么做?我想使用Python列表,所以regex(a | b)似乎不实用。

regex可以很好地工作:

a = ['hello', 'Hello', 'Hi', 'Greetings', 'Hola']
b = ['world', 'World', 'Planet', 'Earth']

import re
r = '^(%s)(%s)$' % ('|'.join(a), '|'.join(b))

print re.match(r, "HelloWorld").groups() # ('Hello', 'World')
print re.match(r, "HiThere") # None
非正则表达式解决方案非常乏味:

s = "GreetingsEarth"
for x in a:
    if s.startswith(x) and s[len(x):] in b:
        print x, '+', s[len(x):]
        break 

正则表达式可以正常工作:

a = ['hello', 'Hello', 'Hi', 'Greetings', 'Hola']
b = ['world', 'World', 'Planet', 'Earth']

import re
r = '^(%s)(%s)$' % ('|'.join(a), '|'.join(b))

print re.match(r, "HelloWorld").groups() # ('Hello', 'World')
print re.match(r, "HiThere") # None
非正则表达式解决方案非常乏味:

s = "GreetingsEarth"
for x in a:
    if s.startswith(x) and s[len(x):] in b:
        print x, '+', s[len(x):]
        break 

这实际上可以通过正则表达式实现,如下所示:

list1 = ['hello', 'Hello', 'Hi', 'Greetings', 'Hola']
list2 = ['world', 'World', 'Planet', 'Earth']
regex = "(%s)(%s)" % ("|".join(list1), "|".join(list2))
print re.match(regex, "HelloWorld")
但也可以使用
itertools.product

print any("HelloWorld" == x + y for x, y in itertools.product(list1, list2)) 
import itertools
combinations = (''.join((first, second)) for first, second in itertools.product(a, b))
any('HelloWorld' == combination for combination in combinations)

这实际上可以通过正则表达式实现,如下所示:

list1 = ['hello', 'Hello', 'Hi', 'Greetings', 'Hola']
list2 = ['world', 'World', 'Planet', 'Earth']
regex = "(%s)(%s)" % ("|".join(list1), "|".join(list2))
print re.match(regex, "HelloWorld")
但也可以使用
itertools.product

print any("HelloWorld" == x + y for x, y in itertools.product(list1, list2)) 
import itertools
combinations = (''.join((first, second)) for first, second in itertools.product(a, b))
any('HelloWorld' == combination for combination in combinations)

如果要避免使用正则表达式,可以使用测试每个组合的生成器表达式(通过
itertools.product
生成):

请注意,这比正则表达式方法慢得多,特别是在遇到最坏情况(不匹配)时:

生成器表达式比正则表达式方法慢10倍


(我使用了
re.compile()
compiled正则表达式进行测试)。

如果要避免使用正则表达式,可以使用生成器表达式测试每个组合(通过
itertools.product
生成):

请注意,这比正则表达式方法慢得多,特别是在遇到最坏情况(不匹配)时:

生成器表达式比正则表达式方法慢10倍


(我在测试中使用了一个
re.compile()
compiled正则表达式)。

我在第二个列表中使用了一个集合,这样您就不必每次迭代它的所有项

a = ['hello', 'Hello', 'Hi', 'Greetings', 'Hola']
b = ['world', 'World', 'Planet', 'Earth']

b_set = set(b)
needle = 'HelloWorld'
for start in a:
    if needle.startswith(start) and needle[len(start):] in b_set:
         print 'match'
如果您正在寻找较短的版本

any((needle[len(start):] in b_set for start in a if needle.startswith(start)))

itertools.product
相比,此解决方案不必比较所有
n^2
可能的组合,只需遍历第一个列表(
n
),在最坏的情况下,执行额外的集合查找

我为第二个列表使用了一个集合,这样您就不必每次都对它的所有项进行迭代

a = ['hello', 'Hello', 'Hi', 'Greetings', 'Hola']
b = ['world', 'World', 'Planet', 'Earth']

b_set = set(b)
needle = 'HelloWorld'
for start in a:
    if needle.startswith(start) and needle[len(start):] in b_set:
         print 'match'
如果您正在寻找较短的版本

any((needle[len(start):] in b_set for start in a if needle.startswith(start)))

itertools.product
相比,此解决方案不必比较所有
n^2
可能的组合,只需遍历第一个列表(
n
),在最坏的情况下,执行额外的集合查找

我猜正则表达式方法更快,但速度有多快?我不会这么说。正则表达式的速度非常慢。正则表达式所做的工作与Python代码基本相同。另一方面,正则表达式引擎是C语言的。我认为它可能部分取决于列表的长度。不一定取决于许多因素。根据具体情况,re实际上可以更快,特别是如果您提前使用
re.compile
编译正则表达式。编译的re是O(1)(常量)时间,而循环版本将取决于输入的大小。@AdamParkin:重新编译不是问题,因为每个正则表达式只编译一次(re模块维护一个已编译模式的缓存)。我猜正则表达式方法更快,但要快多少?我不会这么说。正则表达式的速度非常慢。正则表达式所做的工作与Python代码基本相同。另一方面,正则表达式引擎是C语言的。我认为它可能部分取决于列表的长度。不一定取决于许多因素。根据具体情况,re实际上可以更快,特别是如果您提前使用
re.compile
编译正则表达式。编译的re是O(1)(常量)时间,而循环版本将取决于输入的大小。@AdamParkin:重新编译不是问题,因为每个正则表达式只编译一次(re模块维护编译模式的缓存)。是否有兴趣确定触发了哪个匹配(即hello列表中的第XT个项目后面紧跟着世界列表中的第YT个项目?)或者仅仅是存在任何匹配?您是否有兴趣确定触发了哪个匹配(即hello列表中的第XT个项目后面紧跟着世界列表中的第YT个项目?)或者只是有匹配?出于好奇,您尝试的是编译的re版本,还是只是原始的
re.match(“someregularexpression…”)
?如果它没有被编译,那么通过
re.compile
将它输入可能会使它更快。@AdamParkin:对不起,是的,我使用了一个编译的re。请注意,
re.match
仍然编译表达式,并为此使用缓存;与直接使用编译表达式相比,您看到的速度变慢了。出于好奇y是您尝试编译的re的re版本,还是只是原始的
re.match(“someregularexpression…”)
?如果它没有被编译,那么通过
re.compile
将它输入可能会使它更快。@AdamParkin:对不起,是的,我使用了一个编译的re。请注意,
re.match
仍然编译表达式,并为此使用缓存;与直接使用编译表达式相比,您看到的速度变慢了。