Iterate over pairs in a list (circular fashion) in Python

前端 未结 13 2002
醉酒成梦
醉酒成梦 2020-12-01 01:14

The problem is easy, I want to iterate over each element of the list and the next one in pairs (wrapping the last one with the first).

I\'ve thought about two unpyth

13条回答
  •  一个人的身影
    2020-12-01 02:05

    Even shorter version of Fortran's zip * range solution (with lambda this time;):

    group = lambda t, n: zip(*[t[i::n] for i in range(n)])
    
    group([1, 2, 3, 3], 2)
    

    gives:

    [(1, 2), (3, 4)]
    

提交回复
热议问题