Iterate a list as pair (current, next) in Python

前端 未结 10 1717
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 22:54

I sometimes need to iterate a list in Python looking at the \"current\" element and the \"next\" element. I have, till now, done so with code like:

for curre         


        
10条回答
  •  长发绾君心
    2020-11-21 23:24

    code = '0016364ee0942aa7cc04a8189ef3'
    # Getting the current and next item
    print  [code[idx]+code[idx+1] for idx in range(len(code)-1)]
    # Getting the pair
    print  [code[idx*2]+code[idx*2+1] for idx in range(len(code)/2)]
    

提交回复
热议问题