假设我有一个昂贵的操作expensive(x: int) -> int
和以下列表理解:
# expensive(x: int) -> int
# check(x: int) -> bool
[expensive(i) for i in range(LARGE_NUMBER) if check(expensive(i))]
如果我想避免expensive(i)
为每个运行两次i
,有没有办法通过列表理解来保存它的价值?
假设我有一个昂贵的操作expensive(x: int) -> int
和以下列表理解:
# expensive(x: int) -> int
# check(x: int) -> bool
[expensive(i) for i in range(LARGE_NUMBER) if check(expensive(i))]
如果我想避免expensive(i)
为每个运行两次i
,有没有办法通过列表理解来保存它的价值?