pairwise traversal of a list or tuple

后端 未结 6 1449
走了就别回头了
走了就别回头了 2020-12-03 14:11
a = [5, 66, 7, 8, 9, ...]

Is it possible to make an iteration instead of writing like this?

a[1] - a[0]

a[2] - a[1]

a[3] - a[2]

         


        
6条回答
  •  抹茶落季
    2020-12-03 14:21

    def pairwise(iterable):
        i = iter(iterable)
        while True:
            yield next(i), next(i, '')
    

提交回复
热议问题