常见模块 collections 模块 re模块 random模块
2018-02-28 00:00
141 查看
一collection模块
from collections import namedtuple point=namedtuple('point',['x','y']) p=point(1,2) print(p.x) print(p.y) from collections import deque q=deque(['a','b','c']) q.append('x') q.appendleft('y') print(q) q.popleft() print(q) from collections import OrderedDict s1=dict([('a',1),('b',2),('c',3)]) s=OrderedDict([('a',1),('b',2),('c',3)]) print(s) print(s1) 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) print(my_dict) from collections import Counter s=Counter('aabbddffeecc') print(s) random模块 import random 任意小数 print(random.random()) print(random.uniform(1,3)) 任意整数 print(random.randint(1,5)) print(random.randrange(1,5,2)) 随机返回一个任意数 print(random.choice([1,'2',[2,3]])) print(random.sample([1,'2',[2,3]],3)) item=[1,2,3,4,5,6] random.shuffle(item) print(item) re模块 import re ret=re.findall('a','aaabsoulsalt') print(ret) ret=re.search('a','aaabsoulsalt').group() print(ret) ret=re.match('a','abbcc').group() print(ret) ret=re.split('[a,b]','abcde') print(ret) ret=re.sub('\d','h','abcde123',1) print(ret) ret=re.sub('\d','h','abcde123') print(ret) 打印扑克牌花色 from collections import namedtuple point=namedtuple('point',['x','y']) for x in range(1,14): if x==11:x='J' if x == 12: x = 'Q' if x == 13: x = 'K' for y in ('黑桃','红桃','梅花','方片'): p=point(x,y) print(p)
相关文章推荐
- Python全栈开发之5、几种常见的排序算法以及collections模块提供的数据结构
- python常见模块之collections模块
- Python模块安装: pip常见的使用方法
- Linux模块编译常见错误之一
- Python提供了以下几个内置tools模块:operator collections itertools functools
- python 常用的模块(collections)转
- python常见模块之time模块
- Python常用内建模块之collections
- Xilinx System Generator模块中各个常见选项的含义
- collections在java中的常见用法
- 【python】collections模块(有序字典,计数器,双向队列)
- Python3之collections模块
- HC05 蓝牙模块常见问题
- 简单掌握Python的Collections模块中counter结构的用法
- L13 ansible 基础应用与常见模块
- python collections模块
- python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客
- ASP.NET常见模块:在线文件管理模块的设计与开发
- Python:collections模块的用法
- Nginx源码分析:核心模块剖析及常见问题