Python Learning notes (15) Loop design

Source: Internet
Author: User

Original chain: http://www.cnblogs.com/vamei/archive/2012/07/09/2582435.html

Note: Zip () is inconsistent in Python2 3

#15th talk cycle design#loops in front are learning, simple loops forIinchRange (10):    Print(i**2)#It's a very simple loop.#Range ()s='Abcdefghijk' forIinchRange (0, Len (s), 2):#starting with subscript 0, the number of elements returned by the Len () function, and step Step 2    Print(S[i])#output Result: A C e g i k#Step step This concept is a concept that was introduced before learning the subscript in the list#Range function to define the step size of each cycle of the upper and lower bounds, respectively#the list is separated by:#Note that the number produced by range is not capped. forIinchRange (0, 5, 2):    Print(i)#Output: 0,2,4#Enumerate () for(Index, Char)inchEnumerate (s):Print(index, char)" "Output Result: 0 A1 B2 C3 d4 e5 f6 G7 h8 I9 J10 k" "#In the output, the enumerate () function returns a tuple, and then two elements are assigned to the index char#Print the subscript and the elements. Char characters C language more#zip ()m1 = [1, 2, 3]m2= [9, 8, 7]m3= ['a','b','C'] for(A,B,C)inchZip (m1,m2,m3):Print((a,b,c))#Output Result: (1, 9, ' a ')#(2, 8, ' B ')#(3, 7, ' C ')#The zip () function, which is to remove an element from multiple lists, sequentially, merges the elements from the different list into a tuple and returns#function of the zip () function, aggregate list#For the next 3 variables, must be the list inside enough, list inside there are 4 can also, m1= [1,2,3,4]#The output is constant, but for the subsequent change of 4 variables, an error will be#Valueerror:not enough values to unpack (expected 4, got 3)#get only 3 variables, expected to be 4 variablesM1= [A]m2= [9,8,7]zipped= Zip (m1,m2)#zip () extracts an element from the list M1 m2, forming a tuple and assigning it to zippedPrint(*zipped)#Output: (1, 9) (2, 8) (3, 7)list (Zip (m1,m2))#output list:[(1, 9), (2, 8), (3, 7)]#This is the aggregation#exploded list#the ZIP function is inconsistent in Python2 3 .#Python2 Implementation

Then continue to python3 the zip ()

#Python3x= (A) y= (4,5,6) Z= (7,8,9) n=list (Zip (x, y, z))Print(n)#[(1, 4, 7), (2, 5, 8), (3, 6, 9)]m= List (Zip (*N))Print(m)#[(1, 2, 3), (4, 5, 6), (7, 8, 9)]#are implemented by list.#according to official documentsx = (A) y= (4,5,6) zipped=zip (x, y) List (zipped)#[(1, 4), (2, 5), (3, 6)]#Note: Try it yourselfPrint(zipped)<zip Object at 0x0000023a74ed6748>Print(List (zipped)) [(1, 4), (2, 5), (3, 6)]#Continue documentx2, y2 = Zip (*zip (x, y))#here the zip (x, y) cannot be used zipped, although we have previously assigned a value, Python will think that there is not enough value to solveX2#(1, 2, 3)y2#(4, 5, 6)

Python Learning notes (15) Loop design

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.