How does zip(*[iter(s)]*n) work in Python?

后端 未结 6 1526
渐次进展
渐次进展 2020-11-22 05:55
s = [1,2,3,4,5,6,7,8,9]
n = 3

zip(*[iter(s)]*n) # returns [(1,2,3),(4,5,6),(7,8,9)]

How does zip(*[iter(s)]*n) work? What would it l

6条回答
  •  心在旅途
    2020-11-22 06:27

    The other great answers and comments explain well the roles of argument unpacking and zip().

    As Ignacio and ujukatzel say, you pass to zip() three references to the same iterator and zip() makes 3-tuples of the integers—in order—from each reference to the iterator:

    1,2,3,4,5,6,7,8,9  1,2,3,4,5,6,7,8,9  1,2,3,4,5,6,7,8,9
    ^                    ^                    ^            
          ^                    ^                    ^
                ^                    ^                    ^
    

    And since you ask for a more verbose code sample:

    chunk_size = 3
    L = [1,2,3,4,5,6,7,8,9]
    
    # iterate over L in steps of 3
    for start in range(0,len(L),chunk_size): # xrange() in 2.x; range() in 3.x
        end = start + chunk_size
        print L[start:end] # three-item chunks
    

    Following the values of start and end:

    [0:3) #[1,2,3]
    [3:6) #[4,5,6]
    [6:9) #[7,8,9]
    

    FWIW, you can get the same result with map() with an initial argument of None:

    >>> map(None,*[iter(s)]*3)
    [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
    

    For more on zip() and map(): http://muffinresearch.co.uk/archives/2007/10/16/python-transposing-lists-with-map-and-zip/

提交回复
热议问题