-
写一个匿名函数,判断指定的年是否是闰年 (先直接用普通函数)
year = lambda year_num: year_num % 4 == 0 and year_num % 100 != 0 or year_num % 400 == 0 print(year(2022))
-
写一个函数将一个指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使用列表自带的逆序函数)
def reverse_str(list1: list): list2 = [] for x in list1[-1::-1]: list2.append(x) return list2 list1 = [1, 2, 3] result = reverse_str(list1) print(result)
-
编写一个函数,计算一个整数的各位数的平方和
例如: sum1(12) -> 5(1的平方加上2的平方) sum1(123) -> 14
def sum_1(nums):
sum = 0
for x in str(nums):
sum += int(x) ** 2
return sum
print(sum_1(12))
4求列表 nums 中绝对值最小的元素
例如:nums = [-23, 100, 89, -56, -234, 123], 最大值是:-23
nums = [-23, 100, 89, -56, -234, 123]
result = min(nums, key=lambda item: item ** 2)
print(result)
-
已经两个列表A和B,用map函数创建一个字典,A中的元素是key,B中的元素是value
A = ['name', 'age', 'sex'] B = ['张三', 18, '女'] 新字典: {'name': '张三', 'age': 18, 'sex': '女'}
A = ['name', 'age', 'sex'] B = ['张三', 18, '女'] result = map(lambda key, value: [key, value], A, B) print(dict(result))
-
已经三个列表分别表示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'] result = map(lambda i1, i2, i3: [i1, i3 + '' + i2], names, nums, subjects) print(dict(result))
-
已经一个列表message, 使用reduce计算列表中所有数字的和
message = ['你好', 20, '30', 5, 6.89, 'hello'] 结果:31.89
-
已经列表points中保存的是每个点的坐标(坐标是用元组表示的,第一个值是x坐标,第二个值是y坐标)
points = [ (10, 20), (0, 100), (20, 30), (-10, 20), (30, -100) ]
1)获取列表中y坐标最大的点
result = max(points,key=lambda item:item[-1]) print(result)
2)获取列表中x坐标最小的点
result = min(points,key=lambda item:item[0]) print(result)
3)获取列表中距离原点最远的点
result = max(points, key=lambda item: item[0] ** 2 + item[-1] ** 2) print(result)
4)将点按照点到x轴的距离大小从大到小排序
result = sorted(points, key=lambda item: item[0] ** 2 + item[-1] ** 2, reverse=True) print(result)
-
封装一个函数完成斗地主发牌的功能。
#初级版本
from random import shuffle
def Chess(chess_sum):
play_1 = []
play_2 = []
play_3 = []
dipan_card = []
shuffle(chess_sum)
chess_sum1 = iter(chess_sum)
for y in range(17):
play_1.append(next(chess_sum1))
play_2.append(next(chess_sum1))
play_3.append(next(chess_sum1))
for z in chess_sum1:
dipan_card.append(z)
print(play_1)
print(play_2)
print(play_3)
print(dipan_card)
chess_sum =['黑桃A', '黑桃2', '黑桃3', '黑桃4', '黑桃5', '黑桃6', '黑桃7', '黑桃8', '黑桃9', '黑桃10', '黑桃J', '黑桃Q', '黑桃K', '红桃A', '红桃2', '红桃3', '红桃4',
'红桃5', '红桃6', '红桃7', '红桃8', '红桃9', '红桃10', '红桃J', '红桃Q', '红桃K', '方片A', '方片2', '方片3', '方片4', '方片5', '方片6', '方片7', '方片8',
'方片9', '方片10', '方片J', '方片Q', '方片K', '梅花A', '梅花2', '梅花3', '梅花4', '梅花5', '梅花6', '梅花7', '梅花8', '梅花9', '梅花10', '梅花J',
'梅花Q', '梅花K', '小王', '大王']
result = Chess(chess_sum)
print(result)
#升级版
from random import shuffle
def deal_cards():
# 1)准备一副新的牌
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)
# 2)洗牌
shuffle(pokers)
# 3)发牌
pokers = iter(pokers)
play_1 = []
play_2 = []
play_3 = []
for _ in range(17):
play_1.append(next(pokers))
play_2.append(next(pokers))
play_3.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)})
play_1.sort(key=lambda item: values[item[1:]], reverse=True)
play_2.sort(key=lambda item: values[item[1:]], reverse=True)
play_3.sort(key=lambda item: values[item[1:]], reverse=True)
return play_1, play_2, play_3, list(pokers)
p1, p2, p3, di = deal_cards()
print(p1)
print(p2)
print(p3)
print(di)