当前位置:网站首页>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)
边栏推荐
猜你喜欢

OFDM Lecture 16 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems

flex布局青蛙游戏通关攻略

CAP+BASE

【过一下9】卷积

Qt制作18帧丘比特表白意中人、是你的丘比特嘛!!!

Structured Light 3D Reconstruction (2) Line Structured Light 3D Reconstruction

Dephi逆向工具Dede导出函数名MAP导入到IDA中

Flutter学习5-集成-打包-发布

Structured light 3D reconstruction (1) Striped structured light 3D reconstruction

shell函数
随机推荐
The role of the range function
range函数作用
Cryptography Series: PEM and PKCS7, PKCS8, PKCS12
redis事务
RL强化学习总结(一)
Analysis of Mvi Architecture
Flutter真机运行及模拟器运行
Detailed Explanation of Redis Sentinel Mode Configuration File
【练一下1】糖尿病遗传风险检测挑战赛 【讯飞开放平台】
Dephi reverse tool Dede exports function name MAP and imports it into IDA
Pycharm中使用pip安装第三方库安装失败:“Non-zero exit code (2)“的解决方法
RL reinforcement learning summary (1)
SQL(二) —— join窗口函数视图
数据库 单表查询
HQL语句执行过程
Flutter学习4-基本UI组件
[WeChat applet] WXML template syntax - conditional rendering
Returned object not currently part of this pool
Redis - 13. Development Specifications
【过一下11】随机森林和特征工程