day19 process-oriented programming, functions and modules anonymous

1. dichotomy

1.1 What is an algorithm

Algorithm is an efficient solution to the problem

1.2 dichotomy

# 需求:有一个按照从小到大顺序排列的数字列表
#      需要从该数字列表中找到我们想要的那个一个数字
#      如何做更高效???
nums=[-3,4,7,10,13,21,43,77,89]
find_num=10

nums=[-3,4,13,10,-2,7,89]
nums.sort()
print(nums)

# 方案一:整体遍历效率太低
for num in nums:
    if num == find_num:
        print('find it')
        break
# 方案二:二分法
nums=[-3,4,7,10,13,21,43,77,89]
find_num=8
def binary_search(find_num,l):
    print(l)
    if len(l) == 0:
        print('找的值不存在')
        return
    mid_index=len(l) // 2

    if find_num > l[mid_index]:
        # 接下来的查找应该是在列表的右半部分
        l=l[mid_index+1:]
        binary_search(find_num,l)
    elif find_num < l[mid_index]:
        # 接下来的查找应该是在列表的左半部分
        l=l[:mid_index]
        binary_search(find_num,l)
    else:
        print('find it')

binary_search(find_num,nums)

2. The process-oriented programming

2.1 programming ideas / paradigm

Programming ideas refers to the programming routines, programming ideas itself and no ranking points, but rather the users themselves

Process-oriented programming ideas 2.2

  • The core is the "process" the word, that is, the process flow, referring to the steps of doing things: what first, and then what, after what
  • Based on programming like the idea in the design of an assembly line

2.3 advantages and disadvantages

advantage:

Complex flow problem, and then simplify

Disadvantages:

Scalability is very poor

2.4 Process-oriented programming ideas to resolve scenarios

  • Not all software requires frequent changes: for example, write a script
  • Even a software requires frequent changes, nor does not mean that all the software components of the change are required together

3. anonymous function

3.1 Definitions anonymous function

  • def for defining a named function

    func=函数的内存地址
    def func(x,y):
        return x+y
    
  • lamdab for defining anonymous functions

    print(lambda x,y:x+y)
    

3.2 anonymous call function

A less common way :()

res=(lambda x,y:x+y)(1,2)
print(res)

Second way :( can be used, but does not make sense)

func=lambda x,y:x+y
res=func(1,2)
print(res)

Anonymous calls for a temporary scene: more anonymous is used in conjunction with other functions

Application 3.3 anonymous function

Application of the method 3.3.1 max

salaries={
    'siry':3000,
    'tom':7000,
    'lili':10000,
    'jack':2000
}
# 需求1:找出薪资最高的那个人=》lili
res=max([3,200,11,300,399])
print(res)

res=max(salaries)
print(res)#比较的是key的值
#为此我们可以定义一个函数,来表示值
def func(k):
    return salaries[k]
res=max(salaries,key=func) # 返回值=func('siry')
print(res)
#lambda k:salaries[k],以这个标准来执行
res=max(salaries,key=lambda k:salaries[k])
print(res)

Application of the method of 3.3.2 min

# ========================min的应用
res=min(salaries,key=lambda k:salaries[k])
print(res)

Application 3.3.3 sorted methods

res=sorted(salaries,key=lambda k:salaries[k],reverse=True)
print(res)

3.3.4 application map method (understand)

l=['alex','lxx','wxx','薛贤妻']
new_l=(name+'_dsb' for name in l)
print(new_l)

res=map(lambda name:name+'_dsb',l)
print(res) # 生成器

Application 3.3.5 filter method (understand)

l=['alex_sb','lxx_sb','wxx']
res=(name for name in l if name.endswith('sb'))
print(res)

res=filter(lambda name:name.endswith('sb'),l)
print(res)

3.3.6 reduce application methods (understanding)

from functools import reduce
res=reduce(lambda x,y:x+y,[1,2,3],10) # 16
print(res)

res=reduce(lambda x,y:x+y,['a','b','c']) # 'a','b'
print(res)

4. Module

4.1 What is a module

A module is a collection of a series of functions, divided into three categories

  • I: built-in module
  • II: third-party modules
  • III: custom module

A python file itself is a module, file name m.py, module called m

ps:模块有四种形式
   1 使用python编写的.py文件
   2 已被编译为共享库或DLL的C或C++扩展        
   3 把一系列模块组织到一起的文件夹(注:文件夹下有一个__init__.py文件,该文件夹称之为包)       
   4 使用C编写并链接到python解释器的内置模块

4.2 Why do you use module

  • Built and brought on by the third module, no need to define this ism, can greatly improve their development efficiency
  • Custom modules
    function of each part of the program can be extracted into a module for everyone shared
    the benefits of reducing code redundancy, the organizational structure of the program clearer

4.3 How to module

4.3.1 import statement

#文件名:foo.py
x=1
def get():
    print(x)
def change():
    global x
    x=0
class Foo:
    def func(self):
       print('from the func')
#其他文件
import foo #导入模块foo
a=foo.x #引用模块foo中变量x的值赋值给当前名称空间中的名字a
foo.get() #调用模块foo的get函数
foo.change() #调用模块foo中的change函数
obj=foo.Foo() #使用模块foo的类Foo来实例化,进一步可以执行obj.func()

To quote function foo.py another py in the file, you need to use import foo,

Import module for the first time would do three things:

1, execute files in the source foo.py

2, resulting in a foo.py name space for the name of the stored procedure execution generated source file

3, get a name in the namespace foo execute the current file is located, the name of the module namespace pointing to the newly created module namespace references to the name, we need to add the prefix

Note: After import, are the first direct reference foo.py namespaces imported produce, and will not repeat code execution

4.3.2 references

print(foo.x)
print(foo.get)
print(foo.change)
#强调1:模块名.名字,是指名道姓地问某一个模块要名字对应的值,不会与当前名称空间中的名字发生冲突
x=1111111111111
print(x)
print(foo.x)

#强调2:无论是查看还是修改操作的都是模块本身,与调用位置无关
import foo

x=3333333333
# foo.get()
foo.change()
print(x)

print(foo.x)
foo.get()

Import module specification 4.3.3

Comma Separated Values ​​may be introduced into a plurality of modules in a row, may be introduced in the function block

# 建议如下所示导入多个模块
# import time
# import foo
# import m

# 不建议在一行同时导入多个模块
import time,foo,m

Sequence introduced

  • I. python built-in module

  • II. Third-party modules

  • III. Programmer-defined module

    Since naming a custom module should be lowercase + underscore pure style

import time
import sys

import 第三方1
import 第三方2

import 自定义模块1
import 自定义模块2
import 自定义模块3

4.3.4 module may alias as

import foo as f # f=foo
f.get()
import abcdefgadfadfas as mmm
mmm.f1

4.3.5 module is a first-class objects

Which can be introduced into the assignment of the modules, it can be used as transmission parameters, return value can be done, and the like can be used as elements of the container

Guess you like

Origin www.cnblogs.com/Henry121/p/12575932.html