标签:
一.冒泡排序原理整理图
二.递归流程图剖析
三.正则表达式内容整理
1.re模块:python中re模块提供了正则表达式相关操作
2.元字符的整理
. ^ $ * + ? {} [] | \
3. . 匹配除换行符以外的任意字符(可以通过加参数re.S匹配上换行符)
\w 匹配字母或数字或下划线或汉字
\s 匹配任意的空白符
\d 匹配数字
\b 匹配单词的开始或结束
^ 匹配字符串的开始
$ 匹配字符串的结束
4. * 重复零次或更多次(常用的)
+ 重复一次或更多次(常用的)
? 重复零次或一次(常用的)
{n} 重复n次
{n,} 重复n次或更多次
{n,m} 重复n到m次
5.march用法
# match,从起始位置开始匹配,匹配成功返回一个对象,未匹配成功返回None
# 无分组 r = re.match("h\w+", origin) print(r.group()) # 获取匹配到的所有结果 print(r.groups()) # 获取模型中匹配到的分组结果 print(r.groupdict()) # 获取模型中匹配到的分组结果 # 有分组 # 为何要有分组?提取匹配成功的指定内容(先匹配成功全部正则,再匹配成功的局部内容提取出来) r = re.match("h(\w+).*(?P<name>\d)$", origin) print(r.group()) # 获取匹配到的所有结果 print(r.groups()) # 获取模型中匹配到的分组结果 print(r.groupdict()) # 获取模型中匹配到的分组中所有执行了key的组 Demo
6.search用法
search,浏览整个字符串去匹配第一个,未匹配成功返回None
# 无分组 r = re.search("a\w+", origin) print(r.group()) # 获取匹配到的所有结果 print(r.groups()) # 获取模型中匹配到的分组结果 print(r.groupdict()) # 获取模型中匹配到的分组结果 # 有分组 r = re.search("a(\w+).*(?P<name>\d)$", origin) print(r.group()) # 获取匹配到的所有结果 print(r.groups()) # 获取模型中匹配到的分组结果 print(r.groupdict()) # 获取模型中匹配到的分组中所有执行了key的组 demo
7.findall用法
findall,获取非重复的匹配列表;如果有一个组则以列表形式返回,且每一个匹配均是字符串;如果模型中有多个组,则以列表形式返回,且每一个匹配均是元祖;
# 无分组 r = re.findall("a\w+",origin) print(r) # 有分组 origin = "hello alex bcd abcd lge acd 19" r = re.findall("a((\w*)c)(d)", origin) print(r) Demo
8.sub用法
sub,替换匹配成功的指定位置字符串
# 与分组无关 origin = "hello alex bcd alex lge alex acd 19" r = re.sub("a\w+", "999", origin, 2) print(r)
9.split用法
split,根据正则匹配分割字符串
# 无分组 origin = "hello alex bcd alex lge alex acd 19" r = re.split("alex", origin, 1) print(r) # 有分组 origin = "hello alex bcd alex lge alex acd 19" r1 = re.split("(alex)", origin, 1) print(r1) r2 = re.split("(al(ex))", origin, 1) print(r2) Demo
四.模块的整理
1.模块,用一砣代码实现了某个功能的代码集合。
类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个 .py 文件组成的代码集合就称为模块。
如:os 是系统相关的模块;file是文件操作相关的模块
模块分为三种:
2、定义模块
情景一:
情景二:
情景三:
3、导入模块
Python之所以应用越来越广泛,在一定程度上也依赖于其为程序员提供了大量的模块以供使用,如果想要使用模块,则需要导入。导入模块有一下几种方法:
1
2
3
4
|
import module from module.xx.xx import xx from module.xx.xx import xx as rename from module.xx.xx import * |
导入模块其实就是告诉Python解释器去解释那个py文件
那么问题来了,导入模块时是根据那个路径作为基准来进行的呢?即:sys.path
1
2
3
4
5
|
import sys print sys.path 结果: [ ‘/Users/wupeiqi/PycharmProjects/calculator/p1/pp1‘ , ‘/usr/local/lib/python2.7/site-packages/setuptools-15.2-py2.7.egg‘ , ‘/usr/local/lib/python2.7/site-packages/distribute-0.6.28-py2.7.egg‘ , ‘/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.10-x86_64.egg‘ , ‘/usr/local/lib/python2.7/site-packages/xlutils-1.7.1-py2.7.egg‘ , ‘/usr/local/lib/python2.7/site-packages/xlwt-1.0.0-py2.7.egg‘ , ‘/usr/local/lib/python2.7/site-packages/xlrd-0.9.3-py2.7.egg‘ , ‘/usr/local/lib/python2.7/site-packages/tornado-4.1-py2.7-macosx-10.10-x86_64.egg‘ , ‘/usr/local/lib/python2.7/site-packages/backports.ssl_match_hostname-3.4.0.2-py2.7.egg‘ , ‘/usr/local/lib/python2.7/site-packages/certifi-2015.4.28-py2.7.egg‘ , ‘/usr/local/lib/python2.7/site-packages/pyOpenSSL-0.15.1-py2.7.egg‘ , ‘/usr/local/lib/python2.7/site-packages/six-1.9.0-py2.7.egg‘ , ‘/usr/local/lib/python2.7/site-packages/cryptography-0.9.1-py2.7-macosx-10.10-x86_64.egg‘ , ‘/usr/local/lib/python2.7/site-packages/cffi-1.1.1-py2.7-macosx-10.10-x86_64.egg‘ , ‘/usr/local/lib/python2.7/site-packages/ipaddress-1.0.7-py2.7.egg‘ , ‘/usr/local/lib/python2.7/site-packages/enum34-1.0.4-py2.7.egg‘ , ‘/usr/local/lib/python2.7/site-packages/pyasn1-0.1.7-py2.7.egg‘ , ‘/usr/local/lib/python2.7/site-packages/idna-2.0-py2.7.egg‘ , ‘/usr/local/lib/python2.7/site-packages/pycparser-2.13-py2.7.egg‘ , ‘/usr/local/lib/python2.7/site-packages/Django-1.7.8-py2.7.egg‘ , ‘/usr/local/lib/python2.7/site-packages/paramiko-1.10.1-py2.7.egg‘ , ‘/usr/local/lib/python2.7/site-packages/gevent-1.0.2-py2.7-macosx-10.10-x86_64.egg‘ , ‘/usr/local/lib/python2.7/site-packages/greenlet-0.4.7-py2.7-macosx-10.10-x86_64.egg‘ , ‘/Users/wupeiqi/PycharmProjects/calculator‘ , ‘/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python27.zip‘ , ‘/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7‘ , ‘/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin‘ , ‘/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac‘ , ‘/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages‘ , ‘/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk‘ , ‘/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old‘ , ‘/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload‘ , ‘/usr/local/lib/python2.7/site-packages‘ , ‘/Library/Python/2.7/site-packages‘ ] |
如果sys.path路径列表没有你想要的路径,可以通过 sys.path.append(‘路径‘) 添加。
1
2
3
4
|
import sys import os project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(project_path) |
内置模块是Python自带的功能,在使用内置模块相应的功能时,需要【先导入】再【使用】
4.time & datetime模块用法
5.
OS模块用法五.字符串格式化的整理
Python的字符串格式化有两种方式: 百分号方式、format方式
百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存。[PEP-3101]
This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing ‘%‘ string formatting operator.
1、百分号方式
%[(name)][flags][width].[precision]typecode
注:Python中百分号格式化是不存在自动将整数转换成二进制表示的方式
常用格式化:重点
1
2
3
4
5
6
7
8
9
10
11
|
tpl = "i am %s" % "alex" tpl = "i am %s age %d" % ( "alex" , 18 ) tpl = "i am %(name)s age %(age)d" % { "name" : "alex" , "age" : 18 } tpl = "percent %.2f" % 99.97623 tpl = "i am %(pp).2f" % { "pp" : 123.425556 , } tpl = "i am %.2f %%" % { "pp" : 123.425556 , } |
2、Format方式
[[fill]align][sign][#][0][width][,][.precision][type]
常用格式化:重点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
tpl = "i am {}, age {}, {}" . format ( "seven" , 18 , ‘alex‘ ) tpl = "i am {}, age {}, {}" . format ( * [ "seven" , 18 , ‘alex‘ ]) tpl = "i am {0}, age {1}, really {0}" . format ( "seven" , 18 ) tpl = "i am {0}, age {1}, really {0}" . format ( * [ "seven" , 18 ]) tpl = "i am {name}, age {age}, really {name}" . format (name = "seven" , age = 18 ) tpl = "i am {name}, age {age}, really {name}" . format ( * * { "name" : "seven" , "age" : 18 }) tpl = "i am {0[0]}, age {0[1]}, really {0[2]}" . format ([ 1 , 2 , 3 ], [ 11 , 22 , 33 ]) tpl = "i am {:s}, age {:d}, money {:f}" . format ( "seven" , 18 , 88888.1 ) tpl = "i am {:s}, age {:d}" . format ( * [ "seven" , 18 ]) tpl = "i am {name:s}, age {age:d}" . format (name = "seven" , age = 18 ) tpl = "i am {name:s}, age {age:d}" . format ( * * { "name" : "seven" , "age" : 18 }) tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}" . format ( 15 , 15 , 15 , 15 , 15 , 15.87623 , 2 ) tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}" . format ( 15 , 15 , 15 , 15 , 15 , 15.87623 , 2 ) tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}" . format ( 15 ) tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}" . format (num = 15 ) |
更多格式化操作:https://docs.python.org/3/library/string.html
标签:
原文地址:http://www.cnblogs.com/lijinrui/p/5594840.html