Python__内置函数补充
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python__内置函数补充相关的知识,希望对你有一定的参考价值。
#Author wangmengzhu
salaries = {
‘egon‘:3000,
‘alex‘:100000,
‘wupeiqi‘:1000,
‘yuanhao‘:2000
}
# print(list(zip(salaries.values(),salaries.keys())))
# print(max(list(zip(salaries.values(),salaries.keys()))))
# print(max(salaries,key = lambda name:salaries[name]))
##filter,map,reduce
names = [‘alex‘,‘wupeiqi‘,‘yuanhao‘,‘egon‘]
# res = map(lambda x:x + ‘_SB‘,names)
# print(list(res))
#从functools中导入reduce模块
from functools import reduce
# print(reduce(lambda x,y:x + y,range(101)))
def my_map(seq):
for item in seq:
item = item + ‘_SB‘
yield item
res1 = my_map(names)
# print(next(res1))
def my_map(func,seq):
for item in seq:
yield func(item)
# res1 = my_map(lambda x:x + ‘_SB‘,names)
# print(next(res1))
##filter函数
names = [‘alex_SB‘,‘wupeiqi‘,‘yuanhao‘,‘egon‘]
# print(list(filter(lambda name:name.endswith(‘SB‘),names)))
##eval与exec
# cmd = ‘print(x)‘
# x = 1
# eval(cmd)
# eval(cmd,{},{})#第一个大括号表示的是全局作用域,第二个大括号表示的是局部作用域
# eval(cmd,{‘x‘:0},{‘y‘:10000})
s = ‘for i in range(10):print(i)‘
code = compile(s,‘‘,‘exec‘)
exec(code)
以上是关于Python__内置函数补充的主要内容,如果未能解决你的问题,请参考以下文章
python3全栈开发-内置函数补充,反射,元类,__str__,__del__,exec,type,__call__方法
python基础-内置函数-作用域-闭包-递归-python3