首页 > 代码库 > 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__内置函数补充