1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 4 " "#迭代器5 iteration is one of the most powerful features of Python and is a way to access the elements of a collection. 6 An iterator is an object that remembers where to traverse. 7 The iterator object is accessed from the first element of the collection until all of the elements have been accessed and finished. Iterators can only move forward without backing back. 8 iterators have two basic methods: ITER () and next (). 9 a string, list, or tuple object can be used to create an iteratorTen 01. All objects that can be used for a for loop are iterable types One 02. All objects that can be used for the next () function are iterrator types, which represent a sequence of lazy computations A 03. Collection data types such as list, dict, str, etc. are iterable but not iterator, but can be - The iter () function obtains a iterator object. - " " the fromCollectionsImportiterable, Iterator - - defG ():#Define a generator - yield1 + yield2 - yield3 + A #Judging Iterable and iterator methods at Print('iterable? [1, 2, 3]:', Isinstance ([1, 2, 3], iterable)) - Print('iterable \ ' abc\ ':', Isinstance ('ABC', iterable)) - Print('iterable? 123:', Isinstance (123, iterable)) - Print('iterable g ():', Isinstance (g (), iterable))#iterable g (): True - - in Print('Iterator? [1, 2, 3]:', Isinstance ([1, 2, 3], Iterator)) - Print('Iterator iter ([1, 2, 3]):', Isinstance (ITER ([1, 2, 3]), Iterator)) to Print('Iterator \ ' abc\ ':', Isinstance ('ABC', Iterator)) + Print('Iterator? 123:', Isinstance (123, Iterator)) - Print('Iterator g ():', Isinstance (g (), Iterator))#Iterator g (): True the * #the generator is both iterable and iterator $ Panax Notoginseng #iter list: - Print('For x in [1, 2, 3, 4, 5]:') the forXinch[1, 2, 3, 4, 5]: + Print(X,end =" ")#1 2 3) 4 5 A Print("") the + #Iterator list - Print('for X in ITER ([1, 2, 3, 4, 5]):') $ forXinchITER ([1, 2, 3, 4, 5]): $ Print(X,end =" ")##1 2 3 4 5 - Print("") - the - Print('Next ():') Wuyiit = iter ([1, 2, 3, 4, 5]) the Print(Next (IT)) - Print(Next (IT)) Wu Print(Next (IT)) - Print(Next (IT)) About Print(Next (IT)) $ - -D = {'a': 1,'b': 2,'C': 3} - A + #iter each key: the Print('iter key:', D) - forKinchD.keys (): $ Print('Key:', K, end=" ")#key:a key:b key:c the Print("") the the the #iter each value: - Print('iter value:', D) in forVinchd.values (): the Print('Value:', V, end=" ")#value:1 value:2 Value:3 the Print("") About the the #iter both key and value: the Print('ITER Item:', D) + forKvinchD.items (): - Print('Item:', K, V, end=" ")#item:a 1 item:b 2 item:c 3 the Print("") Bayi the the #iter list with index: - Print('iter enumerate ([\ ' a\ ', \ ' b\ ', \ ' c\ ')') - forI, ValueinchEnumerate (['A','B','C']): the Print(I, Value,end =" ")#0 A 1 B 2 C the Print("") the the - #iter complex list: the Print('iter [(1, 1), (2, 4), (3, 9)]:') the forX, yinch[(1, 1), (2, 4), (3, 9)]: the Print(x, y, end =" ")#1 1 2 4 3 994 Print("") the theList = [1,2,3,4] theit = ITER (list)#creating an Iterator object98 Print(Next (IT))#the next element of the output iterator About Print(Next (IT)) - 101List = [1,2,3,4]102 forXinchiter (list):103 Print(x, end =" ")#traversing with A For loop statement104 Print("") the 106 #use the next () function107 ImportSYS108list=[1,2,3,4]109it =iter (list) the 111 whileTrue: the Try:113 Print(Next (IT), end=" ") the exceptstopiteration: the sys.exit () the Print("")
Python Learning---iterators