How do I efficiently filter computed values within a Python list comprehension?

后端 未结 9 2347
青春惊慌失措
青春惊慌失措 2020-12-15 05:29

The Python list comprehension syntax makes it easy to filter values within a comprehension. For example:

result = [x**2 for x in mylist if type(x) is int]
<         


        
相关标签:
9条回答
  • 2020-12-15 06:31

    If the calculations are already nicely bundled into functions, how about using filter and map?

    result = filter (None, map (expensive, mylist))
    

    You can use itertools.imap if the list is very large.

    0 讨论(0)
  • 2020-12-15 06:31

    The most obvious (and I would argue most readable) answer is to not use a list comprehension or generator expression, but rather a real generator:

    def gen_expensive(mylist):
        for item in mylist:
            result = expensive(item)
            if result:
                yield result
    

    It takes more horizontal space, but it's much easier to see what it does at a glance, and you end up not repeating yourself.

    0 讨论(0)
  • 2020-12-15 06:33

    You could always memoize the expensive() function so that calling it the second time around is merely a lookup for the computed value of x.

    Here's just one of many implementations of memoize as a decorator.

    0 讨论(0)
提交回复
热议问题