collections&time&random模块
一、collections模块
在内置数据类型(dict、list、set、tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter、deque、defaultdict、namedtuple和OrderedDict等。
1.namedtuple: 生成可以使用名字来访问元素内容的tuple
2.deque: 双端队列,可以快速的从另外一侧追加和推出对象
3.Counter: 计数器,主要用来计数
4.OrderedDict: 有序字典
5.defaultdict: 带有默认值的字典
namedtuple
1 from collections import namedtuple 2 Point=namedtuple('point',['x','y']) 3 p=Point(1,2) 4 print(p) #point(x=1, y=2) 5 print(p.x) 6 print #1
1 from collections import namedtuple 2 Circle=namedtuple('circle',['x','y','r']) 3 c=Circle(1,2,3) 4 print(c) #circle(x=1, y=2, r=3)
deque
1 from collections import deque 2 q=deque(['a','b','c']) 3 q.append('x') #从后面放数据 4 q.appendleft('y') #从前面放入数据 5 print(q) #deque(['y', 'a', 'b', 'c', 'x']) 6 7 q.insert(1,'ab') 8 print(q) #deque(['y', 'ab', 'a', 'b', 'c', 'x']) 9 print(q.pop()) #x,从后面取数据 10 print(q.popleft()) #y,从前面取数据
OrderedDict
1 from collections import OrderedDict 2 d=dict([('a',1),('b',2),('c',3)]) #创建列表的另一种方法 3 print(d) 4 D=OrderedDict([('a',1),('b',2),('c',3)]) 5 print(D) 6 for i in D: 7 print(i) 8 #结果:a,b,c D是可迭代的 9 10 od=OrderedDict() 11 od['a']=5 12 od['b']=1 13 od['c']=2 14 print(od) #OrderedDict([('a', 5), ('b', 1), ('c', 2)]),OrderedDict的Key会按照插入的顺序排列,不是Key本身排序;
defaultdict
1 before: 2 values=[11, 22, 33,44,55,66,77,88,99,90] 3 my_dict={} 4 for i in values: 5 if i>66: 6 if 'k1' in my_dict.keys(): 7 my_dict['k1'].append(i) 8 else: 9 my_dict['k1']=[i] 10 else: 11 if 'k2' in my_dict.keys(): 12 my_dict['k2'].append(i) 13 else: 14 my_dict['k2']=[i] 15 print(my_dict) 16 17 #结果:{'k2': [11, 22, 33, 44, 55, 66], 'k1': [77, 88, 99, 90]} 18 19 after: 20 from collections import defaultdict 21 values = [11, 22, 33,44,55,66,77,88,99,90] 22 my_dict=defaultdict(list) 23 for value in values: 24 if value>66: 25 my_dict['k1'].append(value) 26 else: 27 my_dict['k2'].append(value) 28 print(my_dict) #defaultdict(<class 'list'>, {'k1': [77, 88, 99, 90], 'k2': [11, 22, 33, 44, 55, 66]}) 29 30 #defaultdict为一个不存在的键提供默认值,从而避免KeyError异常.
Counter
1 from collections import Counter 2 c = Counter('abcdeabcdabcaba') 3 print(c) #Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1}) 4 5 Counter它是一个无序的容器类型,以字典的键值对形式存储,其中元素作为key,其计数作为value。
二、time模块
在Python中,通常有这三种方式来表示时间:时间戳、元组(struct_time)、格式化的时间字符串。
(1)时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。
(2)格式化的时间字符串(Format String): ‘1999-12-06’
1 %y 两位数的年份表示(00-99) 2 %Y 四位数的年份表示(000-9999) 3 %m 月份(01-12) 4 %d 月内中的一天(0-31) 5 6 %H 24小时制小时数(0-23) 7 %I 12小时制小时数(01-12) 8 %M 分钟数(00=59) 9 %S 秒(00-59) 10 11 %a 本地简化星期名称 12 %A 本地完整星期名称 13 %b 本地简化的月份名称 14 %B 本地完整的月份名称 15 %c 本地相应的日期表示和时间表示 16 %j 年内的一天(001-366) 17 %p 本地A.M.或P.M.的等价符 18 %U 一年中的星期数(00-53)星期天为星期的开始 19 %w 星期(0-6),星期天为星期的开始 20 %W 一年中的星期数(00-53)星期一为星期的开始 21 %x 本地相应的日期表示 22 %X 本地相应的时间表示 23 %Z 当前时区的名称 24 %% %号本身
(3)元组(struct_time) :struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)
| 索引(Index) | 属性(Attribute) | 值(Values) |
|---|---|---|
| 0 | tm_year(年) | 比如2011 |
| 1 | tm_mon(月) | 1 - 12 |
| 2 | tm_mday(日) | 1 - 31 |
| 3 | tm_hour(时) | 0 - 23 |
| 4 | tm_min(分) | 0 - 59 |
| 5 | tm_sec(秒) | 0 - 60 |
| 6 | tm_wday(weekday) | 0 - 6(0表示周一) |
| 7 | tm_yday(一年中的第几天) | 1 - 366 |
| 8 | tm_isdst(是否是夏令时) | 默认为0 |
1 import time 2 print(time.time()) #1546005206.1939626 3 4 print(time.strftime("%Y-%m-%d %X")) #2018-12-28 21:53:36 5 6 ret=time.time() 7 print(time.localtime(ret)) 8 # 结果:time.struct_time(tm_year=2018, tm_mon=12, tm_mday=28, tm_hour=20, tm_min=26, tm_sec=37, tm_wday=4, tm_yday=362, tm_isdst=0) 9 10 #时间戳--->结构化时间 11 print(time.localtime(100000000)) #当地时间。例如我们现在在北京执行这个方法:与UTC时间相差8小时,UTC时间+8小时 = 北京时间 12 print(time.gmtime(100000000)) #UTC时间,与英国伦敦当地时间一致 13 #结果: 14 time.struct_time(tm_year=1973, tm_mon=3, tm_mday=3, tm_hour=17, tm_min=46, tm_sec=40, tm_wday=5, tm_yday=62, tm_isdst=0) 15 time.struct_time(tm_year=1973, tm_mon=3, tm_mday=3, tm_hour=9, tm_min=46, tm_sec=40, tm_wday=5, tm_yday=62, tm_isdst=0) 16 17 #结构化时间--->时间戳 18 ret=time.localtime(100000000) 19 ret1=time.mktime(ret) 20 print(ret1) #100000000.0 21 print(type(ret1)) #<class 'float'> 22 23 #结构化时间--->时间字符串 24 ret=time.localtime(100000000) 25 ret2=time.strftime("%Y-%m-%d %H:%M:%S",ret) 26 print(ret2) #1973-03-03 17:46:40 27 28 #时间字符串--->结构化时间 29 ret3=time.strptime(ret2,"%Y-%m-%d %H:%M:%S") 30 print(ret3) 31 32 #时间差 33 time1=time.mktime(time.strptime('2018-10-31 23:11:11','%Y-%m-%d %H:%M:%S')) 34 time2=time.mktime(time.strptime('2018-11-25 13:13:43','%Y-%m-%d %H:%M:%S')) #先将时间字符串转化为时间戳 35 ret=time2-time1 36 struct_t=time.localtime(ret) #再将时间戳转化为结构化时间 37 print("相差了%d年%d月%s天%d小时%d分%d秒"%(struct_t.tm_year-1970,struct_t.tm_mon-1,struct_t.tm_mday-1,struct_t.tm_hour,struct_t.tm_min,stru
三、random模块
1 import random 2 3 #随机小数 4 print(random.random()) # 大于0且小于1之间的小数 5 print(random.uniform(1,3)) #大于1小于3的小数 6 7 #随机整数 8 #print(random.randint()) #randint() missing 2 required positional arguments: 'a' and 'b' 9 print(random.randint(1,5)) 10 print(random.randrange(1,10,2)) #大于等于1且小于10之间的奇数 11 12 #随机选择一个返回 13 a=random.choice([1,2,3,4,5,'a']) 14 # print(a) 15 16 #随机选择多个返回,返回的个数为函数的第二个参数 17 b=random.sample([1,'23',[4,5]],3) 18 print(b) 19 20 #打乱列表顺序 21 item=[1,3,5,78,987] 22 random.shuffle(item) 23 print(item) 24 25 #练习:生成随机验证码 26 27 import random 28 code='' 29 for i in range (6): 30 zimu = chr(random.randint(65, 90)) 31 suzi = random.randint(0, 9) 32 add=random.choice([zimu,suzi]) 33 code=''.join([code,str(add)]) 34 35 print(code)
posted on 2018-12-29 16:52 cherish-LL 阅读(204) 评论(0) 收藏 举报
浙公网安备 33010602011771号