可命名元祖namedtuple
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
deque双端队列
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
队列queue
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
有序的字典(key是保持顺序的)OrderedDict
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
使用字典时,如果key不存在时,返回一个默认值,可以使用defaultdict
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Counter计数,它是一个无序的容器类型,以字典的键值对形式存储,其中元素作为key,其计数作为value。计数值可以是任意的Interger(包括0和负数)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
时间模块time
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
import time# ret = time.time() #时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量# print(ret) #1510647685.4264479# print(type(ret))# 格式化的时间字符串strftime# ret = time.strftime('%Y-%m-%d %A %H-%M-%S')# print(ret)# ret = time.localtime(1510647685.4264479)# print(ret)# 元组(struct_time):struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)# s = time.time()# # print(s)# ret = time.struct_time(s)# print(ret)# ret = time.gmtime()# print(ret)# ret1 = time.localtime()# print(ret1)# print(time.gmtime(1222222222) )# print(time.localtime(1222222222) )# print(time.localtime())# ret = time.localtime(1222222222)# print(ret)# print(time.mktime(ret))# ret = time.strftime('%Y-%m-%d')# print(ret)# ret1 = time.strptime("2017-03-16","%Y-%m-%d")# print(ret1)# # ret2 = time.mktime(ret1)# # print(ret2)## ret2 = time.strftime('%Y-%m',ret1)# print(ret2)# true_time=time.mktime(time.strptime('2017-09-11 08:30:00','%Y-%m-%d %H:%M:%S'))# time_now=time.mktime(time.strptime('2017-09-12 11:00:00','%Y-%m-%d %H:%M:%S'))# dif_time=time_now-true_time# struct_time=time.gmtime(dif_time)# print(struct_time)# print('过去了%d年%d月%d天%d小时%d分钟%d秒'%(struct_time.tm_year-1970,struct_time.tm_mon-1,# struct_time.tm_mday-1,struct_time.tm_hour,# struct_time.tm_min,struct_time.tm_sec))# ret = time.asctime(time.gmtime())# print(ret)# ret1 = time.ctime(2000000000)# print(ret1)
sys模块
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
random模块
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
# 随机小数# print(random.random()) #0-1之间的小数# print(random.uniform(10,20)) #n,m之间的小数# # 生成随机整数# print(random.randint(1,2))# print(random.randrange(2))# print(random.randrange(1,2))# print(random.randrange(1,5,2))# # 从一个序列中随机选择:一个choice,多个sample# 随机选择一个返回choice# print(random.choice(1,2,3)) #1或者2或者3#随机选择多个返回,返回的个数为函数的第二个参数sample# print(random.sample([1,25,[1,2]],2)) #列表元素任意2个组合# 打乱一个序列的顺序shuffle# item = [1,3,5,7,9]# random.shuffle(item) #改变了原列表# print(item) #应用:洗牌,抽奖,测试一些排序算法# 练习:生成随机验证码# 方法一# print(random.randint(100000,999999))# 方法二:# print(random.randrange(100000,1000000))# 方法三# l = []# for i in range(6):# num = random.randint(0,9)# l.append(str(num))# print(''.join(l))# 生成一个6位数字随机验证码。不能有重复,会少了好多种可能# print(random.sample(range(0,10),6))# l = []# for i in range(6):# alpha = chr(random.randint(65,90))# num = str(random.randint(0,9))# ret = random.choice([alpha,num])# l.append(ret)# print(''.join(l))
os模块
os模块是与操作系统交互的一个接口
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
'''os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径os.chdir("dirname") 改变当前脚本工作目录;相当于shell下cdos.curdir 返回当前目录: ('.')os.pardir 获取当前目录的父目录字符串名:('..')os.makedirs('dirname1/dirname2') 可生成多层递归目录os.removedirs('dirname1') 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推os.mkdir('dirname') 生成单级目录;相当于shell中mkdir dirnameos.rmdir('dirname') 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirnameos.listdir('dirname') 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印os.remove() 删除一个文件os.rename("oldname","newname") 重命名文件/目录os.stat('path/filename') 获取文件/目录信息os.sep 输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"os.linesep 输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"os.pathsep 输出用于分割文件路径的字符串 win下为;,Linux下为:os.name 输出字符串指示当前使用平台。win->'nt'; Linux->'posix'os.system("bash command") 运行shell命令,直接显示os.popen("bash command) 运行shell命令,获取执行结果os.environ 获取系统环境变量os.pathos.path.abspath(path) 返回path规范化的绝对路径 os.path.split(path) 将path分割成目录和文件名二元组返回 os.path.dirname(path) 返回path的目录。其实就是os.path.split(path)的第一个元素 os.path.basename(path) 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。 即os.path.split(path)的第二个元素os.path.exists(path) 如果path存在,返回True;如果path不存在,返回Falseos.path.isabs(path) 如果path是绝对路径,返回Trueos.path.isfile(path) 如果path是一个存在的文件,返回True。否则返回Falseos.path.isdir(path) 如果path是一个存在的目录,则返回True。否则返回Falseos.path.join(path1[, path2[, ...]]) 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略os.path.getatime(path) 返回path所指向的文件或者目录的最后访问时间os.path.getmtime(path) 返回path所指向的文件或者目录的最后修改时间os.path.getsize(path) 返回path的大小'''注意:os.stat('path/filename') 获取文件/目录信息 的结构说明stat 结构:st_mode: inode 保护模式st_ino: inode 节点号。st_dev: inode 驻留的设备。st_nlink: inode 的链接数。st_uid: 所有者的用户ID。st_gid: 所有者的组ID。st_size: 普通文件以字节为单位的大小;包含等待某些特殊文件的数据。st_atime: 上次访问的时间。st_mtime: 最后一次修改的时间。st_ctime: 由操作系统报告的"ctime"。在某些系统上(如Unix)是最新的元数据更改的时间,在其它系统上(如Windows)是创建时间(详细信息参见平台的文档)。