table of Contents
dichotomy
#递归实现二分查找 li是列表 item是要查找的元素
def merge_search( li ,item ):
#传来的列表每次都是新生成的,如果发现里面没有元素,则是查找到尽头都没找到
if not li :
return False
mid = len(li)//2 #mid记录li的中间位置
#检查一下 如果中间这个数就是要找的元素 返回真
if li[mid] == item :
return True
# 如果mid比item大,说明item可能会出现在mid左边,对左边再查找
elif li[mid]> item :
return merge_search( li[:mid] ,item )
# mid 比item小,说明item有可能在mid右边,对右边再查找
else :
return merge_search( li[mid+1:] , item )
if __name__ == '__main__':
li = [1,2,3,4,5,6,7]
print( merge_search(li , 0) ) #False
print( merge_search(li , 1) ) #True
Anonymous function
Anonymous function, he has no name binding, ie, once recovered, either bracketed run.
lambda keyword
# 普通python函数
def func(a, b, c):
return a + b + c
print(func(1, 2, 3))
# 返回值为6
# lambda匿名函数
f = lambda a, b, c: a + b + c
print(f(1, 2, 3))
# 返回结果为6
Colon: before a, b, c indicate that they are parameters of this function.
Anonymous function does not need to return to the return value, the return value is the result of the expression itself.
Just a lambda expression, function body is much simpler than def.
Lambda expression is a body, instead of a code block. We can only package a limited logic into the lambda expression.
lambda function has its own namespace, and can not be accessed outside its own parameter list or the global namespace parameters.
Anonymous function advantages
When you use Python to write some scripts that use lambda can save defined functions, make the code more streamlined.
For some abstract, repeat function is not used elsewhere, sometimes a function of a name is also a problem, you do not need to consider using lambda named
Using lambda sometimes easier to understand the code and
Built-in functions
Process-oriented programming
Process-oriented: the core of the process is the word, that is, the process steps to solve the problem, based on process-oriented to design programs like the design, streamlined programming ideas in the design process, the entire process needs to design out an industrial assembly line, is a mechanical way of thinking
Advantages and disadvantages
Advantages: application architecture definition, can simplify complex issues, streamline
Disadvantages: poor scalability, only one flow line is used to solve a problem, so that the process-oriented multi often unwanted software changes