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)