Python Basics 3-iterators | Slice

Source: Internet
Author: User
Tags iterable shallow copy

There are a lot of python-specific things, very interesting, previously only contact C, C + +, Java, Javascript, did not expect to play like this

#------------------Slice--------------------##slice slices to cut out another array from an arrayLi = List (range (10))Print(LI)#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]#[Start, End, Step] | | (start-end) to be consistent with the sign of stepPrint(Li[2:5])#[2, 3, 4]Print(Li[:4])#[0, 1, 2, 3]Print(li[5:])#[5, 6, 7, 8, 9]Print(Li[0:20:3])#[0, 3, 6, 9]#How about minusPrint(Li[5:-2])#[5, 6, 7]Print(Li[9:0:-1])#[9, 8, 7, 6, 5, 4, 3, 2, 1]Print(Li[9:0:1])# []Print(Li[9::-1])#[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]Print(Li[::-2])#[9, 7, 5, 3, 1]#a new ObjectPrint(LI) re_li= Li[::-1]#[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]Print(Re_li)#------------------Comprehension--------------------##Comprehension Deduction List# Simple CaseLi = [] forIinchRange (10): Li.append (i)Print(LI) Li= List (Range (10)):        Print(LI) Li= [1] * 10Print(LI)#Shallow Copyli_2d = [[0] * 3] * 3Print(li_2d) li_2d[0][0]= 100Print(li_2d)#Deep Copyli_2d = [[0] * 3 forIinchRange (3)]Print(li_2d) li_2d[0][0]= 100Print(li_2d) Li= (x forXinchRange (10))Print(Type (LI))#GeneratorPrint(LI)#Generator Object       forIinchRange (10):#Way1      Print(Next (LI)) forIinchLi#Way2      Print(i) Li= [x forXinchRange (10)]    Print(Type (LI))#ListPrint(LI)#[1, 2, 3, 4]Li= {X forXinchRange (3)}Print(Type (LI))#SetPrint(LI)#{0, 1, 2}s= {X forXinchRange (10)ifx%2==0}Print(Type (s))#SetPrint(s)#{0, 8, 2, 4, 6}s= [x%2==0 forXinchRange (10)]Print(Type (s))#ListPrint(s)#[True, False, Ture, False,ture, False, Ture, False, Ture, false]D= {x:x% 2 = = 0 forXinchRange (10)}Print(Type (d))#ListPrint(d)#{0:true, 1:false, 2:true, 3:false, 4:true, 5:false, 6:true, 7:false, 8:true, 9:false}#So ' x for x in range (Ten) ' is a comprehension#Generator The real calculation until it is used without generating many elements at once, saving memory#2.7 version of a one-time generation of 100W numbers, in the 3.5 version is not really generate 100W digits but the next value is generatedPrint(Type (range (10)))#type#Square TableSquare_table = [] forIinchRange (50000): Square_table.append (i*i) forIinchRange (5):    Print(Square_table[i]) Square_generator= (x * x forXinchRange (50000))Print(Type (square_generator))#Generator forIinchRange (5):    Print(Next (square_generator))deffib (limit): N, a, b= 0, 0, 1 whileN <Limit:yieldb A, b= B, A +B N+ = 1PassImportTRACEBACKF= FIB (5)Print(Type (f))Print(Next (f))Print(Next (f))Print(Next (f))Print(Next (f))Print(Next (f))Try:     Print(Next (f))exceptStopIteration:traceback.print_exc () forIinchFIB (5):    Print(i)#iterable Iterator#Iterative and iterator-like concepts can be iterated to indicate that a for loop can be used, whereas iterators are used to continuously return the next value using next (), using lazy computing#The generator must be an iterator using a build one to see the fib example below fromCollectionsImportiterable fromCollectionsImportIteratorPrint(Isinstance ([iterable])#TruePrint(Isinstance ({}, iterable))#TruePrint(Isinstance (123, iterable))#FalsePrint(Isinstance ('ABC', iterable))#TruePrint(Isinstance ([1, 2, 3], Iterator))#Falseg= (x * x forXinchRange (10))Print(Type (g))#<type ' generator ' >Print(Isinstance (g, iterable))#TruePrint(Isinstance (g, Iterator))#True forIinchg:Print(i)deffib (limit): N, a, b= 0, 0, 1 whileN <Limit:yieldb A, b= B, A +B N+ = 1PassF= FIB (5)Print(Type (f))Print(Isinstance (F, iterable))#TruePrint(Isinstance (F, Iterator))#True forIinchF:Print(i)

Python Basics 3-iterators | Slice

Related Article

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.