- 写一个匿名函数,判断指定的年是否是闰年 (先直接用普通函数)
#方法一:
def t(year):
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
return True
else:
return False
print(t(2001))
#方法二:
result1 = lambda year: year % 4 == 0 and year % 100 != 0 or year % 400 == 0
print(result1(2000))
- 写一个函数将一个指定的列表中的元素逆序( 如[1, 2, 3] -> [3, 2, 1])(注意:不要使用列表自带的逆序函数)
def sort_n(list1:list):
new_list = list1[-1::-1]
return new_list
list1 = [1, 2, 3]
print(sort_n(list1))
- 编写一个函数,计算一个整数的各位数的平方和
例如: sum1(12) -> 5(1的平方加上2的平方) sum1(123) -> 14
def t(item):
sum1 = 0
for i in str(item):
i = int(i)
sum1 += i**2
return sum1
print(t(123))
- 求列表 nums 中绝对值最小的元素
例如:nums = [-23, 100, 89, -56, -234, 123], 最大值是:-23
def t(list1:list):
max1 = list1[0]
for i in list1[1:]:
if i**2 < max1:
max1 = i
return max1
list1 = [-23, 100, 89, -56, -234, 123]
print(t(list1))
-
已经两个列表A和B,用map函数创建一个字典,A中的元素是key,B中的元素是value
A = ['name', 'age', 'sex'] B = ['张三', 18, '女'] 新字典: {'name': '张三', 'age': 18, 'sex': '女'}
A = ['name', 'age', 'sex']
B = ['张三', 18, '女']
result1 = dict(map(lambda x, y: (x,y), A, B))
print(result1)
-
已知三个列表分别表示5个学生的姓名、学科和班号,使用map将这个三个列表拼成一个表示每个学生班级信息的的字典
names = ['小明' , '小花', '小红' , '老王' ] subjects = ['python', 'h5' , 'java', 'python'] nums = ['1906' , '1807', '2001', '2004' ] 结果:{'小明': 'python1906', '小花': 'h51807', '小红': 'java2001', '老王': 'python2004'}
names = ['小明' , '小花', '小红' , '老王' ]
subjects = ['python', 'h5' , 'java', 'python']
nums = ['1906' , '1807', '2001', '2004' ]
result1 = dict(map(lambda x, y,z: (x,f'{y}{z}'), names,subjects,nums))
print(result1)
11.7
-
已知一个列表message, 使用reduce计算列表中所有数字的和
message = ['你好', 20, '30', 5, 6.89, 'hello'] 结果:31.89
result = reduce(lambda i,item: i + item if type(item) in (int,float) else i+0,message,0)
print(result)
-
已知列表points中保存的是每个点的坐标(坐标是用元组表示的,第一个值是x坐标,第二个值是y坐标)
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)
2)获取列表中x坐标最小的点
points = [ (10, 20), (0, 100), (20, 30), (-10, 20), (30, -100) ]
result = min(points,key=lambda item: item[0])
print(result)
3)获取列表中距离原点最远的点
points = [ (10, 20), (0, 100), (20, 30), (-10, 20), (30, -100) ]
result = max(points,key=lambda item: item[0]**2*item[1]**2)
print(result)
4)将点按照点到x轴的距离大小从大到小排序
points = [ (10, 20), (0, 100), (20, 30), (-10, 20), (30, -100) ]
result = sorted(points,key=lambda item: item[1]**2,reverse=True)
print(result)
- 封装一个函数完成斗地主发牌的功能。
import random
def deal():
colors = ['♠', '♥', '♦', '♣']
numbers = ['3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A', '2']
cards = [color + number for color in colors for number in numbers] + ['小王', '大王']
random.shuffle(cards)
player1 = sorted(cards[:17])
player2 = sorted(cards[17:34])
player3 = sorted(cards[34:51])
bottom = sorted(cards[51:])
return player1, player2, player3, bottom
player1, player2, player3, bottom = deal()
print("Player 1: ", player1)
print("Player 2: ", player2)
print("Player 3: ", player3)
print("Bottom: ", bottom)