Python3 's Zip function

Source: Internet
Author: User

The ZIP function accepts any number of iterated objects as parameters, wrapping the corresponding elements in an object into a tuple, and then returns an iterator to the Zip object.

This iterative object can be used to list its elements in a circular way

If multiple iterations of an object have inconsistent lengths, the returned list is the same as the shortest-length iteration object.

Usage 1: Generate a Zip object with two lists

Example 1:

>>> a1=[1,2,3]>>> a2=[4,5,6]>>>  a3=[7,8,9]>>> a4=["A", "B", "C", "D"]>>> zip1=zip (A1,A2,A3) >>>  Print (ZIP1) <zip object at 0x7f5a22651c08>>>> for i in zip1: ...      print (i) ...  (1, 4, 7) (2, 5, 8) (3, 6, 9) 


>>> zip2=zip (A1,A2,A4) >>> print (ZIP2) < zip object at 0x7f5a22651d48>>>> for j in zip2:...      print (j) ...  (1, 4,  ' a ') (2, 5,  ' B ') (3, 6,  ' C ') 

Example 3:

>>> zip3=zip (A4) >>> print (ZIP3) <zip object at 0x7f5a22651d08>>>>-i in ZIP3: ... pr int (i) ... (' A ',) (' B ',) (' C ',) (' d ',)

>>> zip4=zip (*a4) >>> >>> print (ZIP4) <zip object at 0x7f5a22651f08>>>> for J in Zip4: .... Print (j) ... (' A ', ' B ', ' C ', ' d ', ' A ', ' B ', ' C ', ' d ', ' A ', ' B ', ' C ', ' d ')
Usage 2: Two-dimensional matrix transformation (Matrix-column interchange)
>>> l1=[[1,2,3],[4,5,6],[7,8,9]]>>> Print ([[J[i] for J in L1] for I in range (len (l1[0))]) [[1, 4, 7], [ 2, 5, 8], [3, 6, 9]]>>> Zip (*l1) <zip object at 0x7f5a22651f88>>>> for I in Zip (*L1): ... print ( i) ... (1, 4, 7) (2, 5, 8) (3, 6, 9)


Python3 's Zip function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.