# 匿名函数
is_leap_year = lambda year: (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
print(is_leap_year(2020))
# 普通函数
def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
result = True
else:
result = False
return result
print(is_leap_year(2020))
# 方法1:添加新的列表
def reversed1(list1:list):
new_list = []
for index in range(len(list1)-1,-1,-1):
new_list.append(list1[index])
return new_list
# 方法2:切片
def list_reverse(list1:list):
return list1[::-1]
# 方法3:交换前后元素的位置
def list_reverse2(list1:list):
len1 = len(list1)
for i in range(len1 // 2):
j = len1 - 1 -i
list1[i], list1[j] = list1[j], list1[i]
return list1
例如: sum1(12) -> 5(1的平方加上2的平方) sum1(123) -> 14
# 方法1
def quadratic_sum(num1):
sum1 = 0
for x in str(num1):
sum1 += int(x) ** 2
return sum1
# 方法2
def sum_square(num:int):
return reduce(lambda x, item: x + int(item)**2, str(num), 0)
# 方法3
def sum_square2(num:int):
return sum([int(x)**2 for x in str(num)])
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)
5.已经两个列表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 i1, i2: (i1, i2), 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']
# {'小明': 'python1906', '小花': 'h51807', '小红': 'java2001', '老王': 'python2004'}
result = map(lambda i1, i2, i3: (i1,i2+i3), names, nums, subjects)
print(dict(result))
已经一个列表message, 使用reduce计算列表中所有数字的和
message = ['你好', 20, '30', 5, 6.89, 'hello']
结果:31.89
# 方法1
message = ['你好', 20, '30', 5, 6.89, 'hello']
result = reduce(lambda x, item: x + item if type(item) == int or type(item) == float else x + 0, message, 0)
print(result)
# 方法2
message = ['你好', 20, '30', 5, 6.89, 'hello']
num = [x for x in message if type(x) == int or type(x) == float]
result = reduce(lambda y, x: y + x, num, 0)
print(result)
# 方法3
def is_number1(list1):
sum1 = 0
for item in list1:
if type(item) == int or type(item) == float:
sum1 += item
return sum1
message = ['你好', 20, '30', 5, 6.89, 'hello']
print(is_number1(message))
# 方法4
result = reduce(lambda a, item: a + item, [x for x in message if type(x) in (int, 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 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[1]**2, reverse=True)
print(result)
封装一个函数完成斗地主发牌的功能。
from random import shuffle
# 方法1
def landlord_deal_cards(cards):
shuffle(cards)
i1 = iter(cards)
player1 = []
player2 = []
player3 = []
bottom_cards = []
for x in range(17):
player1.append(next(i1))
player2.append(next(i1))
player3.append(next(i1))
for y in i1:
bottom_cards.append(y)
return player1,player2,player3,bottom_cards
cards = ['大王', '小王', '黑桃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']
print(landlord_deal_cards(cards))
# 方法2
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)
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, di = deal_cards()
print(p1)
print(p2)
print(p3)
print(di)