table of Contents
dichotomy
Dichotomy Look for the large amount of data, but the data need to be in good order. The main idea is to find the set :( array interval Array [Low, High])
(. 1) determines the position of the intermediate section K
(2) to find the value of T and array [k] comparison. If equal, look for the successful return of this position; otherwise identify new lookup zones, continue binary search.
Area is determined as follows: array [k]> T consists of an ordered array of apparent array [k, k + 1, ......, high]> T; therefore new interval array [low, ......, k- 1];
If the array [k] <T consists of an ordered array of apparent array [k, k-1, ......, low] <T; therefore new interval array [k + 1, ......, high]
Application: Look for a number in an ordered list
def search(nums, target):
mid_index = len(nums)//2
mid_num = nums[mid_index]
if not nums:
print('not exists!')
if mid_num == target:
print('find it!')
elif mid_num < target:
nums = nums[mid_index+1:]
search(nums,target)
else:
nums = nums[:mid_index]
search(nums, target)
nums = [-1,0,3,5,9,12]
target = 9
search(nums,target)
find it!
Anonymous function
Anonymous function is a function defined by the keyword lambda
# 语法
lambda 参数: 逻辑代码
# 如返回x,y的和
func = lambda x,y:x+y
res = func(1,2)
print(res)
3
Anonymous function becomes ineffective once, generally not used alone, and the maximum value max / min Min / sorted sorting / map mapping / filter filters used in conjunction
Such as: return to the dictionary tallest man wages
# max(iterable,key=func) 将func函数的返回值作为判断的依据,返回iterable中的最大值
saraly_dict = {
'nick':30000,
'egon':100000,
'tank':2000,
'alex':10212
}
res = max(saraly_dict, key=lambda name:saraly_dict[name])
print(res)
egon
Such as: people return to the dictionary lowest wages
# min(iterable,key=func) 将func函数的返回值作为判断的依据,返回iterable中的最小值
saraly_dict = {
'nick':30000,
'egon':100000,
'tank':2000,
'alex':10212
}
res = min(saraly_dict, key=lambda name:saraly_dict[name])
print(res)
tank
Such as: the dictionary sort by salary
# sorted(iterable,key=func) 将func函数的返回值作为判断的依据,对iterable进行从小到大排序
saraly_dict = {
'nick':30000,
'egon':100000,
'tank':2000,
'alex':10212
}
res = sorted(saraly_dict, key=lambda name:saraly_dict[name])
res1 = sorted(saraly_dict, key=lambda name:saraly_dict[name], reverse=True) # 加关键字reverse=True则从大到小排序
print(res)
print(res1)
['tank', 'alex', 'nick', 'egon']
['egon', 'nick', 'alex', 'tank']
Such as: the list of characters plus 'abc'
# map(func,iterable) 将iterable中的每个元素传入到func中执行
lis = ['a', 'b','123']
res = map(lambda x:x+'abc', lis)
print(list(res))
['aabc', 'babc', '123abc']
Such as: the list of elements to 'ed' end screened
# filter(func,iterable) 将iterable中的每个元素传入到func中进行筛选
lis = ['sorted', 'teach', 'jumped']
res = filter(lambda x: x.endswith('ed'), lis)
print(list(res))
['sorted', 'jumped']
Built-in functions
1.bytes () encoded characters, the encoded into the specified character unicode
print(bytes('中国',encoding='utf8'))
b'\xe4\xb8\xad\xe5\x9b\xbd'
Conversion between 2.chr () / ord () and the ASCII code
print(chr(97)) # chr将ASCII码转换成对应字符
print(ord('a')) # ord将字符转换成对应的ASCII码
a
97
3.divmod (x, y) modulo rounding
print(divmod(10,3))
(3, 1)
4.enumerate () Gets the index and value
res = ['a', 'b','c']
for k,v in enumerate(res):
print(k,v)
0 a
1 b
2 c
5.eval () removed the quotation marks from outside the data, which is what marks the data type of data type returned
print(eval('[1,2,3]'))
[1, 2, 3]
6.hash () can not change the hash, the hash is not variable
print(hash('abc')) # 若里面是可变数据类型,则会报错
1361908011752591342
Absolute value 7.abs
print(abs(-10))
10
8.all () is true if the full return True, otherwise False
print(all(['abc',1]))
True
9.any () as long as there is a true will is True, False otherwise
print(any([0,'ab']))
True
10.bin () / oct () / hex () converted to a binary / octal hexadecimal form /
print(bin(17))
print(oct(17))
print(hex(17))
0b10001
0o21
0x11
11.dir () lists all the functions
import time
print(dir(time))
['_STRUCT_TM_ITEMS', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'altzone', 'asctime', 'clock', 'ctime', 'daylight', 'get_clock_info', 'gmtime', 'localtime', 'mktime', 'monotonic', 'perf_counter', 'process_time', 'sleep', 'strftime', 'strptime', 'struct_time', 'time', 'timezone', 'tzname']
12.frozenset () set immutable
print(frozenset([1,3,2]))
frozenset({1, 2, 3})
13.globals () / locals () view the global variables / local variables
14.pow(x,y,z)
print(pow(3,2,4)) # 返回 x**y%z
1
15.round () rounding
print(round(3.4))
3
16.sum () sum
print(sum([1,2,3]))
6
17 .__ import __ () by introducing string module
m = __import__('time')
print(m.time()) # 就是导入了time模块,m.time()就是运行time.time()
1559730202.8626022
Process-oriented programming
According to a certain order, the order of each step can be viewed as functions, this input function is a function of the output, which is called process-oriented programming
advantage:
- Logical, straightforward
- Each function can be independently written
Disadvantages:
- There will be some link between each other, interrupted the previous step, the next step will break the
- There is a change in the function, other functions will be changed
- Poor scalability