How to use re match objects in a list comprehensio

2019-01-30 02:02发布

I have a function to pick out lumps from a list of strings and return them as another list:

def filterPick(lines,regex):
    result = []
    for l in lines:
        match = re.search(regex,l)
        if match:
            result += [match.group(1)]
    return result

Is there a way to reformulate this as a list comprehension? Obviously it's fairly clear as is; just curious.


Thanks to those who contributed, special mention for @Alex. Here's a condensed version of what I ended up with; the regex match method is passed to filterPick as a "pre-hoisted" parameter:

import re

def filterPick(list,filter):
    return [ ( l, m.group(1) ) for l in list for m in (filter(l),) if m]

theList = ["foo", "bar", "baz", "qurx", "bother"]
searchRegex = re.compile('(a|r$)').search
x = filterPick(theList,searchRegex)

>> [('bar', 'a'), ('baz', 'a'), ('bother', 'r')]

4条回答
唯我独甜
2楼-- · 2019-01-30 02:45
return [m.group(1) for m in (re.search(regex, l) for l in lines) if m]
查看更多
劫难
3楼-- · 2019-01-30 02:49
>>> "a" in "a visit to the dentist" 
True 
>>> "a" not in "a visit to the dentist" 
False

That also works with a search query you're hunting down in a list

`P='a', 'b', 'c'

'b' in P` returns true

查看更多
冷血范
4楼-- · 2019-01-30 02:50

It could be shortened a little

def filterPick(lines, regex):
    matches = map(re.compile(regex).match, lines)
    return [m.group(1) for m in matches if m]

You could put it all in one line, but that would mean you would have to match every line twice which would be a bit less efficient.

查看更多
Luminary・发光体
5楼-- · 2019-01-30 03:05
[m.group(1) for l in lines for m in [regex.search(l)] if m]

The "trick" is the for m in [regex.search(l)] part -- that's how you "assign" a value that you need to use more than once, within a list comprehension -- add just such a clause, where the object "iterates" over a single-item list containing the one value you want to "assign" to it. Some consider this stylistically dubious, but I find it practical sometimes.

查看更多
登录 后发表回答