-
写一个匿名函数,判断指定的年是否是闰年 (先直接用普通函数)
def run_(int_: int): return int_ % 400 == 0 or (int_ % 4 == 0 and int_ % 100 != 0) print(run_(2000)) run_2 = lambda int_: int_ % 400 == 0 or (int_ % 4 == 0 and int_ % 100 != 0) print(run_2(2005))
-
写一个函数将一个指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使用列表自带的逆序函数)
nixu_ = lambda x: x[-1::-1] print(nixu_([1, 2, 3]))
-
编写一个函数,计算一个整数的各位数的平方和
例如: sum1(12) -> 5(1的平方加上2的平方) sum1(123) -> 14
sum1 = lambda x: sum([int(y) ** 2 for y in str(x)])
print(sum1(123))
- 求列表 nums 中绝对值最小的元素
例如:nums = [-23, 100, 89, -56, -234, 123], 最大值是:-23
def jue_min(self: list):
return min(self, key=lambda x: x ** 2)
nums = [-23, 100, 89, -56, -234, 123]
print(jue_min(nums))
-
已经两个列表A和B,用map函数创建一个字典,A中的元素是key,B中的元素是value
A = ['name', 'age', 'sex'] B = ['张三', 18, '女'] 新字典: {'name': '张三', 'age': 18, 'sex': '女'}
A = ['name', 'age', 'sex'] B = ['张三', 18, '女'] result = dict(map(lambda x,y:(x,y),A,B)) print(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 = dict(map(lambda x, y, z: (x, z + y), names, nums, subjects)) print(result)
-
已经一个列表message, 使用reduce计算列表中所有数字的和
message = ['你好', 20, '30', 5, 6.89, 'hello'] 结果:31.89
from functools import reduce message = ['你好', 20, '30', 5, 6.89, 'hello'] result = reduce(lambda x,item: x + item, [y for y in message if type(y) == int or type(y) == float], 0) print(result)
-
已经列表points中保存的是每个点的坐标(坐标是用元组表示的,第一个值是x坐标,第二个值是y坐标)
points = [ (10, 20), (0, 100), (20, 30), (-10, 20), (30, -100) ]
1)获取列表中y坐标最大的点
result = max(points, key=lambda x: x[1] ** 2) print(result)
2)获取列表中x坐标最小的点
result = min(points, key=lambda x: x[0] ** 2) print(result)
3)获取列表中距离原点最远的点
result = max(points, key=lambda x: x[0] ** 2 + x[1] ** 2) print(result)
4)将点按照点到x轴的距离大小从大到小排序
result = sorted(points, key=lambda x: x[0] ** 2) print(result)
-
封装一个函数完成斗地主发牌的功能。
from random import shuffle
nums = []
huase = '♥♠♣◇'
daxiao = ['JOKER', 'joker']
huapai = ['J', 'Q', 'K', 'A']
# 封装
for y in huase:
for x in huapai:
nums.append(y + x)
for x in range(2, 11):
nums.append(y + str(x))
nums += daxiao
# print(nums)
# 洗牌
shuffle(nums)
# print(nums, len(nums))
# 发牌 一个十张牌
player1 = []
player2 = []
player3 = []
player = [player1, player2, player3]
xihao = iter(nums)
count = 0
for x in xihao:
player1.append(x)
count += 1
if count == 17:
count = 0
break
for x in xihao:
player2.append(x)
count += 1
if count == 17:
count = 0
break
for x in xihao:
player3.append(x)
count += 1
if count == 17:
count = 0
break
# print(player1, len(player1))
# print(player2, len(player2))
# print(player3, len(player3))
# 判断庄家 并把剩余牌给他
king = []
for x in player:
for y in x:
if y == '♣3':
for z in xihao:
x.append(z)
break
print(player1, len(player1))
print(player2, len(player2))
print(player3, len(player3))