Python - 标准库(time,random,collections,itertools)

本文介绍了Python中的四个标准库:time用于处理时间,random提供随机数生成,collections包含高级数据结构如namedtuple和Counter,而itertools则提供了高效的迭代器工具,包括排列组合和拉链等操作。
摘要由CSDN通过智能技术生成

import time

  1. 获取现在时间
>>> import time
>>> time.localtime()
time.struct_time(tm_year=2020, tm_mon=8, tm_mday=21, tm_hour=11, tm_min=20, tm_sec=59, tm_wday=4, tm_yday=234, tm_isdst=0)
>>> time.ctime()	# 返回字符串类类型的时间
'Fri Aug 21 11:21:07 2020'
  1. 时间戳与计时器
>>> time.time()	# 记录自纪元开始以来的秒数
1597980231.4828305
>>> time.perf_counter()	# 随意选取一个时间点,记录该事件到现在的秒数,不包含sleep
25.7903971
>>> time.process_time() # 随意选取一个时间点,记录该事件到现在的秒数,包含sleep
0.0625
  1. 格式化输出
>>> lctime = time.localtime()
>>> time.strftime("%Y-%m-%d %A %H:%M:%S", lctime)
'2020-08-21 Friday 15:21:59'
>>> time.strftime("%Y-%m-%d", lctime)
'2020-08-21'
>>> time.strftime("%H:%M:%S", lctime)
'15:21:59'
  1. 暂停
>>> time.sleep()

import random

  1. 随机种子
>>> from random import *
>>> seed(10)
>>> print(random())
0.5714025946899135
>>> print(random())
0.4288890546751146
>>> seed(10)
>>> print(random())
0.5714025946899135
  1. 产生随机整数
>>> randint(1,10)	# 取1-9之间的整数
6
>>> randrange(10)	# 取0-9之间的整数
7
>>> randrange(0,10,2)	# 取0,2,4,6,8; 其中的2为步幅
8
  1. 产生随机浮点数
>>> random()   # 0-1之间随机浮点数
0.014832446024553692
>>> uniform(0,10) # 0-10之间的随机浮点数
4.625805080929491
  1. 序列用函数
>>> choice(['a', 'b', 'c'])	# 对样本采样
'b'
>>> choice("python")	# 对字符串采样
't'
>>> choices(['a', 'b', 'c'], [4,4,2], k=20)	# 根据权重进行采样,可设置权重为None, k为采样次数
['b', 'a', 'b', 'a', 'a', 'c', 'c', 'a', 'c', 'b', 'a', 'a', 'b', 'b', 'b', 'b', 'a', 'b', 'c', 'c']

>>> num = ['1', '2', '3', '4']
>>> num
['1', '2', '3', '4']
>>> shuffle(num)	# 打乱序列顺序
>>> num
['2', '3', '1', '4']

>>> sample([1,2,3,4,5], k=3)	# 从pop类型中随机选取k个元素
[2, 5, 1]
  1. 高斯分布
>>> num = gauss(0,1)	# 0表示均值,1表示标准差
>>> num
0.7917381338804127
>>> num = gauss(0,1)	# 0-1正太分布
>>> num
-0.3077180444236722

import collections

  1. namedtuple() -具名元组:用于构建一个新的元组子类。
>>> import collections
>>> Point = collections.namedtuple("Point", ["x", "y"])	# Point是元组类型名字,[x,y]类似于类的属性
>>> p = Point(1,2)
>>> p
Point(x=1, y=2)
>>> p.x
1
>>> p[0]
1
  1. 计数器工具
>>> from collections import Counter
>>> s = ['a','a','b','a','a','b','c']
>>> Counter(s)
Counter({'a': 4, 'b': 2, 'c': 1})
>>> isinstance(Counter(s), dict)
True
>>> Counter(s).most_common(2)	# 选取其中最常出现的2个元素和计数
[('a', 4), ('b', 2)]
>>> list(Counter(s).elements())	# 展开元素
['a', 'a', 'a', 'a', 'b', 'b', 'c']
>>> Counter(s) + Counter(a=1,d=3)
Counter({'a': 5, 'd': 3, 'b': 2, 'c': 1})
  1. 双向队列
