文章目录
- 一、time库
- 1、获取现在时间
- 2、时间戳与计时器
- 3、格式化
- 4、睡眠
- 二、random库
- 1、随机种子
- 2、产生随机整数
- 3、产生随机浮点数
- 4、随机序列
- 5、概率分布——以高斯分布为例
- 三、collections库——容器数据类型
- 1、namedtuple——具名元组
- (1)定义方法
- (2)可以调用属性
- (3)有元组的性质
- (4)是元组的子类
- 2、Counter——计数器工具
- (1)示例
- (2)是字典的一个子类
- (3)统计最常见元素
- (4)元素展开
- (5)加减操作
- 3、deque——双向队列
- 四、itertools库——迭代器
- 1、排列组合迭代器
- (1)product——笛卡尔积
- (2)permutations——排列
- (3)combinations——组合
- (4)combinations_with_replacement——元素可重复组合
- 2、拉链
- (1)zip——短拉链
- (2)zip_longest——长拉链
- 3、无穷迭代器
- (1)count(start=0,step=1)——计数
- (2)cycle(iterable)——循环
- (3)repeat(object [,times])——重复
- 4、其他
- (1)chain(iterables)——锁链
- (2)enumerate(iterable,start=0)——枚举(python内置)
- (3)groupby(iterable, key=None)——分组
一、time库
1、获取现在时间
import time # 结构化时间 print(time.localtime()) # 本地时间 print(time.gmtime()) # UTC世界统一时间 # 北京时间比统一时间UTC早8个小时 print(time.ctime()) # Sun Mar 17 15:25:51 2024 返回本地时间的字符串
2、时间戳与计时器
time.time():float型,返回从19700101 00:00:00开始的秒数
time.perf_counter():float型,随意选取一个时间点,记录现在时间到该时间点的间隔秒数,记录sleep
time.process_time():float型,随意选取一个时间点,记录现在时间到该时间点的间隔秒数,不记录sleep
time.perf_counter()比time()精度更高一些
import time t1_start = time.time() t2_start = time.perf_counter() t3_start = time.process_time() print(t1_start) print(t2_start) print(t3_start) res = 0 for i in range(100000): res += i time.sleep(5) t1_end = time.time() t2_end = time.perf_counter() t3_end = time.process_time() print("t1:",t1_end-t1_start) print("t2:",t2_end-t2_start) print("t3:",t3_end-t3_start) ''' 1710695283.7847786 0.6724354 0.671875 t1: 5.0264506340026855 t2: 5.026069 t3: 0.015625 '''
3、格式化
print( time.strftime("%X",time.localtime()) ) # 01:13:08
4、睡眠
time.sleep(秒数)
二、random库
random库提供了各种伪随机数。
1、随机种子
(1)相同种子会产生相同的随机数。
(2)如果不设置随机种子,以系统当前时间为默认值
from random import * seed(10) print(random()) # 0.5714025946899135 seed(10) print(random()) # 0.5714025946899135 print(random()) # 0.4288890546751146
2、产生随机整数
(1)randint(a,b) 产生[a,b]之间的随机整数
(2)randrange(a) 产生[0,a)之间的随机整数
(3)randrange(a,b,step) 产生(a,b)之间以step为步长的随机整数
from random import * nums1 = [randint(1,4) for i in range(5)] print(nums1) # [2, 3, 3, 4, 1] nums2 = [randrange(1,4) for i in range(5)] print(nums2) # [1, 2, 2, 1, 1] nums3 = [randrange(1,4,2) for i in range(5)] print(nums3) # [1, 1, 1, 3, 3]
3、产生随机浮点数
(1)random() 产生[0.0, 1.0)之间的随机浮点数
(2)uniform(a, b) 产生[a,b]之间的随机浮点数
from random import * nums1 = [random() for i in range(3)] print(nums1) # [0.8185405295924474, 0.3084685502649297, 0.6473292231282459] nums2 = [uniform(1,2) for i in range(3)] print(nums2) # [1.5584797693674464, 1.7587782534434675, 1.7008288028686036]
4、随机序列
(1)choice(seq) 从序列类型中随机返回一个元素
(2)choices(seq, weights= None, k) 对序列进行k次有放回采样,可设置权重
(3)shuffle(seq) 将序列中元素随机排列,返回打乱后的序列
(4)sample(seq,k) 从seq中随机选取k个元素,无放回采样,以列表类型返回。
from random import * seq1 = choice(["win","lose","draw"]) print(seq1) # lose seq2 = choices(["win","lose","draw"],[4,4,2],k=5) print(seq2) # ['lose', 'win', 'win', 'draw', 'win'] ls = ["win","lose","draw"] shuffle(ls) print(ls) # ['win', 'draw', 'lose'] seq4 = sample(["win","lose","draw"],3) print(seq4) # ['draw', 'win', 'lose']
5、概率分布——以高斯分布为例
gauss(mean, std) 产生一个平均值mean,标准差std,符合高斯分布的随机数
from random import * import matplotlib.pyplot as plt res = [gauss(0,1) for i in range(10000)] plt.hist(res, bins=1000) # bins=1000表示将数据分成1000个区间统计 plt.show()
三、collections库——容器数据类型
1、namedtuple——具名元组
就是给元组起个名字。namedtuple是元组的子类。
(1)定义方法
namedtuple(typename, field_names, *, rename=False, defaults=None, module=None)
typename:元组名字 field_names:域名
import collections Pont = collections.namedtuple("Point",["x","y"]) p = Pont(1,y=2) print(p) # Point(x=1, y=2)
(2)可以调用属性
print(p.x) # 1
(3)有元组的性质
print(p[1]) # 2 a,b = p print(a,b) # 1 2
(4)是元组的子类
print(isinstance(p,tuple)) # True
2、Counter——计数器工具
(1)示例
from collections import Counter s = "我爱人人人人爱我" cnt_s = Counter(s) print(cnt_s) # Counter({'人': 4, '我': 2, '爱': 2})
(2)是字典的一个子类
print(isinstance(cnt_s,dict)) # True
(3)统计最常见元素
cmmn = cnt_s.most_common(2) print(cmmn) # [('人', 4), ('我', 2)]
(4)元素展开
print(list(cnt_s.elements())) # ['我', '我', '爱', '爱', '人', '人', '人', '人']
(5)加减操作
s1 = Counter(a=1,b=3) s2 = Counter(a=2,c=4) s3 = s1+s2 print(s3) # Counter({'c': 4, 'a': 3, 'b': 3})
3、deque——双向队列
双向队列可以方便地在队列两边高效、快速地增加和删除元素
from collections import deque d = deque("cde") d.append("1") # 右端增加 d.append("2") # 右端增加 d.appendleft("a") # 左端增加 d.appendleft("b") # 左端增加 print(d) # deque(['b', 'a', 'c', 'd', 'e', '1', '2']) d.pop() # 右端删除 d.popleft() # 左端删除 print(d) # deque(['a', 'c', 'd', 'e', '1'])
四、itertools库——迭代器
1、排列组合迭代器
(1)product——笛卡尔积
import itertools for i in itertools.product("ABC","01"): print(i) ''' ('A', '0') ('A', '1') ('B', '0') ('B', '1') ('C', '0') ('C', '1') '''
import itertools for i in itertools.product("AB",repeat=3): print(i) ''' ('A', 'A', 'A') ('A', 'A', 'B') ('A', 'B', 'A') ('A', 'B', 'B') ('B', 'A', 'A') ('B', 'A', 'B') ('B', 'B', 'A') ('B', 'B', 'B') '''
(2)permutations——排列
import itertools for i in itertools.permutations("ABC",2): # 2是排列的长度 print(i) ''' ('A', 'B') ('A', 'C') ('B', 'A') ('B', 'C') ('C', 'A') ('C', 'B') '''
import itertools for i in itertools.permutations(range(3)): # 不指定长度,默认是第一个参数的长度 print(i) ''' (0, 1, 2) (0, 2, 1) (1, 0, 2) (1, 2, 0) (2, 0, 1) (2, 1, 0) '''
(3)combinations——组合
import itertools for i in itertools.combinations("ABCD",2): # 2是组合的长度 print(i) ''' ('A', 'B') ('A', 'C') ('A', 'D') ('B', 'C') ('B', 'D') ('C', 'D') '''
import itertools for i in itertools.combinations(range(4),2): print(i) ''' (0, 1) (0, 2) (0, 3) (1, 2) (1, 3) (2, 3) '''
(4)combinations_with_replacement——元素可重复组合
import itertools for i in itertools.combinations_with_replacement("ABC",2): # 2是组合的长度 print(i) ''' ('A', 'A') ('A', 'B') ('A', 'C') ('B', 'B') ('B', 'C') ('C', 'C') '''
import itertools for i in itertools.product("ABC",repeat=2): print(i) ''' ('A', 'A') ('A', 'B') ('A', 'C') ('B', 'A') ('B', 'B') ('B', 'C') ('C', 'A') ('C', 'B') ('C', 'C') '''
2、拉链
(1)zip——短拉链
长度不一时,执行到最短的对象处,就停止
for i in zip("ABCD","012","xyz"): # zip是python内置函数 print(i) ''' ('A', '0', 'x') ('B', '1', 'y') ('C', '2', 'z') '''
(2)zip_longest——长拉链
长度不一致时,执行到最长的对象处,就停止,缺省元素用None或指定字符替代。
import itertools for i in itertools.zip_longest("ABC","01"): print(i) ''' ('A', '0') ('B', '1') ('C', None) '''
import itertools for i in itertools.zip_longest("ABC","01",fillvalue="*"): print(i) ''' ('A', '0') ('B', '1') ('C', '*') '''
3、无穷迭代器
(1)count(start=0,step=1)——计数
创建一个迭代器,它从start值开始,返回均匀间隔的值
import itertools it = itertools.count(step=2) print(it.__next__()) # 0 print(it.__next__()) # 2 print(it.__next__()) # 4
(2)cycle(iterable)——循环
创建一个迭代器,返回iterable中所有元素,无限重复
import itertools it = itertools.cycle("AB") print(it.__next__()) # A print(it.__next__()) # B print(it.__next__()) # A
(3)repeat(object [,times])——重复
创建一个迭代器,不断重复object,除非设定参数times,否则将无限重复
import itertools it = itertools.repeat(4,3) print(it.__next__()) # 4 print(it.__next__()) # 4 print(it.__next__()) # 4 print(it.__next__()) # 触发异常 StopIteration
4、其他
(1)chain(iterables)——锁链
把一组迭代对象串联起来,形成一个更大的迭代器
import itertools for i in itertools.chain("ABC",[1,2]): print(i) ''' A B C 1 2 '''
(2)enumerate(iterable,start=0)——枚举(python内置)
产出由两个元素组成的元组,结构是(index, item),其中index从start开始,item从iterable中取。
for i in enumerate("ABC",start=1): print(i) ''' (1, 'A') (2, 'B') (3, 'C') '''
(3)groupby(iterable, key=None)——分组
创建一个迭代器,按照key指定的方式,返回iterable中连续的键和组
一般来说,要预先对数据进行排序
key为None,默认把连续重复元素分组
import itertools animals = ["duck", "eagle", "rat", "giraffe", "bear"\ "bat", "dolphin", "shark", "lion"] animals.sort(key=len) # 元素按长度排序 for key, group in itertools.groupby(animals, key=len): print(key, list(group)) ''' 3 ['rat'] 4 ['duck', 'lion'] 5 ['eagle', 'shark'] 7 ['giraffe', 'bearbat', 'dolphin'] '''