Most pythonic way to get the previous element

后端 未结 5 1872
忘掉有多难
忘掉有多难 2021-01-04 19:18

I would like an enumerate-like functional on iterators which yields the pair (previous_element, current_element). That is, given that iter

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-04 20:10

    To put more itertools on the table:

    from itertools import tee, izip, chain
    
    def tee_zip(iterable):
       a, b = tee(iterable)
       return izip(chain([None], a), b)
    

提交回复
热议问题