函数名 = lambda 参数列表:返回值
相当于:
def 函数名(参数列表):
return 返回值
sum1 = lambda x,y:x+y
print(sum1(1,2))
get_gewei = lambda x: x % 10
print(get_gewei(232))
# a、b、c都是全局变量
a = 100
for b in range(10):
print(b)
c = 20
print(f'循环里面使用a:{a}')
print(f'循环外面使用b和c:{b},{c}')
def func2(x):
y = 100
for z in range(10):
pass
print(x,y,z)
print("函数结束")
func2(10)
## print(x,y,z) 函数报错x,y,z没有被定义
m = 100
def func1():
global m
print(m)
m = 200
global n
n = 20
print(n)
func1()
print(m)
func1 = lambda x: x*2
print(type(func1))
##
def func2():
print("函数")
print(type(func2))
##
c = func2
c()
def func1O():
def func11():
print('hello')
return 100
return func11
print(func1O()())
max,min,sorted,sort,map,reduce
max(序列)/max(序列,key=函数)
函数要求:有且只有一个参数,参数指向的是前面序列中的每个元素,有一个返回值(返回值就是比较对象)
nums = [89, 32, 52, 65, 21, 90]
print(max(nums, key=lambda x: x % 10))
nums = [89, '32', 52, '65', 21, 90]
print(max(nums, key=lambda x: int(x)))
'''c.获取年龄最大的学生信息;获取成绩最低的学生信息'''
students = [
{'name': '小明', 'age': 18, 'score': 90},
{'name': '老王', 'age': 28, 'score': 67},
{'name': '张三', 'age': 22, 'score': 83},
{'name': '李四', 'age': 25, 'score': 57}
]
print("获取年龄最大的学生信息", max(students, key=lambda x: x['age']), sep=':')
print("获取成绩最低的学生信息", min(students, key=lambda x: x['score']), sep=':')
'''d.获取nums各个位数的和的最大的元素'''
nums = [123, 97, 56, 109, 82]
def sum_gewei(x):
sum1 = 0
while x:
sum1 += x % 10
x //= 10
return sum1
print(max(nums, key=sum_gewei))
nums = [23,45,78,91,56]
print(list(map(lambda item:str(item),nums)))
nums1=[1,2,3,4,5]
nums2=[6,7,8,9,1]
'''
[16,27, 38,49,51]
'''
result = map(lambda i1,i2: i1 * 10 + i2, nums1, nums2)
print(list(result))
map(函数,序列1,序列2)
函数要求:a.有两个参数,第一个参数指向序列1中的元素,第二个参数指向序列2中的元素
b.需要一个返回值(新序列中的元素,描述清楚新序列元素和原序列元素的关系)
函数后面可以有N个序列,要求这N个序列中元素个数必须一致;有多少个序列
练习:
['小明', '张三', '李四', '老王']
[18, 30, 26, 35]
->
[
{'name': '小明', 'age': 18},
{'name': '张三', 'age': 30},
...
]
list1 = ['小明', '张三', '李四', '老王']
list2 = [18, 30, 26, 35]
result = map(lambda i1,i2:{'name':i1,'age':i2},list1,list2)
print(list(result))
nums = [1,2,3,4,5]
result = reduce(lambda x,y:x+y, nums, 0)
print(result)
result1 = reduce(lambda x,y:x*y, nums, 1)
print(result1)
result2 = reduce(lambda x,y:x+str(y),nums,'')
print(result2)
students = [
{'name': '小明', 'age': 18, 'score': 90},
{'name': '老王', 'age': 28, 'score': 67},
{'name': '张三', 'age': 22, 'score': 83},
{'name': '李四', 'age': 25, 'score': 57}
]
## 将列表中的元素合并成'小明老王张三李四'
result = reduce(lambda x,y:x + y['name'],students,'')
print(result)
## 将列表中的元素合并成 ['小明','老王','张三','李四']
# result1 = reduce(lambda x,y:x+y['name'],students,'')
# list1 = []
# for item in range(0,len(result1),2):
# list1.append(result1[item:item+2])
# print(list1)
result1 = reduce(lambda x,y:x+[y['name']],students,[])
print(result1)
points = [
(10, 20), (0, 100), (20, 30), (-10, 20), (30, -100)
]
以下问题使用实参高阶函数来解决
result1 = max(points, key=lambda x:x[1])
print(result1)
result2 = min(points, key=lambda x:x[0])
print(result2)
result3 = max(points, key=lambda x:(x[0]**2 + x[1]**2)**0.5)
print(result3)
result4 = sorted(points, key=lambda x:(x[1]**2),reverse=True)
print(result4)
nums = [2,3,8,-9,5]
result2 = max(nums,key= lambda item : (item**2))
print(result2)
A = ['name', 'age', 'sex']
B = ['张三', 18, '女']
新字典: {'name': '张三', 'age': 18, 'sex': '女'}
A = ['name', 'age', 'sex']
B = ['张三', 18, '女']
result3 = map(lambda x,y: (x,y), A, B)
print(dict(result3))
names = ['小明', '小花', '小红', '老王']
nums = ['1906', '1807', '2001', '2004']
subjects = ['python', 'h5', 'java', 'python']
结果:{'小明': 'python1906', '小花': 'h51807', '小红': 'java2001', '老王': 'python2004'}
names = ['小明', '小花', '小红', '老王']
nums = ['1906', '1807', '2001', '2004']
subjects = ['python', 'h5', 'java', 'python']
result4 = map(lambda x,y,z: (x,z+y), names, nums,subjects)
print(dict(result4))
message = ['你好', 20, '30', 5, 6.89, 'hello']
结果:31.89
result5 = reduce(lambda item1,item2:item1+(item2 if type(item2) in [int,float] else 0) ,message,0)
print(result5)
result6 = reduce(lambda item1,item2:item1+item2,[x for x in message if type(x) in [int,float]],0)
print(result6)
studens = [
{'name': 'stu1', 'math': 97, 'English': 67, 'Chinese': 80},
{'name': 'stu2', 'math': 56, 'English': 84, 'Chinese': 74},
{'name': 'stu3', 'math': 92, 'English': 83, 'Chinese': 78},
{'name': 'stu4', 'math': 62, 'English': 90, 'Chinese': 88}
]
# 计算平均分
studens = [
{'name': 'stu1', 'math': 97, 'English': 67, 'Chinese': 80, 'avg':81},
{'name': 'stu2', 'math': 56, 'English': 84, 'Chinese': 74, 'avg':71},
{'name': 'stu3', 'math': 92, 'English': 83, 'Chinese': 78, 'avg':87},
{'name': 'stu4', 'math': 62, 'English': 90, 'Chinese': 88, 'avg':80}
]
# 按照平均分从高到低排序
...
from functools import reduce
for student in studens:
result = (reduce(lambda x,y:x+y,list(student.values())[1:],0))//3
student['avg'] = result
print(studens)
result1 = sorted(studens,key= lambda x:x['avg'],reverse=True)
print(result1)