How do I efficiently filter computed values within

2019-01-23 03:11发布

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]

Will return a list of the squares of integers in mylist. However, what if the test involves some (costly) computation and you want to filter on the result? One option is:

result = [expensive(x) for x in mylist if expensive(x)]

This will result in a list of non-"false" expensive(x) values, however expensive() is called twice for each x. Is there a comprehension syntax that allows you to do this test while only calling expensive once per x?

9条回答
混吃等死
2楼-- · 2019-01-23 03:52

There is the plain old use of a for loop to append to a list, too:

result = []
for x in mylist:
    expense = expensive(x)
    if expense:
        result.append(expense)
查看更多
戒情不戒烟
3楼-- · 2019-01-23 03:56
result = [x for x in map(expensive,mylist) if x]

map() will return a list of the values of each object in mylist passed to expensive(). Then you can list-comprehend that, and discard unnecessary values.

This is somewhat like a nested comprehension, but should be faster (since the python interpreter can optimize it fairly easily).

查看更多
乱世女痞
4楼-- · 2019-01-23 03:56

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.

查看更多
登录 后发表回答