1. 写一个匿名函数,判断指定的年是否是闰年。
judge_leap_year = lambda x: x % 4 == 0 and x % 100 != 0 or x % 400 == 0
print(judge_leap_year(2001))
2. 写一个函数将一个指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使用列表自带的逆序函数)。
def new_flashback(lst1):
new_lst = []
for i in lst1:
new_lst.insert(0, i)
return new_lst
print(new_flashback([1, 2, 3, 4, 5]))
3. 编写一个函数,计算一个整数的各位数的平方和。
例如: sum1(12) -> 5(1的平方加上2的平方) sum1(123) -> 14
from functools import reduce
def sum_square(num: int):
return reduce(lambda x, item: x + eval(item) ** 2, str(num), 0)
print(sum_square(123))
4. 求列表 nums 中绝对值最小的元素。
例如:nums = [-23, 100, 89, -56, -234, 123], 最大值是:-23
def min_ele(nums):
return min(nums, key=lambda x: abs(x))
nums = [-23, 100, 89, -56, -234, 123]
print(min_ele(nums))
5. 已经两个列表A和B,用map函数创建一个字典,A中的元素是key,B中的元素是value。
新字典: {'name': '张三', 'age': 18, 'sex': '女'}
def new_dict(lst1, lst2):
return dict(map(lambda x1, x2: (x1, x2), A, B))
A = ['name', 'age', 'sex']
B = ['张三', 18, '女']
print(new_dict(A, B))
6. 已经三个列表分别表示5个学生的姓名、学科和班号,使用map将这个三个列表拼成一个表示每个学生班级信息的的字典。
结果:{'小明': 'python1906', '小花': 'h51807', '小红': 'java2001', '老王': 'python2004'}
def new_stu_info(lst1, lst2, lst3):
return dict(map(lambda x1, x2, x3: (x1, x3 + x2), names, nums, subjects))
names = ['小明', '小花', '小红', '老王']
nums = ['1906', '1807', '2001', '2004']
subjects = ['python', 'h5', 'java', 'python']
print(new_stu_info(names, nums, subjects))
7. 已知一个列表message, 使用reduce计算列表中所有数字的和。
message = ['你好', 20, '30', 5, 6.89, 'hello']
结果:31.89
方法一:
def lst_sum1(lst):
return reduce(lambda a, item: a + item, [x for x in message if type(x) in (int, float)], 0)
print(lst_sum1(message))
方法二:
def lst_sum2(lst):
return reduce(lambda x, item: x + item if type(item) in (int, float) else x + 0, message, 0)
print(lst_sum2(message))
8. 已经列表points中保存的是每个点的坐标(坐标是用元组表示的,第一个值是x坐标,第二个值是y坐标)。
points = [(10, 20), (0, 100), (20, 30), (-10, 20), (30, -100)]
1)获取列表中y坐标最大的点。
res1 = max(points, key=lambda x: x[1])
print(res1)
2)获取列表中x坐标最小的点。
res2 = min(points, key=lambda x: x[0])
print(res2)
3)获取列表中距离原点最远的点。
res3 = max(points, key=lambda x: x[0] ** 2 + x[1] ** 2)
print(res3)
4)将点按照点到x轴的距离大小从大到小排序。
res4 = sorted(points, key=lambda x: abs(x[1]), reverse=True)
print(res4)
9. 封装一个函数完成斗地主发牌的功能。
from random import shuffle
def deal_cards():
# 准备一副新的牌
colors = ['♥', '♠', '♣', '♦']
nums = [str(x) for x in range(2, 11)] + ['J', 'Q', 'K', 'A']
pokers = ['joker', 'JOKER']
for n in nums:
for c in colors:
pokers.append(c + n)
# 洗牌
shuffle(pokers)
# 发牌
pokers = iter(pokers)
player1 = []
player2 = []
player3 = []
for _ in range(17):
player1.append(next(pokers))
player2.append(next(pokers))
player3.append(next(pokers))
# 4)理牌
values = {'J': 11, 'Q': 12, 'K': 13, 'A': 14, '2': 15, 'oker': 16, 'OKER': 17}
values.update({str(x): x for x in range(3, 11)})
player1.sort(key=lambda item: values[item[1:]], reverse=True)
player2.sort(key=lambda item: values[item[1:]], reverse=True)
player3.sort(key=lambda item: values[item[1:]], reverse=True)
return player1, player2, player3, list(pokers)
p1, p2, p3, landlord = deal_cards()
print(p1)
print(p2)
print(p3)
print(landlord)
Python基础小练习_函数2
最新推荐文章于 2024-06-17 17:35:06 发布