Tuple unpacking in for loops

前端 未结 6 1401
感动是毒
感动是毒 2020-11-22 16:41

I stumbled across the following code:

for i,a in enumerate(attributes):
   labels.append(Label(root, text = a, justify = LEFT).grid(sticky = W))
   e = Entry         


        
6条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 17:16

    Enumerate basically gives you an index to work with in the for loop. So:

    for i,a in enumerate([4, 5, 6, 7]):
        print i, ": ", a
    

    Would print:

    0: 4
    1: 5
    2: 6
    3: 7
    

提交回复
热议问题