Python中日期时间案例演示
案例:准备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.exeC:/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
Python中日期时间案例演示的更多相关文章
- [ Python入门教程 ] Python中日期时间datetime模块使用实例
Python中datetime模块提供强大易用的日期处理功能,用于记录程序操作或修改时间.时间计算.日志时间显示等功能.datatime模块重新封装了time模块,提供的类包括date.time.da ...
- Python中日期和时间格式化输出的方法
本文转自:https://www.jb51.net/article/62518.htm 本文实例总结了python中日期和时间格式化输出的方法.分享给大家供大家参考.具体分析如下: python格式化 ...
- Python中对时间日期的处理方法简单汇总
这篇文章主要介绍了Python实用日期时间处理方法汇总,本文讲解了获取当前datetime.获取当天date.获取明天/前N天.获取当天开始和结束时间(00:00:00 23:59:59).获取两个d ...
- Python学习---日期时间
在Python里面日期时间的功能主要由几个模块提供:time,calendar,datetime,date等 time主要用到的功能函数: #!/usr/bin/python3 # coding:ut ...
- Python实用日期时间处理方法汇总
这篇文章主要介绍了Python实用日期时间处理方法汇总,本文讲解了获取当前datetime.获取当天date.获取明天/前N天.获取当天开始和结束时间(00:00:00 23:59:59).获取两个d ...
- python中的时间和时间格式转换
1.python中的时间:要得到年月日时分秒的时间: import time #time.struct_time(tm_year=2012, tm_mon=9, tm_mday=15, tm_hour ...
- [转]JDBC中日期时间的处理技巧
Java中用类java.util.Date对日期/时间做了封装,此类提供了对年.月.日.时.分.秒.毫秒以及时区的控制方法,同时也提供一些工具方法,比如日期/时间的比较,前后判断等. java.uti ...
- Python 中的时间处理包datetime和arrow
Python 中的时间处理包datetime和arrow 在获取贝壳分的时候用到了时间处理函数,想要获取上个月时间包括年.月.日等 # 方法一: today = datetime.date.today ...
- 【Java 与数据库】JDBC中日期时间的处理技巧
JDBC中日期时间的处理技巧 详谈Java.util.Date和Java.sql.Date 基础知识 Java中用类java.util.Date对日期/时间做了封装,此类提供了对年.月.日.时.分.秒 ...
随机推荐
- DOS在这里
转自: http://blog.csdn.net/rheostat/article/details/8043835 在右键菜单中添加Dos快捷通道-dos在这里 在右键菜单中添加 Dos 窗体 不用每 ...
- new image的使用
在看别人的程序,用到了new image()这种方法,然而怎么看也不是很明白: 于是就上网搜,然而却没有一个人能够解开我心中的疑惑,因为没有一个人的程序是完整的, 即使我知道怎么用,但是我看不到效果就 ...
- 【转】GDI+中发生一般性错误的解决办法
今天在开发.net引用程序中,需要System.Drawing.Image.Save 创建图片,debug的时候程序一切正常,可是发布到IIS后缺提示出现“GDI+中发生一般性错误”的异常. 于是开始 ...
- CentOS 7.2安装Docker-ce
1.Docker分类 Docker Engine改为Docker CE(社区版) 它包含了CLI客户端.后台进程/服务以及API.用户像以前以同样的方式获取.Docker Data Center改为D ...
- c#中Socket网络通信的入门
请访问 http://balabiu.com/?p=16 后续本文更新将在这里: 将设计服务器端异步接受客户端连接和客户端消息.
- C#基础知识回顾--C#遍历enum类型、获取enum项个数
C#遍历enum类型 对于enum类型: 使用foreach遍历enum类型的元素并填充combox foreach ( HatchStyle hs1 in Enum.GetValues(typeof ...
- 爬虫、网页分析解析辅助工具 Xpath-helper
每一个写爬虫.或者是做网页分析的人,相信都会因为在定位.获取xpath路径上花费大量的时间,甚至有时候当爬虫框架成熟之后,基本上主要的时间都花费在了页面的解析上.在没有这些辅助工具的日子里,我们只能通 ...
- winform窗体 小程序【登录窗体】【恶搞程序】
登录窗体 using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; ...
- springboot中使用mybatis之mapper
Spring Boot中使用MyBatis传参方式:使用@Param@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})& ...
- oracle逐步学习总结之权限和角色(基础六)
原创作品,转自请注明出处:https://www.cnblogs.com/sunshine5683/p/10236129.html 继续上节的索引,这次主要总结oracle数据库的权限问题!(在总结的 ...