Is_leapyear = lambda num1: num1 % 400 == 0 or (num1 % 4 == 0 and num1 % 100 != 0)
print(Is_leapyear(2022))
False
def reversed_order(list1: list):
return list1[::-1]
print(reversed_order([1, 2, 3, 4, 5]))
[5, 4, 3, 2, 1]
例如: sum1(12) -> 5(1的平方加上2的平方) sum1(123) -> 14
def quadratic_sum(num1):
sum1 = 0
for i in str(num1):
sum1 += int(i) ** 2
return sum1
print(quadratic_sum(23))
13
例如:nums = [-23, 100, 89, -56, -234, 123], 最大值是:-23
nums = [-23, 100, 89, -56, -234, 123]
result = max(nums, key=lambda item: abs(item))
print(result)
-234
A = ['name', 'age', 'sex']
B = ['张三', 18, '女']
新字典: {'name': '张三', 'age': 18, 'sex': '女'}
A = ['name', 'age', 'sex']
B = ['张三', 18, '女']
result = dict(map(lambda item1, item2: (item1, item2), A, B))
print(result)
{‘name’: ‘张三’, ‘age’: 18, ‘sex’: ‘女’}
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']
result = dict(map(lambda item1, item2, item3: (item1, item3 + item2), names, nums, subjects))
print(result)
{‘小明’: ‘python1906’, ‘小花’: ‘h51807’, ‘小红’: ‘java2001’, ‘老王’: ‘python2004’}
message = ['你好', 20, '30', 5, 6.89, 'hello']
结果:31.89
from functools import reduce
message = ['你好', 20, '30', 5, 6.89, 'hello']
result = reduce(lambda i, item: i + (item if type(item) == int or type(item) == float else 0), message, 0)
print(result)
31.89
points = [
(10, 20), (0, 100), (20, 30), (-10, 20), (30, -100)
]
1)获取列表中y坐标最大的点
points = [
(10, 20), (0, 100), (20, 30), (-10, 20), (30, -100)
]
result = max(points, key=lambda item: item[-1])
print(result)
(0, 100)
2)获取列表中x坐标最小的点
result = min(points, key=lambda item: item[0])
print(result)
(-10, 20)
3)获取列表中距离原点最远的点
result = max(points, key=lambda item: item[0] ** 2 + item[-1] ** 2)
print(result)
(30, -100)
4)将点按照点到x轴的距离大小从大到小排序
result = sorted(points, key=lambda item: abs(item[-1]), reverse=True)
print(result)
[(0, 100), (30, -100), (20, 30), (10, 20), (-10, 20)]