>>> from collections import deque
>>> d = deque("abcde")
>>> d.append('f')
>>> d
deque(['a', 'b', 'c', 'd', 'e', 'f'])
>>> d.appendleft('z')
>>> d
deque(['z', 'a', 'b', 'c', 'd', 'e', 'f'])
>>> d.pop()
'f'
>>> d.popleft()
'z'
>>> d
deque(['a', 'b', 'c', 'd', 'e'])

import itertools

  1. 排列组合迭代器
>>> import itertools

>>> ###### itertools.product() 笛卡尔积  ##########

>>> for i in itertools.product('ABC', '01'):
...     print(i)
...
('A', '0')
('A', '1')
('B', '0')
('B', '1')
('C', '0')
('C', '1')

>>> for i in itertools.product('AB', repeat=2):
...     print(i)
...
('A', 'A')
('A', 'B')
('B', 'A')
('B', 'B')

>>> 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')

>>> ###### itertools.permutations() 排列  ##########
>>> for i in itertools.permutations('ABCD', 3):		# 排列长度为3
...     print(i)
...
('A', 'B', 'C')
('A', 'B', 'D')
('A', 'C', 'B')
('A', 'C', 'D')
('A', 'D', 'B')
('A', 'D', 'C')
('B', 'A', 'C')
('B', 'A', 'D') 	
('B', 'C', 'A')
('B', 'C', 'D')
('B', 'D', 'A')
('B', 'D', 'C')
('C', 'A', 'B')
('C', 'A', 'D')
('C', 'B', 'A')
('C', 'B', 'D')
('C', 'D', 'A')
('C', 'D', 'B')
('D', 'A', 'B')
('D', 'A', 'C')
('D', 'B', 'A')
('D', 'B', 'C')
('D', 'C', 'A')
('D', 'C', 'B')

>>> 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)

>>> ###### itertools.combinations() 组合  ##########
>>> for i in itertools.combinations('ABCD',2):
...     print(i)
...
('A', 'B')
('A', 'C')
('A', 'D')
('B', 'C')
('B', 'D')
('C', 'D')

>>> for i in itertools.combinations(range(4),2):
...     print(i)
...
(0, 1)
(0, 2)
(0, 3)
(1, 2)
(1, 3)
(2, 3)

>>> for i in itertools.combinations_with_replacement('ABCD',2):	# 可重复
...     print(i)
...
('A', 'A')
('A', 'B')
('A', 'C')
('A', 'D')
('B', 'B')
('B', 'C')
('B', 'D')
('C', 'C')
('C', 'D')
('D', 'D')
  1. 拉链
>>> for i in zip('abc', '012', 'ABCD'):	# 执行到最短的一条结束,zip是内置函数,不需增加itertools
...     print(i)
...
('a', '0', 'A')
('b', '1', 'B')
('c', '2', 'C')

>>> for i in itertools.zip_longest('abc', '012', 'ABCD'):
...     print(i)
...
('a', '0', 'A')
('b', '1', 'B')
('c', '2', 'C')
(None, None, 'D')

>>> for i in itertools.zip_longest('abc', '012', 'ABCD', fillvalue='?'):
...     print(i)
...
('a', '0', 'A')
('b', '1', 'B')
('c', '2', 'C')
('?', '?', 'D')
  1. 其他
>>> for i in itertools.repeat(10,3):	# 重复
...     print(i)
...
10
10
10
>>> for i in itertools.chain('abc', [1,2,3]):	# chain()将多串迭代器串联起来
...     print(i)
...
a
b
c
1
2
3
>>> for i in enumerate('python', start=1): 	# 内置函数,枚举
...     print(i)
...
(1, 'p')
(2, 'y')
(3, 't')
(4, 'h')
(5, 'o')
(6, 'n')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值