## 常用内置模块collections模块 1.具名元组 ```python from collections import namedtuple #表示二位坐标系 n1 = namedtuple('车', ('num name')) # 可以用空格分开表示 res1 =n1(1, '轿车') res2 =n1(2,'老爷车') print(res1,res2) print(res1.num, res2.name) 队列 队列与堆栈 队列:先进先出 堆栈:先进后出 ``` ### 2.deque ```python from collections import deque d = deque([1,2,3]) print(d.pop()) print(d.popleft()) ``` ### 3.defaultdict ```python values = [11, 22, 33,44,55,66,77,88,99,90] my_dict = {} for value in values: if value>66: if my_dict.has_key('k1'): my_dict['k1'].append(value) else: my_dict['k1'] = [value] else: if my_dict.has_key('k2'): my_dict['k2'].append(value) else: my_dict['k2'] = [value] 原生字典解决方法 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>66: my_dict['k1'].append(value) else: my_dict['k2'].append(value) defaultdict字典解决方法 ``` ### 4.ordereddict顺序字典 ```python >>> from collections import OrderedDict >>> d = dict([('a', 1), ('b', 2), ('c', 3)]) >>> d # dict的Key是无序的 {'a': 1, 'c': 3, 'b': 2} >>> od = OrderedDict([('a', 1), ('b', 2), ('c', 3)]) >>> od # OrderedDict的Key是有序的 OrderedDict([('a', 1), ('b', 2), ('c', 3)]) >>> od = OrderedDict() >>> od['z'] = 1 >>> od['y'] = 2 >>> od['x'] = 3 >>> od.keys() # 按照插入的Key的顺序返回 ['z', 'y', 'x'] ``` ### 5.counter ```python from collections import Counter c = Counter('asdhjgajksd') print(c) ``` ## 常用内置模块时间模块 ### 1.time  ```python import time """ 三种时间表现形式 1.时间戳 秒数 2.结构化时间 主要是给计算机看的 人看不适应 3.格式化时间 主要是给人看的 """ time.time() # 获取当前时间戳 time.localtime() ''' time.struct_time(tm_year=2022, tm_mon=10, tm_mday=19, tm_hour=11, tm_min=32, tm_sec=50, tm_wday=2, tm_yday=292, tm_isdst=0) ''' time.strftime("%Y-%m-%d %H:%M:%S") time.strftime("%Y-%m-%d %X") time.sleep(1) # 运行到这停止多少秒 ``` 2.datetime  ```python from datetime import time,datetime,data print(datetime.now()) print(datetime.today()) print(datetime.utcnow()) # print(datetime.strftime()) # ''' datetime 年月日 时分秒 date 年月日 time 时分秒 ''' data.now() # 时分秒 print(datetime.strptime('2022-10-19','%Y-%m-%d')) print(datetime.strftime(datetime.today(),'%Y-%m-%d')) # print(datetime.time().day) print(time.strptime('2022-10-19', '%Y-%m-%d')) print(datetime.today()+timedelta(weeks=1)) datetime.timedelta(days = 3) date print(date.today()) print(date.today().strftime('%Y-%m-%d')) print(date.today().timetuple()) # 结构化时间 print(date.today().year) print(date.today().replace(2019)) # 替换时间 print(date.today().ctime()) print(date.today().toordinal()) print(date.today().weekday()) ``` ## 常用内置模块随机数模块 ```python random 模块 import random random.random() # 返回一个0~1之间的小数 random.randint(1,10) # 返回一个顾头顾尾的,随机一个整数 random.randrange(1,10,2) # 跟randint类似,但是randrange可以有步长 random.choice([1,2,3]) # 可迭代对象随机抽取一个返回 random.choices([1,2,3,4,5,6])#返回抽取的数据,以列表套原数据类型返回 random.simple('akjsghd',2)#返回抽取的指定个数数据,以列表套原数据类型返回 l1= [1,2,3,4] random.shuffle(l1) #放可变数据类型,可以随机打乱顺序,然后返回 '''产生图片验证码: 每一位都可以是大写字母 小写字母 数字 4位''' import string import random def get_code(num): code = '' for i in range(num): data = string.ascii_lowercase + string.ascii_uppercase + string.digits code += random.choice(data) return code print(get_code(10)) ``` ### string模块 ```python import string string.ascii_lowercase # 全部小写字母 string.ascii_uppercase # 全部大写字母 string.digits #0-9数字 ``` Last modification:October 19th, 2022 at 07:35 pm © 允许规范转载 Support 如果觉得我的文章对你有用,请随意赞赏 ×Close Appreciate the author Sweeping payments
Comment here is closed