Zip lists in Python

前端 未结 10 2072
无人及你
无人及你 2020-11-22 02:09

I am trying to learn how to \"zip\" lists. To this end, I have a program, where at a particular point, I do the following:

x1, x2, x3 = stuff.calculations(wi         


        
10条回答
  •  伪装坚强ぢ
    2020-11-22 02:25

    When you zip() together three lists containing 20 elements each, the result has twenty elements. Each element is a three-tuple.

    See for yourself:

    In [1]: a = b = c = range(20)
    
    In [2]: zip(a, b, c)
    Out[2]: 
    [(0, 0, 0),
     (1, 1, 1),
     ...
     (17, 17, 17),
     (18, 18, 18),
     (19, 19, 19)]
    

    To find out how many elements each tuple contains, you could examine the length of the first element:

    In [3]: result = zip(a, b, c)
    
    In [4]: len(result[0])
    Out[4]: 3
    

    Of course, this won't work if the lists were empty to start with.

提交回复
热议问题