习题:
用 lambda 和 filter 完成下面功能: 输出一个列表, 列表里面包括: 1-100 内的所有偶数.(提示: 可以用 filter,lambda)
- [ x for x in range(1,101) if x%2==0]
- def func(x):
- return x % 2 == 0
- new_list = list(filter(func, range(1, 101)))
- print(new_list)
用位置匹配, 关键字匹配, 收集匹配 (元组收集, 字典收集) 分别写 4 个函数, 完成功能;
传递 3 个列表参数:
[1,2,3],[1,5,65],[33,445,22]
返回这 3 个列表中元素最大的那个, 结果是: 445
- def get_max(list1, list2, list3):
- return max(list1+list2+list3)
- print get_max([1,2],[3,4],[67,9])
- def get_max2(list1=[], list2=[], list3=[]):
- return max(list1+list2+list3)
- print get_max2([1,2],[3,4],[67,9])
- def get_max3(*list):
- a = []
- for x in list:
- a.extend(x)
- return max(a)
- print(get_max3([1,2,3],[4,6,8]))
- def get_max4(**list):
- new_list = []
- for x in list.values():
- new_list.extend(x)
- return max(new_list)
- print(get_max4(list1=[1,2,3],list2=[4,6,8]))
来源: http://www.bubuko.com/infodetail-2668583.html