list comprehension filtering - “the set() trap”

前端 未结 5 1308
野性不改
野性不改 2020-11-30 04:16

A reasonably common operation is to filter one list based on another list. People quickly find that this:

[x for x in list_1 if x          


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 04:53

    In order to optimize set(list_2), the interpreter needs to prove that list_2 (and all of its elements) does not change between iterations. This is a hard problem in the general case, and it would not surprise me if the interpreter does not even attempt to tackle it.

    On the other hand a set literal cannot change its value between iterations, so the optimization is known to be safe.

提交回复
热议问题