Python之初识模块

Collections模块

在内置数据类型(dict、list、set、tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter、deque、defaultdict、namedtuple和OrderedDict等。

1.namedtuple: 生成可以使用名字来访问元素内容的tuple

2.deque: 双端队列,可以快速的从另外一侧追加和推出对象

3.Counter: 计数器,主要用来计数

4.OrderedDict: 有序字典

5.defaultdict: 带有默认值的字典

namedtuple:

from collections import namedtuple
Point = namedtuple('point', ['x', 'y', 'z'])
p1 = Point(1,2,3)
print(p1.x,p1.y,p1.z)

deque:

from collections import deque
d_q = deque([1,2,3])
d_q.append(4)
d_q.appendleft(5)
print(d_q)
d_q.pop()
print(d_q)
d_q.popleft()
print(d_q)

OrderedDict:

from collections import OrderedDict
d = dict([('a',1),('b',2),('c',3)])  # 无序的
od = OrderedDict([('a',1),('b',2),('c',3)])  # 有序的
print(d)
print(od)

defaultdict :

from collections import defaultdict
values = [11, 22, 33,44,55,66,77,88,99]
my_dict = defaultdict(list)  # 字典的值默认为列表类型,必须是可调用的
for value in  values:
    if value>66:
        my_dict['k1'].append(value)
    else:
        my_dict['k2'].append(value)
print(my_dict)

Counter:

from collections import  Counter
c = Counter('abcdeabcdabcaba')  # 统计元素出现的次数
print(c)
>>>Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1})

time模块

 表示时间的三种方式:时间戳(timestamp):表示从1970年1月1日00:00:00开始按秒计算的偏移量

          格式化时间字符串(Format string): ‘1999-12-06’

          元组(struct_time):struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)

%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 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身
日期格式化符号
import time
print(time.time())  # 时间戳
print(time.strftime("%Y-%m-%d %H:%M:%S"))  # 时间字符串
print(time.localtime())  # 时间元组:localtime将一个时间戳转换为当前时区的struct_time
>>>1557738559.0358012
>>>2019-05-13 17:09:19
>>>time.struct_time(tm_year=2019, tm_mon=5, tm_mday=13, tm_hour=17, tm_min=9, tm_sec=19, tm_wday=0, tm_yday=133, tm_isdst=0)

几种时间格式的转换关系:

# 时间戳转结构化时间
t = time.time()
print(time.localtime(t))  # 本地时间
print(time.gmtime(t))  # UTC时间(英国伦敦时间)
# 结构化时间转时间戳
print(time.mktime(time.localtime(1500000000)))
# 结构化时间转字符串时间
# time.strftime("格式定义","结构化时间")  结构化时间参数若不传,则显示当前时间
print(time.strftime('%Y-%m-%d %X',time.localtime(1500000000)))
# 字符串时间转结构化时间
print(time.strptime('2010-12-1','%Y-%m-%d'))

#结构化时间 --> %a %b %d %H:%M:%S %Y串
#time.asctime(结构化时间) 如果不传参数,直接返回当前时间的格式化串
print(time.asctime(time.localtime(1500000000)))
print(time.asctime())
#时间戳 --> %a %b %d %H:%M:%S %Y串
#time.ctime(时间戳)  如果不传参数,直接返回当前时间的格式化串
print(time.ctime(1500000000))
print(time.ctime())

random模块

import random
print(random.random())  # 随机一个大于0小于1的小数
print(random.uniform(1,3))  # 随机一个大于1小于3的小数
print(random.randint(1,5))  # 随机一个大于等于1小于等于5的数
print(random.randrange(0,10,2))  # 随机一个大于等于0 小于等于10 的偶数
print(random.choice([1,2,3,4]))  # 随机选一个数
print(random.sample([1,2,3,4],2))  # random.sample([],n)随机选区N个
l = [1,2,3,4]  
random.shuffle(l)  # 打乱顺序
print(l)
print(chr(random.randint(65,90)))  # 随机字母
原文地址:https://www.cnblogs.com/xfdhh/p/10853659.html