sorted() using generator expressions rather than lists

前端 未结 8 1587
梦如初夏
梦如初夏 2020-11-30 05:31

After seeing the discussion here: Python - generate the time difference I got curious. I also initially thought that a generator is faster than a list, but when it comes to

8条回答
  •  悲&欢浪女
    2020-11-30 06:10

    If performance is important why not process the data as it is yielded by the generator, and apply the ordering over results of the iterations? Of course this could be used only if there is no causal conditioning between iterations (i.e. the data of sorted iteration #[i] is not needed to do any calculation for sorted iteration #[i + 1]). What I am trying to say in this case is that sorting a set of potentially larger structures yielded by the generator might be adding a lot of unnecessary complexity to an ordering which might take place posterior to processing all elements.

提交回复
热议问题