在Python中模式匹配后打印列表元素

时间:2020-12-07 14:34:08

I would like to print the preceding element in a list following a pattern match. In the basic example below I match the string 'foo' in a list. I would like to print out not the match itself ('foo') but the following element to the match (in this case 'bar').

我想在模式匹配后在列表中打印前面的元素。在下面的基本示例中,我匹配列表中的字符串'foo'。我想打印出匹配本身('foo')但匹配的以下元素(在本例中为'bar')。

theList = ["foo", "bar", "baz", "qurx", "bother"] # example list

list1 = "foo" # matching string

regex = re.compile(list1)

[m.group(0) for l in theList for m in [regex.search(l)] if m] # returns 'foo'

The above code returns the match, but like I said I would like to return the following element in theList. Any help with be greatly appreciated.

上面的代码返回匹配,但就像我说的,我想在列表中返回以下元素。任何帮助都非常感谢。

1 个解决方案

#1


1  

Don't try to use clever things like list comprehensions when you are not yet quite sure how they work. It is perfectly OK to use simple code - readable code is easier to verify, after all.

当你还不确定它们是如何工作时,不要试图使用列表推导等聪明的东西。完全可以使用简单的代码 - 毕竟,可读的代码更容易验证。

Try this (for clarity I left out the regular expression nonsense):

试试这个(为了清楚起见,我省略了正则表达式的废话):

haystack = ["foo", "bar", "baz", "foo", "qurx", "bother"]
needle = "foo"

result = []
for i, element in enumerate(haystack):
    if needle in element:
        result.append(haystack[i+1])

print(result)

If you are only interested in the first (or only) match, you can use

如果您只对第一(或唯一)匹配感兴趣,可以使用

haystack = ["foo", "bar", "baz", "foo", "qurx", "bother"]
needle = "foo"

for i, element in enumerate(haystack):
    if needle in element:
        print(haystack[i+1])
        break

If you can also match on equality instead of using regex, this is the easiest approach:

如果你也可以匹配相等而不是使用正则表达式,这是最简单的方法:

idx = haystack.index(needle)
result = haystack[idx+1]

The examples above will all break if the needle is found at the last position of haystack, but I leave that as exercise to the reader.

如果在大海捞针的最后位置找到针头,上面的例子都会破裂,但我将其作为练习留给读者。

Properly implementing your original approach, the solution could look something like this:

正确实施原始方法,解决方案可能如下所示:

pairs = zip(haystack, haystack[1:])
[following for matching, following in pairs if needle in matching]

#1


1  

Don't try to use clever things like list comprehensions when you are not yet quite sure how they work. It is perfectly OK to use simple code - readable code is easier to verify, after all.

当你还不确定它们是如何工作时,不要试图使用列表推导等聪明的东西。完全可以使用简单的代码 - 毕竟,可读的代码更容易验证。

Try this (for clarity I left out the regular expression nonsense):

试试这个(为了清楚起见,我省略了正则表达式的废话):

haystack = ["foo", "bar", "baz", "foo", "qurx", "bother"]
needle = "foo"

result = []
for i, element in enumerate(haystack):
    if needle in element:
        result.append(haystack[i+1])

print(result)

If you are only interested in the first (or only) match, you can use

如果您只对第一(或唯一)匹配感兴趣,可以使用

haystack = ["foo", "bar", "baz", "foo", "qurx", "bother"]
needle = "foo"

for i, element in enumerate(haystack):
    if needle in element:
        print(haystack[i+1])
        break

If you can also match on equality instead of using regex, this is the easiest approach:

如果你也可以匹配相等而不是使用正则表达式,这是最简单的方法:

idx = haystack.index(needle)
result = haystack[idx+1]

The examples above will all break if the needle is found at the last position of haystack, but I leave that as exercise to the reader.

如果在大海捞针的最后位置找到针头,上面的例子都会破裂,但我将其作为练习留给读者。

Properly implementing your original approach, the solution could look something like this:

正确实施原始方法,解决方案可能如下所示:

pairs = zip(haystack, haystack[1:])
[following for matching, following in pairs if needle in matching]