Python常用模块re,collections,random

来源:互联网 发布:学高等数学的软件 编辑:程序博客网 时间:2024/06/16 16:24
本文简要介绍Python常用模块中的正则表达式处理re模块,集合类collections模块,随机数处理random模块的相关内容。

re模块

它是正则表达式处理模块,正则表达式是匹配字符串的有利手段。

例如:用\d可以匹配一个数字,\w可以匹配一个字母或数字,所以,'00\d'可以匹配'007',但无法匹配'00A''\w\w\d'可以匹配'py3'

           .可以匹配任意字符,所以:'py.'可以匹配'pyc''pyo''py!'等等。

           要匹配变长的字符,在正则表达式中,用*表示任意个字符(包括0个),用+表示至少一个字符,用?表示0个或1个字符,用{n}表示n个字符,用{n,m}表示n-m个字符。

有关正则表达式的介绍请看另外一片博文“正则表达式”:

import reif re.match(r'^\d{3}\-\d{3,8}$', '010-12345'):  # 从字符串的开始匹配一个模式    print 'ok'else:    print 'failed'print 'a,b;; c  d'.split(' ')  # 直接切分字符串print re.split(r'[\s\,\;]+', 'a,b;; c  d')  # 用正则表达式的方法text = "JGood is a handsome boy, he is cool, clever, and so on..."if re.search(r'handsome', text):    print 'ok'else:    print 'failed'print re.sub(r'\s+', '-', text)  # 替换字符串的匹配项print re.findall(r'\w*s\w*', text)regex = re.compile(r'\w*s\w*')  # 编译成一个正则表达式以便以后使用print regex.findall(text)# ok# ['a,b;;', 'c', '', 'd']# ['a', 'b', 'c', 'd']# ok# JGood-is-a-handsome-boy,-he-is-cool,-clever,-and-so-on...# ['is', 'handsome', 'is', 'so']# ['is', 'handsome', 'is', 'so']

collections模块

collections是Python内建的一个集合模块,提供了许多有用的集合类。

from collections import namedtuple, deque, defaultdict, OrderedDict, Counterlst = ['zhao', 'zhao', 'wang']c = Counter(lst)print cc.update(['wang', ])print c# Counter({'zhao': 2, 'wang': 1})# Counter({'zhao': 2, 'wang': 2})# 使用dict时,Key是无序的。在对dict做迭代时,我们无法确定Key的顺序。# 如果要保持Key的顺序,可以用OrderedDict:d = dict([('a', 1), ('b', 2), ('c', 3)])print dod = OrderedDict([('a', 1), ('b', 2), ('c', 3)])print od# 注意,OrderedDict的Key会按照插入的顺序排列,不是Key本身排序:od['z'] = 1print od.keys()  # 按照插入的Key的顺序返回# {'a': 1, 'c': 3, 'b': 2}# OrderedDict([('a', 1), ('b', 2), ('c', 3)])q = deque(['a', 'b', 'c'])q.append('x')q.appendleft('y')print q# deque(['y', 'a', 'b', 'c', 'x'])dd = defaultdict(lambda: 'N/A')dd['key1'] = 'abc'print dd['key1'], dd['key2']# abc N/A# namedtuple可以很方便地定义一种数据类型,它具备tuple的不变性,又可以根据属性来引用Point = namedtuple('Point', ['x', 'y'])p = Point(1, 2)print "p.x =", p.x, ", p.y =", p.yprint isinstance(p, Point)print isinstance(p, tuple)# p.x = 1 , p.y = 2# True# True

random模块

用于参数各种随机数,简单方便。

import randomprint random.random()  # 用于生成一个0到1的随机符点数: 0 <= n < 1.0print random.uniform(10, 20)  # 用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。print random.randint(12, 20)  # 生成的随机数n: 12 <= n <= 20print random.randrange(1, 10, 1)  # 按指定基数递增的集合中 获取一个随机数。print random.choice(["JGood", "is", "a", "handsome", "boy"])p = ["Python", "is", "powerful", "simple", "and so on..."]random.shuffle(p)  # 用于将一个列表中的元素打乱print plist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]slice = random.sample(list, 5)  # 从list中随机获取5个元素,作为一个片断返回print sliceprint list  # 原有序列并没有改变。

0 0
原创粉丝点击