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

数据库 单表查询

Convert the paper official seal in the form of a photo into an electronic official seal (no need to download ps)

Dephi reverse tool Dede exports function name MAP and imports it into IDA

数据库实验五 备份与恢复

2022 Hangzhou Electric Multi-School 1st Session 01

CAP+BASE

The mall background management system based on Web design and implementation

jvm 三 之堆与栈

Dephi逆向工具Dede导出函数名MAP导入到IDA中
![[cesium] element highlighting](/img/99/504ca9802db83eb33bc6d91b34fa84.png)
[cesium] element highlighting
随机推荐
coppercam primer [6]
【读书】长期更新
【微信小程序】WXML模板语法-条件渲染
shell函数
uboot enable debug printing information
【练一下1】糖尿病遗传风险检测挑战赛 【讯飞开放平台】
OFDM 十六讲 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
第三讲 Gradient Tutorial梯度下降与随机梯度下降
[Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management
2022牛客多校第四场C.Easy Counting Problem(EGF+NTT)
【过一下14】自习室的一天
数据库 单表查询
物理层的接口有哪几个方面的特性?各包含些什么内容?
电话溥功能
NodeJs接收上传文件并自定义保存路径
Mysql5.7 二进制 部署
Pycharm中使用pip安装第三方库安装失败:“Non-zero exit code (2)“的解决方法
Detailed Explanation of Redis Sentinel Mode Configuration File
Flutter learning three-Flutter basic structure and principle
RL强化学习总结(一)