当前位置:网站首页>day12函数进阶作业
day12函数进阶作业
2022-08-05 05:13:00 【非鱼丶丶】
写一个匿名函数,判断指定的年是否是闰年
judge_leap_year = lambda item: '闰' if (item % 100 != 0 and item % 4 == 0) or (item % 400 == 0) else '平' print(judge_leap_year())写一个函数将一个指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使用列表自带的逆序函数)
def reverse_1(list1: list): list_new = [] while True: a = max(list1) list_new.append(a) list1.remove(a) if not list1: break return list_new编写一个函数,计算一个整数的各位数的平方和
例如: sum1(12) -> 5(1的平方加上2的平方) sum1(123) -> 14def quadratic_sum(num): sum1 = 0 str1 = str(num) for x in str1: sum1 += int(x) ** 2 return sum1求列表 nums 中绝对值最小的元素
例如:nums = [-23, 100, 89, -56, -234, 123], 最大值是:-23
nums = [-23, 100, 89, -56, -234, 123]
res1 = min(nums, key=lambda item: abs(item))
print(res1)
- 已经两个列表A和B,用map函数创建一个字典,A中的元素是key,B中的元素是value
A = ['name', 'age', 'sex']
B = ['张三', 18, '女']
新字典: {
'name': '张三', 'age': 18, 'sex': '女'}
A = ['name', 'age', 'sex']
B = ['张三', 18, '女']
res4 = map(lambda i1, i2: {
i1: i2}, A, B)
print(list(res4))
已经三个列表分别表示5个学生的姓名、学科和班号,使用map将这个三个列表拼成一个表示每个学生班级信息的的字典
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'] res4 = map(lambda i1, i2, i3: { 'name': i1, 'number': i2, 'subjects': i3}, names, nums, subjects) print(list(res4))已经一个列表message, 使用reduce计算列表中所有数字的和
message = ['你好', 20, '30', 5, 6.89, 'hello'] 结果:31.89message = ['你好', 20, '30', 5, 6.89, 'hello'] from functools import reduce res =reduce(lambda i, item: i + item if type(item) == int or type(item) == float else i + 0, message, 0) print(res)已经列表points中保存的是每个点的坐标(坐标是用元组表示的,第一个值是x坐标,第二个值是y坐标)
points = [ (10, 20), (0, 100), (20, 30), (-10, 20), (30, -100) ]1)获取列表中y坐标最大的点
2)获取列表中x坐标最小的点
3)获取列表中距离原点最远的点
4)将点按照点到x轴的距离大小从大到小排序
points = [
(10, 20), (0, 100), (20, 30), (-10, 20), (30, -100)
]
1)
res = max(points, key=lambda x: x[1])
print(res)
2)
res1 = min(points, key=lambda x: x[0])
print(res)
3)
res2 = max(points, key=lambda x: x[0] ** 2 + x[1] ** 2)
print(res)
4)
res3 = sorted(points, key=lambda x: abs(x[0]), reverse=True)
print(res)
边栏推荐
- Transformation 和 Action 常用算子
- Returned object not currently part of this pool
- [cesium] 3D Tileset model is loaded and associated with the model tree
- Flutter学习-开篇
- WPF中DataContext作用
- 第二讲 Linear Model 线性模型
- 【过一下3】卷积&图像噪音&边缘&纹理
- C#关于set()和get()方法的理解及使用
- Lecture 2 Linear Model Linear Model
- u-boot debugging and positioning means
猜你喜欢
随机推荐
【过一下15】学习 lstm的一周
Difference between for..in and for..of
1068找到更多的硬币
【过一下14】自习室的一天
Mesos学习
【过一下3】卷积&图像噪音&边缘&纹理
Redis - 13. Development Specifications
server disk array
将照片形式的纸质公章转化为电子公章(不需要下载ps)
第四讲 back propagation 反向传播
Flutter learning 5-integration-packaging-publish
How to quickly upgrade your Taobao account to a higher level
结构光三维重建(二)线结构光三维重建
重新审视分布式系统:永远不会有完美的一致性方案……
uva1325
u-boot debugging and positioning means
Lecture 2 Linear Model Linear Model
u-boot in u-boot, dm-pre-reloc
MySQL Foundation (1) - Basic Cognition and Operation
[Go through 3] Convolution & Image Noise & Edge & Texture









![[cesium] element highlighting](/img/99/504ca9802db83eb33bc6d91b34fa84.png)