文章目录
- 一、数学函数-random
- 1、随机数 random
- 1.0、导包
- 1.1 random.random()
- 1.2 random.seed(n)
- 1.3 random.uniform(a,b)
- 1.4 random.randint(a,b)
- 1.5 random.randrange([start=0], stop[, step=1])
- 1.6 random.choice(sequence)
- 1.7 random.choices(sequence, k)
- 1.8 random. shuffle(x)
- 1.9 random. sample(sequence,k)
- 二、时间函数
- 1、time
- 1.0 导包
- 1.1 time.time() 秒数
- 1.2 time.localtime(time.time()) 初步格式化时间
- 1.3 time.asctime(struct_time) 一种外国标准格式
- 1.4 自定义格式时间
- 2、calendar 日历类
- 2.0 导包
- 2.1 打印月日历
- 2.x 帮助
- 3、datetime模块 ★
- 3.0 导包
- 3.1 datatime()和date()
- 3.2 日期差
- 3.3 当前日期 date.today() or datetime.now()
- 3.4 timedelta 换算为天+时分秒
- 3.5 N天前(后)
- 3.6 日期分析
- 三、数据结构
- 3.1 队列
- 3.2 栈
长期更新
一、数学函数-random
1、随机数 random
1.0、导包
import random
1.1 random.random()
用于生成一个0到1的随机浮点数: 0 <= n < 1.0
list1 = [random.random() for i in range(5)]
print(list1)
[0.9271152577610227, 0.7006174506049112, 0.5127977465367721, 0.2895395046880833, 0.8221562682396618]
1.2 random.seed(n)
用于设定种子值,其中的n可以是任意数字。
random.random() 生成随机数时,每一次生成的数都是随机的。
但是,使用 random.seed(n) 设定好种子之后,在先调用seed(n)时,使用 random() 生成的随机数将会是同一个。(保证每次运行的随机结果一样)
random.seed(10)
list1 = [random.random() for i in range(5)]
print(list1)
random.seed(11)
list1 = [random.random() for i in range(5)]
print(list1)
random.seed(10)
list1 = [random.random() for i in range(5)]
print(list1)
random.seed(10) # 没执行一次random之前都要设置随机数种子
list1 = [random.random() for i in range(5)]
print(list1)
# 未设置随机数种子 立刻不一样了
list1 = [random.random() for i in range(5)]
print(list1)
1.3 random.uniform(a,b)
返回a,b之间的均匀分布里随机采样的浮点数,若a<=b则范围[a,b],若a>=b则范围[b,a] ,a和b可以是实数。
list1 = [random.uniform(15,18) for i in range(6)]
print(list1)
[15.398934434136638, 17.30351344183197, 17.94723974703357, 17.908164481214754, 16.839980461640128, 15.132781898593862]
1.4 random.randint(a,b)
返回a,b之间的整数,范围[a,b],注意:传入参数必须是整数,a一定要比b小。
实测:确实包含右边区间b
list1 = [random.randint(10,20) for i in range(20)]
print(list1)
[17, 19, 12, 14, 13, 11, 12, 13, 17, 16, 14, 11, 15, 12, 19, 20, 20, 11, 19, 10]
1.5 random.randrange([start=0], stop[, step=1])
返回前闭后开区间[start,stop)内的整数,可以设置step。只能传入整数。
list1 = [random.randrange(0,100,2) for i in range(20)] # [0,100) 内的偶数(步长为2嘛)
print(list1)
[20, 82, 80, 14, 22, 0, 76, 50, 18, 98, 72, 20, 24, 20, 2, 84, 30, 56, 80, 48]
list1 = [random.randrange(0,100) for i in range(20)] # [0,100) 内的数(步长默认为1)
print(list1)
[7, 86, 74, 41, 21, 6, 96, 35, 90, 89, 15, 91, 6, 76, 28, 39, 67, 79, 27, 83]
list1 = [random.randrange(100) for i in range(20)] # [0,100) 内的数 (start默认0、step都默认1)
print(list1)
[12, 4, 17, 27, 82, 11, 15, 32, 8, 88, 93, 51, 4, 69, 54, 60, 57, 43, 89, 58]
1.6 random.choice(sequence)
从sequence(序列,列表、元组和字符串)中随机获取一个元素。
list1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
list2 = [random.choice(list1) for i in range(10)] # 每次list1中随机取一个数 共取10次
print(list2)
[8, 9, 1, 0, 17, 9, 7, 12, 0, 2]
1.7 random.choices(sequence, k)
从sequence(序列,列表、元组和字符串)中随机获取k个元素,可能重复,k用参数名传值,k省略则默认取1个,返回list。
list1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
list2 = random.choices(list1,k=10) # 直接从list1中随机获取10个元素 可能重复
print(list2)
list2 = random.choices(list1,k=10) # 直接从list1中随机获取10个元素 可能重复
print(list2)
[7, 3, 2, 3, 9, 7, 8, 15, 14, 10]
[3, 4, 0, 15, 5, 9, 18, 18, 8, 0]
1.8 random. shuffle(x)
用于将列表中的元素打乱顺序,俗称为洗牌。
list1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
random.shuffle(list1)
print(list1)
[8, 19, 0, 6, 14, 2, 12, 1, 5, 16, 15, 7, 11, 13, 4, 17, 18, 9, 3, 10]
1.9 random. sample(sequence,k)
从指定序列中随机获取k个不重复元素作为一个列表返回, sample函数不会修改原有序列。
list1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
list2 = random.sample(list1,10) # list1中随机选取10个不重复的数
print(list2)
[0, 6, 16, 5, 15, 11, 9, 10, 19, 8]
二、时间函数
1、time
1.0 导包
import time
1.1 time.time() 秒数
1970-1-1到现在的浮点秒数 时间戳
time.time() # 1970-1-1到现在的浮点秒数 时间戳
1.2 time.localtime(time.time()) 初步格式化时间
localtime方法自动转换为固定时间格式
time.localtime(time.time())
time.struct_time(tm_year=2022, tm_mon=8, tm_mday=26, tm_hour=22, tm_min=44, tm_sec=5, tm_wday=4, tm_yday=238, tm_isdst=0)
艰苦自定义
t = time.localtime(time.time())
print(f'{t.tm_year}年{t.tm_mon}月{t.tm_mday}日',f'{t.tm_hour}:{t.tm_min}:{t.tm_sec}',f'星期{t.tm_wday}')
2023年1月18日 13:58:47 星期2
1.3 time.asctime(struct_time) 一种外国标准格式
struct_time = time.localtime(time.time())
time.asctime(struct_time)
'Wed Jan 18 13:59:37 2023'
1.4 自定义格式时间
3.2 有详细当前时间
time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
'2023-01-18 14:01:39'
time.strftime('%Y年%m月%d日 %H:%M:%S',time.localtime())
'2022年08月26日 22:49:16'
2、calendar 日历类
2.0 导包
import calendar as cal
2.1 打印月日历
print(cal.month(2022,8))
print(cal.month(2022,9))
2.x 帮助
import calendar
print(calendar.__doc__)
print(help(calendar.month))
3、datetime模块 ★
python里专门用来进行日期处理的模块(.py文件)
3.0 导包
from datetime import date
from datetime import datetime
from datetime import timedelta
或者直接
from datetime import date,datetime,timedelta
3.1 datatime()和date()
data 只有年月日
datetime 则有时分秒(+年月日)
timedelta 用于时间间隔
t1 = date(2023,1,18)
t2 = datetime(2023,1,18,12,1,5)
print(t1)
print(t2)
help(t1)
help(t2)
def fmt(dt):
return f'{dt.year}年{dt.month}月{dt.day}日'
def fmt2(dt):
return f'{dt.year}年{dt.month}月{dt.day}日 {dt.hour}:{dt.minute}:{dt.second}'
print(fmt(t1))
print(fmt2(t2))
3.2 日期差
- 计算两个日期相差多少天
d1 = datetime(2023,1,18)
d2 = datetime(2020,5,10)
dc = (d1-d2).days
print(f'{fmt(d1)} 与 {fmt(d2)} 相差 {dc} 天',)
- 计算两个日期相差多少秒
# 获取两个日期时间的时间差
time1 = datetime(2020, 5, 10, 12, 2, 0)
time2 = datetime.now()
differtime = (time1 -time2).total_seconds()
print(f'{fmt2(time1)}与{fmt2(time2)}(现在) 相差{differtime}秒')
3.3 当前日期 date.today() or datetime.now()
print(date.today())
print(datetime.now())
print()
t = datetime.now()
print(f'{t.year}年{t.month}月{t.day}日 {t.hour}:{t.minute}:{t.second}')
time.strftime('%Y年%m月%d日 %H:%M:%S',time.localtime())
3.4 timedelta 换算为天+时分秒
help(timedelta)
timedelta(days=0, secnotallow=0, microsecnotallow=0, millisecnotallow=0, minutes=0, hours=0, weeks=0)
3.5 N天前(后)
+timedelta(days = d, hours= h)
-timedelta(days = d, hours= h)
nowday = datetime.now()
print('现在:',nowday)
afterday = nowday + timedelta(days=10) #10天后的现在
frontday = nowday - timedelta(days=10) #10天前的现在
print('十天前:',afterday);
print(afterday.ctime());# ctime() 返回一个表示日期和时间的字符串。
print('十天后:',frontday);
print(frontday.ctime());# ctime() 返回一个表示日期和时间的字符串。
3.6 日期分析
- 指定日期获取相关结构体
testday = date(2023, 8, 31) # 指定日期 (只能有3个参数)
print(testday.isocalendar())# 年份,周数,周几
print(testday.timetuple())# 时间大结构体
- 当前日期获取相关结构体
now = datetime.now()
print(now)
print(now.timetuple()) # 时间大结构体
print(now.isocalendar()) # 年份,周数,周几
- lx
# td = date(2023, 8, 31) # 指定日期 (只能有3个参数)
td = date.today() #td = datetime.now() 两种写法都行
print('日期:',td)
print('是一年中的第',td.isocalendar().week,'周')
print('是一年中的第',td.timetuple().tm_yday,'天')
print('是星期',td.isocalendar().weekday)
三、数据结构
3.1 队列
直接list就能模拟了
L = [1, 2, 3]
top = L.pop(0) # 删除第0个元素 (相当于弹栈了)
print(top) # 1
print(L) # [2, 3]
L.append(4) # 末尾追加元素 (相当于压栈了)
print(L) # [2, 3, 4]
也有专门的数据结构
import queue
if __name__ == '__main__':
q = queue.Queue()
q.put(1)
q.put(2)
q.put(3)#入队
while not q.empty():
top = q.get()#出队
print(top)
3.2 栈
直接list就能模拟了
if __name__ == '__main__':
L = [1, 2, 3]
L.append(4) # 末尾追加 相当于压栈了
print(L) # [1, 2, 3, 4]
top = L.pop(-1) # 删除最后一个(栈顶) 相当于弹栈了
print(top) # 4
print(L) # [1, 2, 3]