递归--二分法
# 普通二分法
def binary_search(li,values):
low = 0
height = len(li) - 1
while count <= height:
ind = (low + heigth) // 2
if li[ind] > values:
height = ind - 1
elif li[ind] < values:
low = ind + 1
else:
return ind
# 递归二分法
def search(search_num, nums):
mid_index = len(nums)//2
print(nums)
if not nums:
print('not exists')
return
if search_num > nums[mid_index]:
# in the right
nums = nums[mid_index+1:]
search(search_num, nums)
elif search_num < nums[mid_index]:
# in the left
nums = nums[:mid_index]
search(search_num, nums)
else:
print('find it')
有名函数
def f1():
pass
匿名函数
语法
res = (lambda x,y:x**y)(参数1,参数2)
print(res)
1. max()配合函数的一种用法:
# 有名函数
salary_dict = {
'nick': 3000,
'jason': 100000,
'tank': 5000,
'sean': 2000
}
def func(res):
return salary_dict[res]
max_num_key = max(salary_dict,key=func)
print(max_num_key)
'''
jason
'''
# 匿名函数
max_num_key = max(salary_dict,key = lambda key:salary_dict[key])
print(max_num_key)
# 列表求出最大值
nums_list = [2324,1231,1,0,1314,53634,234,14,15124]
res = max(nums_list)
print(res)
2. min()同max类似
3. sorted() 排序
a = [199,444,231,15156,2,1561,9987655,12312415,252352341,33,1,0,324232,2323423]
new_list = sorted(a,key = lambda x:x,reverse=True) # 返回的是一个迭代对象
print(list(new_list))
'''
[252352341, 12312415, 9987655, 2323423, 324232, 15156, 1561, 444, 231, 199, 33, 2, 1, 0]
'''
4. map() 映射
name_list = ['panlifu','lt','xiesha','chendiao','helloworld']
x = map(lambda x:'%s haha'%x,name_list)
print(list(x))
'''
['panlifu haha', 'lt haha', 'xiesha haha', 'chendiao haha', 'helloworld haha']
'''
5. filter() 过滤
name_list = ['panlifu','lt','xiesha','chendiao','helloworld']
x = filter(lambda x:x.endswith("d"),name_list)
print(list(x))
'''
['helloworld']
'''
内置函数
掌握
1. bytes()
res = '你好'.encode('utf8')
print(res)
'''
b'xe4xbdxa0xe5xa5xbd'
'''
res = bytes('你好', encoding='utf8')
print(res)
'''
b'xe4xbdxa0xe5xa5xbd'
'''
2. chr()/ord() chr()参考ASCII码表将数字转成对应字符;ord()将字符转换成对应的数字。
print(chr(65))
'''
A
'''
print(ord('A'))
'''
65
'''
3. divmod 取整,取余
print(divmod(10, 3)) # 10//3 10%3
'''
(3,1)
'''
4. enumerate 枚举,带有索引的迭代
a = ['a','b','c','d','e','f']
for i,value in enumerate(a):
print(i,value)
# 通过枚举和函数结合
def f1():
print("from f1")
def f2():
print("from f2")
list_func = [f1,f2]
for i,value in enumerate(list_func):
print(i,value.__name__)
5. eval() 把字符串转列表,去掉字符串的引号,然后它是什么数据eval就是什么数据类型
a = '[1,23,5,1,1321,5123,151]'
b = eval(a)
print("b的类型为:%s 值为%s"%(type(b),b))
'''
b的类型为:<class 'list'> 值为[1, 23, 5, 1, 1321, 5123, 151]
'''
a = '2'
b = eval(a)
print("b的类型为:%s 值为%s"%(type(b),b))
'''
b的类型为:<class 'int'> 值为2
'''
'''
总结:
1. 去掉字符串的引号,然后他是什么数据eval就是什么数据类型
'''
6. hash() 可哈希 不可变,不可哈希可变
print(hash(1))
了解
1. abs() 绝对值
print(abs(-13)) # 求绝对值
'''
13
'''
2. all() 全部为真,返回true,否则为false
print(all([1, 2, 3, 0]))
print(all([]))
'''
False
True
'''
3. any() 一个为真,返回true,否则为false
print(any([1, 2, 3, 0]))
print(any([]))
'''
True
False
'''
4. bin()/oct()/hex() 二进制、八进制、十六进制转换。
print(bin(17))
print(oct(17))
print(hex(17))
'''
0b10001
0o21
0x11
'''
5. dir 把模块所有的方法读出来
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', 'tzset']
'''
6. frozenset() 不可变集合
s = frozenset({1, 2, 3})
print(s)
'''
frozenset({1, 2, 3})
'''
7. globals() / locals() 查看全局名字;查看局部名字
# print(globals())
def func():
a = 1
# print(globals())
print(locals())
func()
'''
{'a': 1}
'''
8. pow()
print(pow(3, 2, 3)) # (3**2)%3
'''
0
'''
9. round()
print(round(3.5))
'''
4
'''
10. slice()
lis = ['a', 'b', 'c']
s = slice(1, 4, 1)
print(lis[s]) # print(lis[1:4:1])
'''
['b', 'c']
'''
11. sum()
print(sum(range(100)))
'''
4950
'''
12. import()
m = __import__('time')
print(m.time())
'''
1556607502.334777
'''