案例:准备10个人姓名,然后为这10个人随机生成生日【都是90后】
1、统计出那些人是夏季【6月-8月】出生的。
2、最大的比最小的大多少天
3、谁的生日最早,谁的生日最晚
备注:春季【3-5】夏季【6-8】秋季【9-11】冬季【12-2】
演示:
from datetime import date, timedelta
from random import randint
def build_birthday(list_person_name:list):
# 初始化存储“姓名:生日”字典
name_birthday = {}.fromkeys(list_person_name)
# 生成生日
for key in name_birthday:
temp_year = randint(1990, 1999)
temp_month = randint(1, 12)
temp_day = randint(1, 30)
name_birthday[key] = date(temp_year, temp_month, temp_day)
# 返回
return name_birthday
def person_birthday_summer(name_birthday:dict):
# 用于存储夏天出生的key
list_person = []
for key in name_birthday:
if name_birthday[key].month >= 6 and name_birthday[key].month <= 8:
list_person.append(key)
# 返回
return list_person
def get_person_year_max(name_birthday:dict):
# 在字典中提取出生日
person_birth = list(name_birthday.values())
# 获取最大的生日
max_birthday = sorted(person_birth)[len(person_birth)-1]
# 遍历
for key in name_birthday:
if name_birthday[key] == max_birthday:
return key
def get_person_year_min(name_birthday:dict):
# 在字典中提取出生日
person_birth = list(name_birthday.values())
# 获取最小的生日
min_birthday = sorted(person_birth)[0]
# 遍历
for key in name_birthday:
if name_birthday[key] == min_birthday:
return key
def get_person_days(name_birthday:dict):
# 在字典中提取出生日
person_birth = list(name_birthday.values())
# 获取最大的生日
min_birthday = sorted(person_birth)[0]
max_birthday = sorted(person_birth)[len(person_birth) - 1]
# 返回天数
return (max_birthday-min_birthday).days
def get_person_early_birthday(name_birthday:dict):
for key in name_birthday:
name_birthday[key] = name_birthday[key].replace(year=1990)
person_birth = list(name_birthday.values())
return(sorted(person_birth)[0])
def get_person_later_birthday(name_birthday:dict):
for key in name_birthday:
name_birthday[key] = name_birthday[key].replace(year=1990)
person_birth = list(name_birthday.values())
return(sorted(person_birth)[len(person_birth)-1])
if __name__ == "__main__":
list_name = ["赵一", "杨二", "张三", "李四", "王五", "赵六", "马七", "郑八", "刘九", "胡十"]
# 为list_name中所有的学员生成生日
name_birthday = build_birthday(list_name)
print(name_birthday)
# 调用功能模块
birthday_summer_list = person_birthday_summer(name_birthday)
if len(birthday_summer_list) == 0:
print("没有人的生日是在夏天: ")
else:
print("生日为夏天的有:", birthday_summer_list) # 需求一
# 需求二
print("生日最大的:", get_person_year_max(name_birthday))
print("生日最小的:", get_person_year_min(name_birthday))
print("最大比最小的天数:", get_person_days(name_birthday))
# 需求三
date_early = get_person_early_birthday(name_birthday)
print("生日最大的是:%d月%d日"%(date_early.month,date_early.day))
date_later = get_person_later_birthday(name_birthday)
print("生日最小的是:%d月%d日" % (date_later.month, date_early.day))
执行结果:
C:\python\python.exe C:/python/demo/file3.py
{'赵一': datetime.date(1992, 12, 30), '杨二': datetime.date(1995, 6, 23), '张三': datetime.date(1990, 6, 21), '李四': datetime.date(1991, 9, 29), '王五': datetime.date(1996, 2, 26), '赵六': datetime.date(1995, 9, 18), '马七': datetime.date(1996, 7, 4), '郑八': datetime.date(1990, 3, 5), '刘九': datetime.date(1992, 3, 3), '胡十': datetime.date(1992, 11, 6)}
生日为夏天的有: ['杨二', '张三', '马七']
生日最大的: 马七
生日最小的: 郑八
最大比最小的天数: 2313
生日最大的是:2月26日
生日最小的是:12月26日
Process finished with exit code 0
转载于:https://blog.51cto.com/13043937/2107180