标签:raw ges for ati shuf 常见 删除元素 设定 整数
1、掌握time、random库的常用用法。
2、了解collection库,掌握Counter、namedtuple、deque函数的含义和用法。
3、了解itertools库,掌握enumarate、zip、product等函数的含义和用法。
Python自身提供了比较丰富的生态,拿来即用,可极大的提高开发效率。
Python处理时间的标准库
import time
t_local = time.localtime()
t_UTC = time.gmtime()
print("t_local: ", t_local) # 本地时间
print("t_UTC: ", t_UTC) # UTC统一时间
print(time.ctime()) # 返回本地时间的字符串
"""
t_local: time.struct_time(tm_year=2020, tm_mon=2, tm_mday=8, tm_hour=12, tm_min=9, tm_sec=9, tm_wday=5, tm_yday=39, tm_isdst=0)
t_UTC: time.struct_time(tm_year=2020, tm_mon=2, tm_mday=8, tm_hour=4, tm_min=9, tm_sec=9, tm_wday=5, tm_yday=39, tm_isdst=0)
Sat Feb 8 12:09:09 2020
"""
time.process_time():随意选取一个时间点,记录现在时间到该时间点的间隔秒数,不记录sleep
import time
t_local = time.localtime()
print(time.strftime("%Y-%m-%d %H:%m:%S", t_local))
"""
2020-02-08 12:16:51
"""
time.sleep(m):休眠m秒
随机数在计算机应用中十分常见,Python通过random库提供各种伪随机数,基本可以用于除加密解密算法外的大多数工程应用
如果不设置随机种子,以系统当前时间为默认值
(3) randrange(a, b, step):产生[a,b)之间以step为步长的随机整数
from random import *
numbers = [uniform(2.1, 3.5) for i in range(10)]
print(numbers)
"""
[2.1929577529017896, 2.1924724048023942, 2.774429454086627,
2.8437011579509877, 2.124246843787783, 3.28800355840583,
2.1034517877154464, 2.1154575533581137, 2.1230887166888635, 3.209706038847072]
"""
choice(['win', 'lose', 'draw'])
choice("python")
from random import *
print(choices(['win', 'lose', 'draw'], [4, 4, 2], k=10))
"""
['draw', 'lose', 'lose', 'win', 'draw',
'draw', 'lose', 'lose', 'win', 'win']
"""
sample(pop, k)——从pop类型中随机选取k个元素,以列表类型返回
import matplotlib.pyplot as plt
from random import *
res = [gauss(0, 1) for i in range(10000)]
plt.hist(res, bins=1000)
plt.show()
collections.namedtuple(typename, filed_name, *, rename=False, defaults=None, module=None)
- typename是元组名字,field_names是域名
import collections
Point = collections.namedtuple("Point", ["x", "y"])
p = Point(1, y=2) # Point(x=1, y=2)
# 可以调用属性
print(p.x)
print(p.y)
# 有元组的属性
print(p[0])
print(p[1])
x, y = p
print(x, y)
# 确实是元组的子类
print(isinstance(p, tuple)) # True
模拟扑克牌
"""
Counter({‘奶‘: 5, ‘牛‘: 2, ‘找‘: 1, ‘刘‘: 1, ‘买‘: 1})
Counter({‘blue‘: 3, ‘red‘: 2, ‘green‘: 1})
"""
print(cat_color.most_common(2)) # [('blue', 3), ('red', 2)]
print(list(cat_str.elements()))
# ['牛', '牛', '奶', '奶', '奶', '奶', '奶', '找', '刘', '买']
c = Counter(a=3, b=1)
d = Counter(a=1, b=2)
print(c+d)
# Counter({'a': 4, 'b': 3})
列表访问数据非常快速,插入和删除非常慢——通过移动元素位置来实现,特别是insert(0, v)和pop(0),在列表开始进行的插入和删除操作
双向列表可以方便的在队列两边搞笑、快速的增加和删除元素
from collections import deque
d = deque('cde')
d.append("f") # 在右端添加
d.appendleft("g") # 在左端添加
print(d) # deque(['g', 'c', 'd', 'e', 'f'])
d.pop() # 右端删除
d.popleft() # 左端删除
print(d) # deque(['c', 'd', 'e'])
"""
Counter({'奶': 5, '牛': 2, '找': 1, '刘': 1, '买': 1})
Counter({'blue': 3, 'red': 2, 'green': 1})
"""
其他用法参照官网
import itertools
for i in itertools.product('ABC', '01'):
print(i)
for i in itertools.product('ABC', repeat=3):
print(i)
"""
('A', '0')
('A', '1')
('B', '0')
('B', '1')
('C', '0')
('C', '1')
('A', 'A', 'A')
('A', 'A', 'B')
('A', 'A', 'C')
('A', 'B', 'A')
('A', 'B', 'B')
('A', 'B', 'C')
('A', 'C', 'A')
('A', 'C', 'B')
('A', 'C', 'C')
('B', 'A', 'A')
('B', 'A', 'B')
('B', 'A', 'C')
('B', 'B', 'A')
('B', 'B', 'B')
('B', 'B', 'C')
('B', 'C', 'A')
('B', 'C', 'B')
('B', 'C', 'C')
('C', 'A', 'A')
('C', 'A', 'B')
('C', 'A', 'C')
('C', 'B', 'A')
('C', 'B', 'B')
('C', 'B', 'C')
('C', 'C', 'A')
('C', 'C', 'B')
('C', 'C', 'C')
"""
- (3)combinations——组合
- (4) combinations_with_replacement——元素可重复组合
(2)permutations——排列
import itertools
for i in zip("ABC", "012", "xyz"):
print(i)
"""
('A', '0', 'x')
('B', '1', 'y')
('C', '2', 'z')
"""
# 长度不一致是,执行到最短的对象处就停止
for i in zip("ABC", "012345"):
print(i)
"""
('A', '0')
('B', '1')
('C', '2')
"""
import itertools
for i in itertools.zip_longest("ABC", "012345"):
print(i)
"""
('A', '0')
('B', '1')
('C', '2')
(None, '3')
(None, '4')
(None, '5')
"""
for i in itertools.zip_longest("ABC", "012345", fillvalue="?"):
print(i)
"""
('A', '0')
('B', '1')
('C', '2')
('?', '3')
('?', '4')
('?', '5')
"""
(3) repeat(object, times)——重复:创建一个迭代器,不断重复object,除非设定参数times,否则将无限重复
import itertools
for i in itertools.chain("ABC", "012"):
print(i)
"""
A
B
C
0
1
2
"""
for i in enumerate("Python", start=1):
print(i)
"""
(1, ‘P‘)
(2, ‘y‘)
(3, ‘t‘)
(4, ‘h‘)
(5, ‘o‘)
(6, ‘n‘)
"""
import itertools
animal = ['duck', 'eagle', 'rat', 'giraffe', 'bear', 'bat', 'dolphin', 'shark', 'lion']
# 按照长度进行排序
animal.sort(key=len)
print(animal)
"""
['rat', 'bat', 'duck', 'bear', 'lion', 'eagle', 'shark', 'giraffe', 'dolphin']
"""
for key, group in itertools.groupby(animal, key=len):
print(key, list(group))
"""
3 ['rat', 'bat']
4 ['duck', 'bear', 'lion']
5 ['eagle', 'shark']
7 ['giraffe', 'dolphin']
"""
# 按照首字母大小排序
animal.sort(key=lambda x: x[0])
print(animal)
"""
['bat', 'bear', 'duck', 'dolphin', 'eagle', 'giraffe', 'lion', 'rat', 'shark']
"""
for key, group in itertools.groupby(animal, key=lambda x: x[0]):
print(key, list(group))
"""
b ['bat', 'bear']
d ['duck', 'dolphin']
e ['eagle']
g ['giraffe']
l ['lion']
r ['rat']
s ['shark']
"""
标签:raw ges for ati shuf 常见 删除元素 设定 整数
原文地址:https://www.cnblogs.com/lyszyl/p/12285957.html