Generator, built-in functions and expression

Builder

1. What is a generator? Generation tool. Builder is a "Custom" iterator is an iterator nature.

2. How to achieve generator whenever the internal function defines the yield, when you call the function, the function body code is not executed, returns a result that is a generator.

yield: Every time yield added value will be reborn in a generator object. - yield only inside the function definition - yield function can save the state of pause

yield and return: the same point: Returns the value of the number is unlimited.

Different points: return only return a value, yield can return multiple values

Python iterator object built generated iterator

iter_list = list1.__ iter __()

# Custom iterator

def func():

print('from func')

yield 'tank'

res = func()

# When we value by __ next __, will perform the function body code.

print(res.__ next __())

10 cycles 

for I in Range (1,11 ): 
Print (I)         # . 1 ~ ~ 10 

python2: Range ( l, 5) ---> [. 1, 2,. 3,. 4 ] 

to python3: Range ( . 1, 5) ---> range objects ---> generator ----> iterator 

RES = Range (l, 5 ) 

Print (RES)
 # Custom function range, create a custom generator 

 # (. 1,. 3) #start ->. 1, End --->. 5, Move = 2 

 DEF My_Range (Start, End, Move =. 1 ):
 the while Start < End
 the yield Start
 
 Start + = Move

Process-oriented programming

Process-oriented programming is a programming ideas.

Process-oriented programming: the core is the 'process' word, refers to the process is a problem-solving steps, which is to do and then do write programs based on the programming ideas, like the design of a factory assembly line, a mechanical way of thinking .

advantage:

The complex issue processes, thus simplifying

Disadvantages:

If a certain part of the current program design modifications, it will lead to other parts need to be modified at the same time, poor scalability.

A triplet of expressions

A triplet of expressions:

if ... else ... can turn into a branch line.

grammar:

Condition holds return value if left to determine the conditions else condition does not hold the right of return value

if the determination condition: 

performing 

the else : 

performing 



find two magnitude values 

by the syntax if ... else 

DEF max (num1, num2): 
if num1> num2: 
return num1 
the else : 
return num2 

RES = max ( 100,20 ) 

Print (RES)

 > 100

A triplet of expressions

def max1(num1,num2):
    res = num1    if num1 > num2 else num2
    return res
res = max1(num1,num2)
print(res)

List of formula:

You can generate a list of his party achieved. Syntax: = [a value taken for each, for each arbitrary value of an object may be taken out in an iterative iterables] list

It is right for the number of cycles, and may be taken out of each value iterables

for the left can add value to the current list

list = [a value for each iteration may be withdrawn in the object iterable]

list = [value for each value of the iteration can be retrieved object if the object is determined in the iteration may be]

The values in the list, sequentially taken out, added to the new_list 
list = [. 1, 2,. 3,. 4 ] 
new_list = []
 for Line in List1: 
    new_list.append (Line) 
Print (new_list) 

# conventional manner 
new_list = []
 for Line in Range (1,101 ): 
    new_list.append (Line) 
Print (new_list The) 

# list generator 
list = [F ' . 1} {Line '  for Line in Range (1,101 )]
 Print (list)

Generator formula

  List formula: If the amount of data using the hour [line for line in range (1 , 6)] ---> [1, 2, 3, 4, 5]

   advantage:

     May depend on the index value, convenient value

   Disadvantages:

     a waste of resource

  • Builder formula: If using large amount of data () ---> Returns generator (line for line in range (1 , 6)) ---> g generators (1, 2, 3, 4, 5 )

  advantage:

    save resources

  Disadvantages:

     The value is not convenient

# Generates a generator 1000 values 
G = (Line for Line in Range (. 1, 1000001 ))
 # <Generator Object <genexpr> AT 0x00000203262318E0> 
Print (G) 

# listing formula achieve 
List1 = [Line for Line in Range (. 1, 1000001 )]
 Print (List1)

Built-in functions

range()

print()

Only ()

Built-in method of providing internal #python

max,min,sorted,map,filter

sorted: Sort of iterables

 

selecting the maximum value max max (iterables)

list = [1, 2, 3, 4, 5]

Max inner value will be a list of values ​​for each extracted for, and the determination

print(max(list))

dict1 = {
    'tank': 1000,
    'egon': 500,
    'sean': 200,
    'jason': 500
}

# 获取dict1中薪资最大的人的名字
# 字符串的比较: ASCII
print(max(dict1, key=lambda x: dict1[x]))


# 获取dict1中薪资最小的人的名字
print(min(dict1, key=lambda x:dict1[x]))
##### sorted: 默认升序(从小到大) reverse:反转 reverse默认是False

list1 = [10, 2, 3, 4, 5]
print(sorted(list1))

##### reverse=True--> 降序

print(sorted(list1, reverse=True))

匿名函数

  无名字的函数

    :左边是参数, 右边是返回值

  lambda :

    PS: 原因,因为没有名字,函数的调用 函数名 + () 匿名函数需要一次性使用。

     注意: 匿名函数单独使用毫无意义,它必须配合 “内置函数” 一起使用的才有意义

有名函数

def func():
    return 1
print(func())  # func函数对象 + ()
print(func())

匿名函数

def  ():
    pass

()  #  + ()
lambda 匿名(): return 1
func = lambda : 1
print(func())
func = lambda : 1
print()

Guess you like

Origin www.cnblogs.com/lvguchujiu/p/11857872.html