Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A different pattern with a list comprehension

I'm facing a peculiar problem, recently I've felt the need to produce list comprehensions of a certain type.

For example:

[ re.search('xyz', l).group(0) for l in my_list if re.search('xyz', l) ]

Now as the keen reader might have observed this list comprehension does regexp matching twice.

I would like to eliminate this 'extra' overhead somehow losing out on the elegance of list comprehensions. Has someone faced such a problem before? If so how did they go about solving it?

In general I have a function f() that I apply to a value/object x from the list, now I wish to have f(x).b in my list if and only if f(x).a statisfies some condition.

I know

empty_list = []
for l in my_list:
    match = re.search('xyz', l)
    if match:
        empty_list.append(match.group(0))

or more generally:

empty_list = []
for x in my_list:
    val = f(x)
    if val.a == condition:
        empty_list.append(val.b)

is a possible solution, but this seems to be too verbose, I'm sure there is a more 'pythonic' way of doing this.

like image 660
ffledgling Avatar asked Oct 22 '25 04:10

ffledgling


1 Answers

Use generator comprehensions:

# Your original list of data.
my_list = []

# This 'knows' how to produce a new list of elements where each 
# element satisfies some test, here represented as `predicate`.
# Looks like a list comprehension, but note the surrounding parens.
passing = (x for x in my_list if predicate(x))

# This knows how to apply some function `f` to each element in the list.
mapped = (f(x) for x in passing)

# Now use the know-how above to actually create your list.
# Note that because you're using chained generators, you are 
# only iterating over the original list once!
results = list(mapped)

# Or if you don't need all the intermediate values at once...
for el in mapped:
  do_something_with(el)
like image 170
Kenan Banks Avatar answered Oct 23 '25 18:10

Kenan Banks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!