字典表可以给方法
def hello_chinese(name):
print('你好:',name)
def hello_english(name):
print('hello:',name)
operation={
'e': hello_english,
'c': lambda name : print ('你好', name)
}
while True:
name = input('请输入姓名:\n')
if name == 'stop':
break
language = input ('请选择语言版本:\n' c: 中文版\n e:英文版\n)
operation. get(language, hello_chinese)(name)
def hello_chinese(name):
print('你好:',name)
def hello_english(name):
print('hello:',name)
def hello(action,name):
action(name)
hello(hello_chinese,'Tom')
hello(lambda name: print('...',name), 'Tom')
l = list(range(1,21))
result = []
//1. 使用循环,效率最低
for n in l:
if n % 2 ==0
rasult.append(n)
//2.使用推导
result = [x for x in l if x%2 == 0]
// 高级工具:map(),返回结果为map类型
def add_num(x):
return x+5
res = list(map(add_num,l) ) //灵活性最高
//若不写list则显示不出来列表
res = list(map(lambda n: n**2,l)
print(result)
filter(函数,可迭代对象)
l = list (range(1,11))
def even_num(x):
return x%2 ==0
res = list(filter(even_number,1))
print(res)
或: res = filter (even_num,1)
for n in res:
print(n,end='')
或:
res =filter(lambda n: n%2 ==0, 1)
推导最好