The python iterator generator built-in functions common recursive enumeration

Iterator

Iterator object: Object has __next __ () method is the iterator object, the iterator object dependency __next __ () method in turn values

Open with ( ' named text.txt in ' , ' RB ' ,) AS F: 
    RES = F. __next__ ()   # first line 
    Print (RES) 
    RES = F. __next__ () # the second row of content 
    Print (RES)

 

Builder

In Python, using a function generator is referred to as yield (generator).

The difference is that with an ordinary function, the generator is a return iterator function can only be used to iterate operation, more simply understood generator is a iterator.

During the call generator is running, each encounter yield function pauses and save all of the current operating information, the return value of yield, and the next execution of the next () continue to operate from its current position method.

Calling a generator function, it returns an iterator object.

The following example uses yield achieved Fibonacci columns:

Import SYS 
 
DEF Fibonacci (n-): # generator function - Fibonacci 
    A, B, counter = 0,. 1 , 0
     the while True:
         IF (counter> n-): 
             return 
        the yield A 
        A, B = B, A + B 
        counter + =. 1 
F = Fibonacci (10) # F is an iterator that returns from the generator generates 
 
the while True:
     the try :
         Print (Next (F), End = "  " )
     the except the StopIteration: 
        the sys.exit ()

0 Results performed . 1 . 1 2 . 3 . 5 . 8 13 is 21 is 34 is 55          
 

# Enumeration

It is to be iterator object and add iterator object iteration index

s = 'abn'
for v in enumerate(s):
    print(v)

# 结果
'''
(0, 'a')
(1, 'b')
(2, 'n')
'''

# Send the works

# 1.send occurrence information to stop the current yield 
# 2. calls again the __next __ () method, and then directed downward generator, a yield value and returns the next stop
= persons [ ' Joe Smith ' , ' John Doe ' , ' king five ' , ' Zhao six ' , ' Qian's Seven ' ] 

DEF the Order (persons):
     for i in the Range (len (persons)):
         IF i == 0 :
             Print ( ' % s interview ' % persons [0])
         the else :
             Print ( ' % s% s interview called ' % (name, persons [I]))
         Print (' % Complete interview S ' % persons [I]) 
        name = the yield persons [I] 

obj = Order (persons)
 for I in Range (len (persons)):
     IF I == 0: 
        P . = Obj __next__ ()
     the else : 
        P = obj.send (P)
     Print ( ' ============================= ' ) 

'' ' 
Joe Smith interview 
Joe Smith finished the interview 
============================= 
Zhang Jiao Li four in the interview 
the interview is completed John Doe  
====== =======================
John Doe tempest in five interview 
Wang Wu interview is completed
============================= 
king five Jiaozhao sixth in the interview 
the interview is completed Zhao six 
========== =================== 
Zhao Qian's seven six called in the interview 
finished Qian's seven interview 
==================== ========= 
'' '

Common built-in functions

Format anonymous lambda function: before the colon is the parameter can have multiple, separated by commas, colons to the right of expression. In fact, the return value is the address of a lambda function, which is the function object.

a = lambda x,y,z:(x+8)*y-z
print(a(5,6,8))  

# 1 associated with the type of
# list () str () ord () chr () bool () int () ...

print(ord('A'))
print(chr(97))

# 2 binary conversion
Print (bin (10)) # 1010
Print (OCT (10)) # 12 is
Print (hex (10)) # A

print(0b1111) # 15
print(0o10) # 8
print(0x11) # 17


3. Common Operation class #
# range () len () iter () next () enumerate () id () type () print () input () open ()

# Literal string 4.
Print (R'a \ Nb ')
Print (ASCII (' A \ Nb '))
Print (the repr (' A \ Nb '))


The mathematical correlation operation #
# ABS () SUM () max () min () POW () the sorted ()
Print (ABS (-1))
Print (POW (2,. 3)). 3 # 2 **
Print (POW (2, 3, 3)) ** 2 # 3 3%

 

Recursion

 

Recursive #:
# function calls itself directly or indirectly, are called recursively
# backtracking: the process of looking for answers
# recurrence: results of a process launched

 

# Prerequisites:
# 1. There must be a recursive exit
# 2. recursive backtracking regular recurrence of certain conditions

 

# 递归

def get_age(count):
    if count == 1:
        return 58
    age = get_age(count-1)-2
    return age
age = get_age(3)
print(age)

'''
递归 函数直接或间接调用自身的都是递归 
递归有两个特点  一个是递推  一个是回溯  起始步骤是递推 当递推满足函数第一个return时  递推走完  开始回溯 就是从递推结束的参数往回到原始值
此题中  count最初是3  第一次走完 count为2  第二次走完count为1 此时return 58 递推走完了 
开始回溯 回溯第一次走完count为2  再走一次count为3 此时递归结束  此次过程中有两次是分别是58-2 和56-2   故最终结果是54
'''

 

 

 

 

Guess you like

Origin www.cnblogs.com/wakee/p/11599001.html