首页 > 代码库 > Python__内置函数补充
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__内置函数补充
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。