3.29python学习笔记


collections模块

这个模块实现了特定目标的容器,以提供Python标准内建容器 dict、list、set、tuple 的替代选择。

1.ametuple   具名元组
 """
    namedtuple('名称',[名字1,名字2,...])
    namedtuple('名称','名字1 名字2 ...')
 """
2.queue        内置队列模块(解释器自带) # 内置队列模块:FIFO
 import queue
    q = queue.Queue()  # 初始化队列
    q.put('添加内容')  #往队列中添加元素
    #从队列中获取元素
    print(q.get())  # 值去没了则原地等待
    
3.deque        双端队列模块
from collections import deque
    q = deque([11,22,33])
    q.append(44)  # 从右边添加
    q.appendleft(55)  # 从左边添加
    print(q.pop())  # 从右边取值
    print(q.popleft())  # 从做边取值
    
4.orderedDict  有序字典
from collections import OrderedDict
    order_dict = OrderedDict([('name', 'lili'), ('age', 17)])
    order_dict['hobby'] = 'read'
    print(order_dict)
    
5.defaultdict  默认字典
from collections import defaultdict
values = [11, 22, 33,44,55,66,77,88,99,90]
my_dict = defaultdict(list)
for value in values:
    if value > 60:
        my_dict['k1'].append(value)
    else:
        my_dict['k2'].append(value)
print(my_dict)

6.counter         计数器
 from collections import Counter  # 计数器
 res = 'hellohello' 
 res = Counter(res)
    # ({'l': 4, 'h': 2, 'e': 2, 'o': 2})

time与datetime模块

  • 它们是 Python 中与时间处理有关的标准库模块
1.时间戳 (timestamp)
通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量
返回时间戳方式的函数主要有time( ),clock( )等, 运行
	print(type(time.time())), 返回的是float类型

2.格式化的时间字符串 (Format String)
import time
print(time.strftime("%Y-%m-%d %H:%M:%S %p"))  
符号 说明
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
3.结构化的时间 (struct_time) 元组
struct_time 元组共有九个元素
返回 struct_time 的函数主要有 : localtime( ), gmtime( ), strptime( )
    import time

?localtime( )
print(time.localtime())        # 本地时区元组(struct_time) 
'''输出
time.struct_time(tm_year=2020, tm_mon=12, tm_mday=18, tm_hour=8, tm_min=45, tm_sec=9, tm_wday=4, tm_yday=353, tm_isdst=0)\
'''
print(type(time.localtime()))  # 
print(time.localtime()[3])     # 8

?gmtime( )
print(time.gmtime())           # UTC时区元组(struct_time)
'''输出
time.struct_time(tm_year=2020, tm_mon=12, tm_mday=18, tm_hour=0, tm_min=45, tm_sec=9, tm_wday=4, tm_yday=353, tm_isdst=0)
'''
print(type(time.gmtime()))     # 
print(time.gmtime()[0])        # 2022

时间格式转换关系

格式化的字符串时间与时间戳之间的转换都是以结构化的时间作为中转站来进行操作的

结构化时间与时间戳之间的转化

time.mktime([结构化时间]) : “struct_time” 转换 “timestamp”
time.localtime([时间戳]) : “timestamp” 转换 “struct_time” 本地时区
time.gmtime([时间戳]) : “timestamp” 转换 “struct_time” UTC时区
time.gmtime([time.time()])
import time

?"struct_time" 转换 "timestamp"
print(time.mktime(time.localtime()))   # 1608259357.0
print(time.mktime(time.gmtime()))      # 1608259357.0

?"timestamp" 转换 "struct_time"
print(time.localtime(456465.4685))  # 返回的是"struct_time"本地时区元组
print(time.localtime(time.time()))  # 里面不填时间戳默认就是当前时间戳"time.time()"

print(time.gmtime(456465.4685))     # 返回的是"struct_time"UTC时区元组
print(time.gmtime(time.time()))
#结构化时间与格式化字符串时间之间的转换
time.strftime([时间格式],[结构化时间]) : “struct_time” 转换 “format time”
time.strptime([格式化的字符串时间],[时间格式]) : “format time” 转换 “struct_time”

random随机数模块

#random.random() 
用于生成一个0-1的随机浮点数:0<=n<1.0
a = random.random()
b = random.random()
print(a,b)  
0.14950126763787908 0.18635283756700527
#random.uniform(a,b) 
用于生成一个指定范围内的随机浮点数,两个参数中,一个是上限,一个是下限,位置可以互换。if a