ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

3.29python学习笔记

2022-04-18 00:31:21  阅读:224  来源: 互联网

标签:struct python gmtime 笔记 print tm time 3.29 localtime


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()))  # <class 'time.struct_time'>
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()))     # <class 'time.struct_time'>
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<b,则生成的随机数n:a<=n<b;else 同理。
#random.randint(a,b) 
用于声场一个指定范围内的整数。其中,参数a是下限,b是上限,生成的随机数n:a<=n<=b。
#random.randrange([start],stop,[step]) 
从指定范围中,按指定基数递增的集合中获取一个随机数。参数必须为整数,start默认为0,step默认为1,所以,写单个参数时,最小是1,不然会报错哦。
#random.choice(sequence) 
从序列中获取一个随机元素,参数sequence表示一个有序类型,泛指一系列类型,如list,tuple,字符串。
#random.shuffle(x,[random]) 
用于将一个列表中的元素打乱,即将列表中的元素随机排列。
#random.sample(sequence,k) 
从指定序列中随机获取指定长度的片段。sample函数不会修改原有的序列。

标签:struct,python,gmtime,笔记,print,tm,time,3.29,localtime
来源: https://www.cnblogs.com/zq0408/p/16157993.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有