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对日期/时间做了封装,此类提供了对年.月.日.时.分.秒 ...
随机推荐
- 记一次java程序占用cpu超高排查
1.首先通过top命令查看占用cpu过高的pid #top top - 18:07:25 up 48 days, 1:07, 3 users, load average: 11.94, 11.9 ...
- (转)mybatis热加载(依赖mybatis-plus插件)的实现
最近在使用mybatis,由于是刚刚开始用,用的并不顺手,目前是感觉有2个地方非常的不好用: 1.mybatis调试不方便 由于dao层只有接口,实现只是一个map的xml文件,想加断点都没有地方加, ...
- [开源] .NET数据库ORM类库 Insql
介绍 新年之际,给大家介绍个我自己开发的ORM类库Insql.TA是一个轻量级的.NET ORM类库 . 对象映射基于Dapper , Sql配置灵感来自于Mybatis.简单优雅性能是TA的追求. ...
- shell命令——if
if中[ ]实际上调用的是test的一种快捷方法.bash的数值和字符串比较运算符: 注意=两边的空格 字符串 数值 为真,如果 x = y x -eq y x != y x -ne y x ...
- [C#]System.Timers.Timer(2)
摘要 之前学习过c#中定时器Timer的基本用法,在使用过程中,有一个问题,一直困扰着自己,就是在初始化定时器的时候,如果设置的interval过小,或者每次执行的业务非常耗时的时候,这时候该怎么处理 ...
- mysql 中int类型字段unsigned和signed的探索
转自:http://www.0791quanquan.com/news_keji/topic_816453/ 探索一:正负数问题 拿tinyint字段来举例,unsigned后,字段的取值范围是0-2 ...
- Codeforces841A
A. Generous Kefa time limit per test:2 seconds memory limit per test:256 megabytes input:standard in ...
- Java 基础:变量 与 字符串
变量 Java中没有初始化的变量是不能直接使用的 局部变量 String msg; System.out.print(msg); 就会提示错误,我们必须显式的为变量指定一个初值如null.刚开始学Ja ...
- var和const和let的区别
简述: 1.前端的变量申明,可以用到var,ES6的const(衡量)/let(变量) 2.在ES5用的都是var,到ES6之后,也就是2015年开始出现const/let. var 不会报错,有声明 ...
- 【代码笔记】iOS-plist获得城市列表
一,工程图. 二,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the ...