Zip function-Python 3, zip function-python

Source: Internet
Author: User

Zip function-Python 3, zip function-python

The zip function accepts any number of (including 0 and 1) sequences as parameters and returns a tuple list.

The zip function is easier to generate a dictionary (dict) after obtaining data.

 

For examples:
1 # Code based on Python 3.x 2 # _ * _ coding: UTF-8 _ * _ 3 # _ Author: "LEMON" 4 5 pList = [('lil ', 'ly ', 80), ('zeng', 'zw', 90), ('dudu', 'lr', 98)] 6 names = [] 7 scores = [] 8 for I in range (len (pList )): 9 aStr = pList [I] [0] 10 bStr = pList [I] [2] 11 names. append (aStr) 12 scores. append (bStr) 13 14 15 aDict = dict (zip (names, scores) 16 print (aDict)

Running result:

1 {'dudu': 98, 'zeng': 90, 'lil': 80}

 

Of course, the above case provides a simpler implementation method:

1 # Code based on Python 3.x 2 # _ * _ coding: UTF-8 _ * _ 3 # _ Author: "LEMON" 4 5 pList = [('lil ', 'ly ', 80), ('zeng', 'zw', 90), ('dudu', 'lr', 98)] 6 aDict = {} 7 for data in pList: 8 aDict [data [0] = data [2] 9 # dict [Key] = value10 print (aDict)

 

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.