-
写一个匿名函数,判断指定的年是否是闰年 (先直接用普通函数)
def leap_year(year): if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0): print(f'{year}是闰年') else: print(f'{year}不是闰年') years = 2020 leap_year(years) result = lambda x: x % 400 == 0 or (x % 4 == 0 and x % 100 != 0) print(f'该年是否是闰年:{result(2021)}')
-
写一个函数将一个指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使用列表自带的逆序函数)
def element_reversed_order(list1): list2 = sorted(list1,reverse=True) return list2 list1 = [1,2,3] print(element_reversed_order(list1))
-
编写一个函数,计算一个整数的各位数的平方和
例如: sum1(12) -> 5(1的平方加上2的平方) sum1(123) -> 14
def sum_of_squares(nums):
sum1 = 0
for i in range(len(nums)):
sum1 += int(nums[i])**2
return sum1
sum1 = input('请输入一个整数:')
print(sum_of_squares(sum1))
- 求列表 nums 中绝对值最小的元素
例如:nums = [-23, 100, 89, -56, -234, 123], 最大值是:-23
def min_nums(list1:list):
num = list1[0]
for i in list1:
if abs(i) <= abs(num):
num = i
return num
nums = [-23, 100, 89, -56, -234, 123]
print(min_nums(nums))
-
已经两个列表A和B,用map函数创建一个字典,A中的元素是key,B中的元素是value
A = ['name', 'age', 'sex'] B = ['张三', 18, '女'] 新字典: {'name': '张三', 'age': 18, 'sex': '女'} def create_dictionary(i, j): dict1 = {} for x in range(len(i)): for y in range(x, len(j)): dict1.setdefault(i[x], j[y]) return dict1 map(create_dictionary,['name', 'age', 'sex'],['张三', 18, '女']) A = ['name', 'age', 'sex'] B = ['张三', 18, '女'] print(create_dictionary(A,B))
-
已经三个列表分别表示5个学生的姓名、学科和班号,使用map将这个三个列表拼成一个表示每个学生班级信息的的字典
names = ['小明', '小花', '小红', '老王'] nums = ['1906', '1807', '2001', '2004'] subjects = ['python', 'h5', 'java', 'python'] 结果:{'小明': 'python1906', '小花': 'h51807', '小红': 'java2001', '老王': 'python2004'} def create_dictionary(i, j, k): dict1 = {} for x in range(len(i)): for y in range(x, len(j)): z = k[y] + j[y] dict1.setdefault(i[x], z) return dict1 map(create_dictionary,['小明', '小花', '小红', '老王'],['1906', '1807', '2001', '2004'],['python', 'h5', 'java', 'python']) names = ['小明', '小花', '小红', '老王'] nums = ['1906', '1807', '2001', '2004'] subjects = ['python', 'h5', 'java', 'python'] print(create_dictionary(names,nums,subjects))
-
已经一个列表message, 使用reduce计算列表中所有数字的和
message = ['你好', 20, '30', 5, 6.89, 'hello'] 结果:31.89 from functools import reduce def sum1(*args): num = 0 for i in message: if type(i) == int or type(i) == float: num += i return num message = ['你好', 20, '30', 5, 6.89, 'hello'] sum2 = reduce(sum1,message) print(sum2)
-
已经列表points中保存的是每个点的坐标(坐标是用元组表示的,第一个值是x坐标,第二个值是y坐标)
points = [ (10, 20), (0, 100), (20, 30), (-10, 20), (30, -100) ]
1)获取列表中y坐标最大的点
def max_y(list1:list): list2 = [] for i in list1: list2.append(i[-1]) return max(list2) print(max_y(points))
2)获取列表中x坐标最小的点
def min_x(list1:list): list2 = [] for i in list1: list2.append(i[0]) return min(list2) print(min_x(points))
3)获取列表中距离原点最远的点
def max_distance(list1): num = abs(list1[0][0] * list1[0][1] / 2) for i in list1: if i[0] == 0 or i[1] == 0: if abs(i[0] / 2) >= num or abs(i[1] / 2) >= num: num = i[0] / 2 + i[1] / 2 elif abs(i[0] * i[1] / 2) >= num: num = i[0] * i[1] / 2 return i print(max_distance(points))
4)将点按照点到x轴的距离大小从大到小排序
def max_x_distance(list1: list): for i in range(len(list1)): for j in (i, len(list1)-1): if abs(list1[i][-1]) < abs(list1[j][-1]): list1[i], list1[j] = list1[j], list1[i] return list1 print(max_x_distance(points))