Definition of the zip () function
- Combine elements from multiple iterators in a parameter into a new iterator;
- return: returns a Zip object whose inner elements are tuples, which can be converted to lists or tuples;
- Incoming parameters: tuples, lists, dictionaries, and other iterators.
Use of the zip () function
When there is only one parameter in the ZIP () function
Zip (iterable) takes a meta-ancestor from the iterable in turn, forming a meta-ancestor
#Zip () function single parameterList1 = [1, 2, 3, 4]tuple1=Zip (list1)#Print the return type of a zip functionPrint("The return type of the zip () function: \ n", type (tuple1))#Convert a Zip object to a listPrint("the Zip object is converted to a list: \ n", List (tuple1))#Output ResultsThe return type of the zip () function:<class 'Zip'>the Zip object is converted to a list: [(1,), (2,), (3,), (4,)]
When there are multiple parameters in the ZIP () function:
#Practice AreaList_1 = ['a','b','C','D','e','F','g','h']list_2= [9, 8, 7, 6, 5, 4, 3, 2 ] forIinchRange (len (list_1)):ifList_2[i]% 2 = = 1: Print(list_1[i],end=' ') Print("")#in this case, we'll use a zip to package two lists into a list of two ancestors. forI,jinchZip (list_1,list_2):ifJ% 2 = =0:Print(i,end=' ') A c e G b d F h
Python:zip () function