当前位置:网站首页>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)
边栏推荐
- SQL(二) —— join窗口函数视图
- NodeJs接收上传文件并自定义保存路径
- Understanding and use of C# on set() and get() methods
- "Recursion" recursion concept and typical examples
- What are the characteristics of the interface of the physical layer?What does each contain?
- 位运算符与逻辑运算符的区别
- Dephi reverse tool Dede exports function name MAP and imports it into IDA
- Lecture 2 Linear Model Linear Model
- jvm three heap and stack
- How can Flutter parent and child components receive click events
猜你喜欢
![coppercam primer [6]](/img/d3/a7d44aa19acfb18c5a8cacdc8176e9.png)
coppercam primer [6]

2022 Hangzhou Electric Multi-School 1st Session 01

第二讲 Linear Model 线性模型

SQL(一) —— 增删改查

Qt produces 18 frames of Cupid to express his love, is it your Cupid!!!

flex布局青蛙游戏通关攻略

Flutter learning 2-dart learning

Error creating bean with name 'configDataContextRefresher' defined in class path resource

vscode+pytorch use experience record (personal record + irregular update)

【过一下3】卷积&图像噪音&边缘&纹理
随机推荐
jvm 三 之堆与栈
Flutter学习4-基本UI组件
02.01-----参数的引用的作用“ & ”
【过一下3】卷积&图像噪音&边缘&纹理
2022牛客多校第四场C.Easy Counting Problem(EGF+NTT)
RDD和DataFrame和Dataset
day10-字符串作业
coppercam primer [6]
number_gets the specified number of decimals
Structured light 3D reconstruction (1) Striped structured light 3D reconstruction
Opencv中,imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) 报错:error:!_src.empty() in function ‘cv::cvtColor‘
【cesium】Load and locate 3D Tileset
How can Flutter parent and child components receive click events
jvm three heap and stack
phone call function
Convert the paper official seal in the form of a photo into an electronic official seal (no need to download ps)
【读书】长期更新
Flutter 父子组件如何都能收到点击事件
[Software Exam System Architect] Software Architecture Design ③ Domain-Specific Software Architecture (DSSA)
小白一枚各位大牛轻虐虐