Python - 标准库学习
-
- import time
- import random
- import collections
- import itertools
import time
- 获取现在时间
>>> 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'
- 时间戳与计时器
>>> time.time()
1597980231.4828305
>>> time.perf_counter()
25.7903971
>>> time.process_time()
0.0625
- 格式化输出
>>> 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'
- 暂停
>>> time.sleep()
import random
- 随机种子
>>> from random import *
>>> seed(10)
>>> print(random())
0.5714025946899135
>>> print(random())
0.4288890546751146
>>> seed(10)
>>> print(random())
0.5714025946899135
- 产生随机整数
>>> randint(1,10)
6
>>> randrange(10)
7
>>> randrange(0,10,2)
8
- 产生随机浮点数
>>> random()
0.014832446024553692
>>> uniform(0,10)
4.625805080929491
- 序列用函数
>>> choice(['a', 'b', 'c'])
'b'
>>> choice("python")
't'
>>> choices(['a', 'b', 'c'], [4,4,2], k=20)
['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)
[2, 5, 1]
- 高斯分布
>>> num = gauss(0,1)
>>> num
0.7917381338804127
>>> num = gauss(0,1)
>>> num
-0.3077180444236722
import collections
- namedtuple() -具名元组:用于构建一个新的元组子类。
>>> import collections
>>> Point = collections.namedtuple("Point", ["x", "y"])
>>> p = Point(1,2)
>>> p
Point(x=1, y=2)
>>> p.x
1
>>> p[0]
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)
[('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})
- 双向队列
>>> 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
- 排列组合迭代器
>>> import itertools
>>>
>>> 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')
>>>
>>> for i in itertools.permutations('ABCD', 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)
>>>
>>> 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')
- 拉链
>>> for i in zip('abc', '012', 'ABCD'):
... 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')
- 其他
>>> for i in itertools.repeat(10,3):
... print(i)
...
10
10
10
>>> for i in itertools.chain('abc', [1,2,3]):
... 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')