Is there an easy way to unpack a tuple while using enumerate in loop?

后端 未结 1 941
清酒与你
清酒与你 2021-01-12 14:19

Consider this:

the_data = [\'a\',\'b\',\'c\']

With enumerate this loop can be written as:

  for index,item in enumerate(the         


        
相关标签:
1条回答
  • 2021-01-12 14:53

    Use this:

    for index, (name, sport) in enumerate(the_data.iteritems()):
       pass
    

    This is equivalent to:

    >>> a, (b, c) = [1, (2, 3)]
    >>> a, b, c
    (1, 2, 3)
    

    This is commonly used with zip and enumerate combo as well:

    for i, (a, b) in enumerate(zip(seq1, seq2)):
        pass
    
    0 讨论(0)
提交回复
热议问题