Python学习笔记(二)

一、函数Python
1、内置函数
2、自定义函数
3、函数参数、函数对象
4、函数装饰器 (有趣)
5、生成器函数 (重点)
6、字符串常用函数
二、lambda表达式 (掌握,重点)
三、正则的使用
1、语法
2、分组(重点) 跳过分组,?:
3、指定界定符

一、函数Python

1、内置函数

  • help() : 获取关键字或内置函数的详细信息

 

2、自定义函数

def func_name(参数列表):
    函数体
    return/yield 函数返回值   #可选的
  • 允许嵌套函数
  • 无需声明函数返回值类型
  • 函数能够被赋值给变量

 

3、函数参数

!!!!!!Python是弱类型参数,不需要指定函数形参的类型!!!

A) 无参函数

def show_log():
    print('I am a log')
show_log()

 

B) 位置参数

#传入的参数与定义的参数一一对应
def func_name(arg1,arg2,arg3):
	print(arg1,arg2,arg3)
func_name(val1,val2,val3)

 

C)关键字参数

#即scala中的命名参数,调用函数的时候声明值的形参名
def func_name(arg1,arg2,arg3):
	print(arg1,arg2,arg3)
func_name(arg1=val1,arg3=val3,arg2=val2)

 

D)默认值参数

#即scala中的参数缺省值。定义函数时,设置参数的默认值
#可以指定通过位置或者关键字指定参数的值
#一般默认值设置在参数列表的尾部
def func_name(arg1="val1",arg2="val2",arg3="val3") :
	print(arg1,arg2,arg3)
func_name()

 

E)不定长参数

参数个数不确定,可适应更加复杂情况

  • 包裹(packing)位置参数

    • 接收不定长的位置参数,参数被组织成一个元组传入
    • 参数表示为*args
    def  func2( *t ) :	# t is a tuple
    	print(t)
    func2()		# no argument
    func2(1,2,3)		
    func2(1,2,3,4,5)
    
  • 包裹(packing)关键字参数

    • 接收不定长的关键字参数,参数被组织成一个字典传入
    • 参数表示为**args
    def  func3( **d ) :      # d is a dictionary
    	print(d)
    func3()		# no argument
    func3(a=1, b=2, c=3)
    

 

3、函数对象

  • 函数可以被引用,即函数可以赋值给一个变量
  • 函数可以当做参数传递
  • 函数可以作返回值
  • 函数可以嵌套
def  factorial(n):
    if n <= 2: return n
    return factorial(n-1)*n
#递归函数
f=factorial	
f(4)
l=[factorial, f]
l[0](4)	
l[1](4)
d={'x':factorial}
d['x'](4)
#函数作为参数对象

4、函数装饰器 (有趣)

  • 装饰器函数:一个函数参数、一个内部函数、返回一个函数对象

  • 被装饰的函数:使用 @装饰器函数 注解

  • 装饰过程:

    将要装饰的函数A作为参数传入到装饰器函数B,装饰器函数的内置函数C中调用函数A,然后装饰器函数返回内置函数对象C。因此使用@注解的函数就相当于重新生成了。

  • 一般在装饰器函数中完成一些前置动作或扫尾动作。

def my_decorator(some_func):
    def wrapper(*args):
        print("I am watching you!")
        some_func(*args)
        print("You are called.")
    return wrapper

@my_decorator   #@my_decorator = my_decorator(add)
def add(x, y):
    print(x,'+',y,'=',x+y)

add(5,6)

5、生成器函数 (重点)

  • 可以使用多个yield语句或多次调用yield语句来返回值

  • 迭代器调用.__next__方法,每次调用获得一次yield语句的返回值

  • 函数返回一个迭代器

    • 能够使用for循环遍历
    • 生成器每次只被读取一次
    • 生成器有内置方法next(),调用后返回下一个元素。
    • yield不会终止程序,返回值之后程序继续运行

    生成器函数返回的迭代对象,只有在调用的时候才会进行下一次代码块的执行

    def  list_pythagorean_triples(n) :
        for c in range(n):
            for a in range(1, c):
                for b in range(1, c):
                    if a*a+b*b==c*c:
                        yield (a,b,c)
    
  • 使用方法

    1、for循环迭代生成器 2、next()函数从生成器中取值 3、通过列表构造函数,产生生成器的结果列表

 

  • 生成器生成式和列表生成式
(x**3  for  x  in  range(10))
# 小括号()  生成器
# 中括号 []  列表
# 花括号 {}  字典

6、字符串常用函数

字符串操作说明举例
string[n:m] 字符串切片 string='Hello World\n' string[0] string[:-1] string[3:5]
int() 字符串转数值类型 int("123") float("123")
str() 数值类型转字符串 str(123)str(123.456)
ord() 字符转Unicode码 ord('A')
chr() Unicode码转字符 chr(65)
lower() 转成小写字符串 "WELCOME".lower()
upper() 转成大写字符串 "welcome".upper()
split()分割字符串N'] words = "WELCOME TO PYTHON".split(' TO ') print(words)# ['WELCOME', 'PYTHON']
join() 将序列中的元素以指定的字符连接生成一个新的字符串 s = '++' list=['1', '2', '3'] s.join(list) #result is '1++2++3' s.join('why') #result is 'w++h++y'
strip()lstrip()rstrip() 用于移除字符串头尾/头/尾指定的字符(默认为空格或换行符)或字符序列 s = ' "hi\\ \n\tPython" ' s.strip()# result is '"hi\ \n\tPython"' s.strip(' "no') # result is 'hi\ \n\tPyth'
in判断是否为子串'or' in 'toronto or orlando' # True
find() 返回子串开始的索引值,找不到子串时返回-1 s = 'toronto or orlando's.find('or') # return index 1 s.find('or', 2, 8) # return -1, meaning not found
index() 返回子串开始的索引值,找不到子串时抛出异常 s = 'toronto or orlando's.index('or') # return index 1 s.index('or', 2, 8) # throw ValueError: substring not found
count() 统计字符串里某个字符出现的次数 s = 'toronto or orlando' s.count('or') # return 3 s.count('or', 2) # return 2 s.count('or', 2, 9) # return 0
replace() 方法把字符串中的 旧字符串替换成新字符串 s = 'toronto or orlando's.replace('or', '/x\')# result: t/x\onto /x\ /x\lando s.replace('or', '/x\', 2)# result: t/x\onto /x\ orlando
startswith()检查字符串是否是以指定子字符串开头s = 'toronto or orlando' s.startswith('or') # return False s.startswith('or', 1) # return True s.startswith(('or', 'tor')) # return True
endswith() 检查字符串是否是以指定子字符串结尾 s = 'toronto or orlando' s.endswith('and') # return False s.endswith('and', 1, -1) # return True s.endswith(('and', 'do')) # return True
maketrans() translate() 字符串转换,maketrans() 设置转换模式,一一替换,translate()执行转换操作。可以一次定义多个模式 s = 'toronto or orlando' # define a translation table: table=s.maketrans('on', '.N') # o change to . # n change to N # translate using above table s.translate(table) # result: 't.r.Nt. .r .rlaNd.'

二、lambda表达式 (掌握,重点)

  • 匿名函数,同scala,但是需要使用关键字lambda

  • 可以用*x 代表多个重复类型的参数

    demo实例

    # return results of +, -, *, //, % in a list
    lambda x,y: [x+y, x-y, x*y, x//y, x%y]
    
    # return max, min, sum in a tuple
    lambda *n: (max(n), min(n), sum(n))
    
    # sum 1 to n
    lambda n: sum(range(1, n+1))
    
    
  • 常见函数使用场景同scala

    • filter函数 filter(func,param)

      • items=[0, 1, 2, 3, 4, 5]
        list(filter(lambda x: x%2==0, items))
        list(filter(None, items))
        
        
    • map函数 map(func,*param)

      • i1=[1, 2, 3, 4, 5, 6]
        i2=[11, 12, 13, 14]
        i3=[111, 112]
        list(map(lambda x,y,z: x+y+z, i1, i2, i3))
        #map次数以最短的那个迭代对象为准
        
        
    • max函数 max(param,key=func)

      • key 是关键字,必须的
    • min函数 min(param,key=func)

    • sort函数 sort(param,key=func)

      • sort (param,reverse=True) 排序反转
      • sorted([1,2,3,4], key=lambda x:-x) 对复杂对象自定义排序规则

三、正则的使用

  • 不能直接使用,需要导入re模块 :import re

1、语法

表达式说明
compile() 用于编译正则表达式,生成一个正则表达式( Pattern )对象
match() 查看字符串的开头是否符合匹配模式,如果匹配失败,返回none(即完整匹配)
search() 扫描整个字符串并返回第一个成功的匹配
findall() 在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表
sub() 替换字符串中的匹配项, sub('a?','+_+','bb') ,单字符?使用时会出现一个问题,字母间的边界也会被匹配到。

 

符号说明示例
  逐字匹配 re.match('abc', 'abc123')# match! re.match('abc', '123abc')# not match!
| re.match('abc|xyz', 'xyz123') # match!
. 除换行符外匹配任意字符 re.match('.', '123abc') # match!
[...] 匹配任意一组字符中的一个 re.match('[aAbc]', 'b') # a, A, b, or c re.match('[a-c]', 'b') # a to c re.match('[A-C0-9]', 'B') # A to C and 0 to 9
[^...] 匹配不在[]中的字符 re.match('[^A-Z0-9]', 'b') # not A to Z nor 0 to 9
\d 匹配任意数字 re.match(r'\d', '8') # match!
\D 匹配任意非数字 re.match(r'\D', 'B') # match!

 

符号说明示例
\w 匹配字母数字及下划线 re.match(r'\w', '_') # match!
\W 匹配非字母数字及下划线 re.match(r'\W', '?') # match!
\s 匹配任意空白字符 re.match(r'\s', ' ') # match!
\S 匹配任意非空字符 re.match(r'\S', '#') # match!
^ 匹配字符串的开头 re.findall('^[abc]', 'xabc') # not found
$ 匹配字符串的末尾 re.findall('[c]$', 'cc cc') # found one
\b 匹配一个单词边界 re.findall(r'\bc', 'cc cc') # cc cc
\B 匹配非单词边界 re.findall(r'c\B', 'cc cc') # cc cc
re* 匹配0个或多个的表达式 re.match('[abc]*', 'xabc') # match
re+ 匹配1个或多个的表达式 re.match('[abc]+', 'abcd') # match
re? 匹配0个或1个表达式 re.match('[abc]?', 'xabc') # match
re{n}匹配n个前面的表达式re.match('[abc]{2}', 'abc') # match
re{n, m} 匹配n到m次前面的表达式 re.match('[abc]{1,3}', 'abc') # match

 

2、分组

  • 组匹配
match=re.match(r'(\d+)', '123') 
groups=match.groups() 	#  groups is ('123',)
g1=match.group(1) 		#  g1 is '123'

#groups返回所有子组的元组集合
match=re.match(r'(\d)(\d)(\d)', '123') 
groups=match.groups() 	#  groups is ('1', '2', '3')

#group(x) 返回第x组的匹配项
g1=match.group(1) 		#  g1 is '1'
g2=match.group(2) 		#  g2 is '2'
g3=match.group(3) 		#  g3 is '3'

  • 模式中引用自身的分组: \1\2, \1代表本模式中的分组1内容的重复。
#引用本模式中的分组
ma = re.match(r'(\d(\d(\d)))\1\2\3', '123123233')
print(ma.groups())

(重点) 跳过分组?:
  • 消耗匹配,但不进入组序列
#?:所在的分组会被匹配,但不会被纳入分组序列。
ma = re.match(r'(\d(?:\d(\d)))-\2',  '647-7')
print(ma.groups())

 

3、指定界定符

  • 通过指定界定符
符号说明示例解释
?= 正向先行断言/前向肯定界定符 #matchre.match(r'\w+(?=.com)', 'google.com') #not matchre.match(r'\w+(?=.com)', 'google.net') y(?=x),y后面必须能匹配到x,但是不消耗x
?! 反向先行断言/前向否定界定符 #matchre.match(r'\w+(?!.com)', 'google.com') #not matchre.match(r'\b\w+\b(?!.com)', 'google.com') y(?!x),y后面不能匹配到x,不消耗x
?<= 向后肯定界定符 #found: 1234 re.findall(r'(?<=800)\d+', '8001234') (?<=x)y,y前面必须能匹配到x,但是不消耗x
?<! 向后否定界定符 #found: 8001234 re.findall(r'(?<!800)\d+', '8001234') #not found re.findall(r'(?<!800)\d{4}$', '8001234') (?<!x)y,y前面不能匹配到x,不消耗x

 

<div>[TOC]</div><p>&nbsp;</p><h3>一、函数Python</h3><h4>1、内置函数</h4><p><img src='file://C:/Users/HuYang/AppData/Roaming/Typora/typora-user-images/1565228506657.png' alt='1565228506657' referrerPolicy='no-referrer' /></p><ul><li><strong>help()</strong>  : 获取关键字或内置函数的详细信息</li>
</ul><p>&nbsp;</p><h4>2、自定义函数</h4><pre><code class='language-python' lang='python'>def func_name(参数列表):    函数体    return/yield 函数返回值   #可选的</code></pre><ul><li><strong>允许嵌套函数</strong></li><li><strong>无需声明函数返回值类型</strong></li><li><strong>函数能够被赋值给变量</strong></li>
</ul><p>&nbsp;</p><h4>3、函数参数</h4><p><strong>!!!!!!Python是弱类型参数,不需要指定函数形参的类型!!!</strong></p><p><strong>A) 无参函数</strong></p><pre><code class='language-python' lang='python'>def show_log():    print(&#39;I am a log&#39;)show_log()</code></pre><p>&nbsp;</p><p><strong>B) 位置参数</strong></p><pre><code class='language-python' lang='python'>#传入的参数与定义的参数一一对应def func_name(arg1,arg2,arg3):print(arg1,arg2,arg3)func_name(val1,val2,val3)</code></pre><p>&nbsp;</p><p><strong>C)关键字参数</strong></p><pre><code class='language-python' lang='python'>#即scala中的命名参数,调用函数的时候声明值的形参名def func_name(arg1,arg2,arg3):print(arg1,arg2,arg3)func_name(arg1=val1,arg3=val3,arg2=val2)</code></pre><p>&nbsp;</p><p><strong>D)默认值参数</strong></p><pre><code class='language-python' lang='python'>#即scala中的参数缺省值。定义函数时,设置参数的默认值#可以指定通过位置或者关键字指定参数的值#一般默认值设置在参数列表的尾部def func_name(arg1=&quot;val1&quot;,arg2=&quot;val2&quot;,arg3=&quot;val3&quot;) :print(arg1,arg2,arg3)func_name()</code></pre><p>&nbsp;</p><p><strong>E)不定长参数</strong></p><p>参数个数不确定,可适应更加复杂情况</p><ul><li><p>包裹(packing)<strong>位置</strong>参数</p><ul><li>接收不定长的位置参数,参数被组织成一个<strong>元组传入</strong></li><li>参数表示为*args</li>
</ul><pre><code class='language-python' lang='python'>def  func2( *t ) :# t is a tupleprint(t)func2()# no argumentfunc2(1,2,3)func2(1,2,3,4,5)</code></pre></li><li><p>包裹(packing)<strong>关键字</strong>参数</p><ul><li>接收不定长的关键字参数,参数被组织成一个<strong>字典传入</strong></li><li>参数表示为**args</li>
</ul><pre><code class='language-python' lang='python'>def  func3( **d ) :      # d is a dictionaryprint(d)func3()# no argumentfunc3(a=1, b=2, c=3)</code></pre></li>
</ul><p>&nbsp;</p><h4>3、函数对象</h4><ul><li>函数可以被引用,即<strong>函数可以赋值给一个变量</strong></li><li><strong>函数可以当做参数传递</strong></li><li><strong>函数可以作返回值</strong></li><li><strong>函数可以嵌套</strong></li>
</ul><pre><code class='language-python' lang='python'>def  factorial(n):    if n &lt;= 2: return n    return factorial(n-1)*n#递归函数</code></pre><pre><code class='language-python' lang='python'>f=factorialf(4)l=[factorial, f]l[0](4)l[1](4)d={&#39;x&#39;:factorial}d[&#39;x&#39;](4)#函数作为参数对象</code></pre><h4>4、函数装饰器  <strong>(有趣)</strong></h4><ul><li><p>装饰器函数:一个函数参数、一个内部函数、返回一个函数对象</p></li><li><p>被装饰的函数:使用 @装饰器函数 注解</p></li><li><p>装饰过程:</p><p>将要装饰的函数A作为参数传入到装饰器函数B,装饰器函数的内置函数C中调用函数A,然后装饰器函数返回内置函数对象C。因此使用@注解的函数就相当于重新生成了。</p></li><li><p>一般在装饰器函数中完成一些前置动作或扫尾动作。</p></li>
</ul><pre><code class='language-python' lang='python'>def my_decorator(some_func):    def wrapper(*args):        print(&quot;I am watching you!&quot;)        some_func(*args)        print(&quot;You are called.&quot;)    return wrapper
@my_decorator   #@my_decorator = my_decorator(add)def add(x, y):    print(x,&#39;+&#39;,y,&#39;=&#39;,x+y)
add(5,6)</code></pre><h4>5、生成器函数 (重点)</h4><ul><li><p>可以使用多个yield语句或多次调用yield语句来返回值</p></li><li><p>迭代器调用.__next__方法,每次调用获得一次yield语句的返回值</p></li><li><p>函数返回一个迭代器</p><ul><li>能够使用for循环遍历</li><li>生成器每次只被读取一次</li><li>生成器有内置方法<strong>next()</strong>,调用后返回下一个元素。</li><li>yield不会终止程序,返回值之后程序继续运行</li>
</ul><p><strong>生成器函数返回的迭代对象,只有在调用的时候才会进行下一次代码块的执行</strong></p><pre><code class='language-python' lang='python'>def  list_pythagorean_triples(n) :    for c in range(n):        for a in range(1, c):            for b in range(1, c):                if a*a+b*b==c*c:                    yield (a,b,c)</code></pre></li><li><p><strong>使用方法</strong></p><p>1、for循环迭代生成器2、next()函数从生成器中取值3、通过列表构造函数,产生生成器的结果列表</p></li>
</ul><p>&nbsp;</p><ul><li><strong>生成器生成式和列表生成式</strong></li>
</ul><pre><code class='language-python' lang='python'>(x**3  for  x  in  range(10))# 小括号()  生成器# 中括号 []  列表# 花括号 {}  字典</code></pre><h4>6、字符串常用函数</h4><figure><table><thead><tr><th><strong>字符串操作</strong></th><th><strong>说明</strong></th><th><strong>举例</strong></th></tr></thead><tbody><tr><td>string[n:m]</td><td>字符串切片</td><td>string=&#39;Hello World\n&#39; string[0] string[:-1] string[3:5]</td></tr><tr><td>int()</td><td>字符串转数值类型</td><td>int(&quot;123&quot;) float(&quot;123&quot;)</td></tr><tr><td>str()</td><td>数值类型转字符串</td><td>str(123)str(123.456)</td></tr><tr><td>ord()</td><td>字符转Unicode码</td><td>ord(&#39;A&#39;)</td></tr><tr><td>chr()</td><td>Unicode码转字符</td><td>chr(65)</td></tr><tr><td>lower()</td><td>转成小写字符串</td><td>&quot;WELCOME&quot;.lower()</td></tr><tr><td>upper()</td><td>转成大写字符串</td><td>&quot;welcome&quot;.upper()</td></tr></tbody></table></figure><figure><table><thead><tr><th>split()</th><th>分割字符串</th><th>N&#39;] words = &quot;WELCOME TO PYTHON&quot;.split(&#39; TO &#39;) print(words)# [&#39;WELCOME&#39;, &#39;PYTHON&#39;]</th></tr></thead><tbody><tr><td>join()</td><td>将序列中的元素以指定的字符连接生成一个新的字符串</td><td><code>s = &#39;++&#39; list=[&#39;1&#39;, &#39;2&#39;, &#39;3&#39;] s.join(list)</code> #result is  &#39;1++2++3&#39;                     <code>s.join(&#39;why&#39;)</code> #result is  &#39;w++h++y&#39;</td></tr><tr><td>strip()lstrip()rstrip()</td><td>用于移除字符串头尾/头/尾指定的字符(默认为空格或换行符)或字符序列</td><td><code>s = &#39; &quot;hi\\   \n\tPython&quot;    &#39; s.strip()</code># result is &#39;&quot;hi\   \n\tPython&quot;&#39; s.strip(&#39; &quot;no&#39;) # result is &#39;hi\   \n\tPyth&#39;</td></tr></tbody></table></figure><figure><table><thead><tr><th>in</th><th>判断是否为子串</th><th>&#39;or&#39; in &#39;toronto or orlando&#39; # True</th></tr></thead><tbody><tr><td>find()</td><td>返回子串开始的索引值,找不到子串时返回-1</td><td>s = &#39;toronto or orlando&#39;s.find(&#39;or&#39;)    # return index 1 s.find(&#39;or&#39;, 2, 8)  # return -1, meaning not found</td></tr><tr><td>index()</td><td>返回子串开始的索引值,找不到子串时抛出异常</td><td>s = &#39;toronto or orlando&#39;s.index(&#39;or&#39;)   # return index 1 s.index(&#39;or&#39;, 2, 8) # throw ValueError: substring not found</td></tr><tr><td>count()</td><td>统计字符串里某个字符出现的次数</td><td>s = &#39;toronto or orlando&#39; s.count(&#39;or&#39;)   # return 3 s.count(&#39;or&#39;, 2)    # return 2 s.count(&#39;or&#39;, 2, 9) # return 0</td></tr><tr><td>replace()</td><td>方法把字符串中的 旧字符串替换成新字符串</td><td>s = &#39;toronto or orlando&#39;s.replace(&#39;or&#39;, &#39;/x\&#39;)# result:    t/x\onto /x\ /x\lando s.replace(&#39;or&#39;, &#39;/x\&#39;, 2)# result:   t/x\onto /x\ orlando</td></tr></tbody></table></figure><figure><table><thead><tr><th>startswith()</th><th>检查字符串是否是以指定子字符串开头</th><th>s = &#39;toronto or orlando&#39; s.startswith(&#39;or&#39;)  # return False s.startswith(&#39;or&#39;, 1)   # return True s.startswith((&#39;or&#39;, &#39;tor&#39;)) # return True</th></tr></thead><tbody><tr><td>endswith()</td><td>检查字符串是否是以指定子字符串结尾</td><td>s = &#39;toronto or orlando&#39; s.endswith(&#39;and&#39;)   # return False s.endswith(&#39;and&#39;, 1, -1)    # return True s.endswith((&#39;and&#39;, &#39;do&#39;))   # return True</td></tr><tr><td><strong>maketrans() translate()</strong></td><td><strong>字符串转换,</strong>maketrans() 设置转换模式,<strong>一一替换</strong>,translate()执行转换操作。<strong>可以一次定义多个模式</strong></td><td><code>s = &#39;toronto or orlando&#39;</code> # define a translation table: <code>table=s.maketrans(&#39;on&#39;, &#39;.N&#39;)</code>   # o change to .                                                       # n change to N # translate using above table <code>s.translate(table)</code>  # result:   &#39;t.r.Nt. .r .rlaNd.&#39;</td></tr></tbody></table></figure><h3>二、lambda表达式  (掌握,重点)</h3><ul><li><p>匿名函数,同scala,但是需要使用<strong>关键字lambda</strong></p></li><li><p>可以用*x 代表多个重复类型的参数</p><p>demo实例</p><pre><code class='language-python' lang='python'># return results of +, -, *, //, % in a listlambda x,y: [x+y, x-y, x*y, x//y, x%y]
# return max, min, sum in a tuplelambda *n: (max(n), min(n), sum(n))
# sum 1 to nlambda n: sum(range(1, n+1))
</code></pre></li><li><p><strong>常见函数使用场景同scala</strong></p><ul><li><p>filter函数  filter(func,param)</p><ul><li><pre><code class='language-python' lang='python'>items=[0, 1, 2, 3, 4, 5]list(filter(lambda x: x%2==0, items))list(filter(None, items))
</code></pre></li>
</ul></li><li><p>map函数 map(func,*param)</p><ul><li><pre><code class='language-python' lang='python'>i1=[1, 2, 3, 4, 5, 6]i2=[11, 12, 13, 14]i3=[111, 112]list(map(lambda x,y,z: x+y+z, i1, i2, i3))#map次数以最短的那个迭代对象为准
</code></pre></li>
</ul></li><li><p>max函数 max(param,key=func)</p><ul><li>key 是关键字,必须的</li>
</ul></li><li><p>min函数 min(param,key=func)</p></li><li><p>sort函数 sort(param,key=func)</p><ul><li>sort (param,reverse=True) 排序反转</li><li>sorted([1,2,3,4], key=lambda x:-x) 对复杂对象自定义排序规则</li>
</ul></li>
</ul></li>
</ul><h3>三、正则的使用</h3><ul><li>不能直接使用,需要导入re模块 :<code>import re</code></li>
</ul><h4>1、语法</h4><figure><table><thead><tr><th>表达式</th><th>说明</th></tr></thead><tbody><tr><td>compile()</td><td>用于编译正则表达式,生成一个正则表达式( Pattern )对象</td></tr><tr><td>match()</td><td>查看字符串的<strong>开头</strong>是否符合匹配模式,如果匹配失败,返回none(即完整匹配)</td></tr><tr><td>search()</td><td>扫描整个字符串并<strong>返回第一个成功的匹配</strong></td></tr><tr><td>findall()</td><td>在字符串中找到正则表达式所匹配的<strong>所有子串</strong>,并<strong>返回一个列表</strong>,如果没有找到匹配的,则返回空列表</td></tr><tr><td>sub()</td><td>替换字符串中的匹配项,        sub(&#39;a?&#39;,&#39;+_+&#39;,&#39;bb&#39;) ,<strong>单字符?使用时会出现一个问题,字母间的边界也会被匹配到。</strong></td></tr></tbody></table></figure><p>&nbsp;</p><figure><table><thead><tr><th>符号</th><th>说明</th><th>示例</th></tr></thead><tbody><tr><td>&nbsp;</td><td>逐字匹配</td><td>re.match(&#39;abc&#39;, &#39;abc123&#39;)#  match! re.match(&#39;abc&#39;, &#39;123abc&#39;)#  not match!</td></tr><tr><td>|</td><td>或</td><td>re.match(&#39;abc|xyz&#39;, &#39;xyz123&#39;)   #  match!</td></tr><tr><td>.</td><td><strong>除换行符外</strong>匹配任意字符</td><td>re.match(&#39;.&#39;, &#39;123abc&#39;) #  match!</td></tr><tr><td>[...]</td><td>匹配任意一组字符中的一个</td><td>re.match(&#39;[aAbc]&#39;, &#39;b&#39;) #  a, A, b, or c re.match(&#39;[a-c]&#39;, &#39;b&#39;)  #  a to c re.match(&#39;[A-C0-9]&#39;, &#39;B&#39;)   #  A to C and 0 to 9</td></tr><tr><td>[^...]</td><td>匹配不在[]中的字符</td><td>re.match(&#39;[^A-Z0-9]&#39;, &#39;b&#39;) #  not A to Z nor 0 to 9</td></tr><tr><td>\d</td><td>匹配任意数字</td><td>re.match(r&#39;\d&#39;, &#39;8&#39;)    #  match!</td></tr><tr><td>\D</td><td>匹配任意非数字</td><td>re.match(r&#39;\D&#39;, &#39;B&#39;)    #  match!</td></tr></tbody></table></figure><p>&nbsp;</p><figure><table><thead><tr><th>符号</th><th>说明</th><th>示例</th></tr></thead><tbody><tr><td>\w</td><td>匹配字母数字<strong>及下划线</strong></td><td>re.match(r&#39;\w&#39;, &#39;_&#39;)     #  match!</td></tr><tr><td>\W</td><td>匹配非字母数字<strong>及下划线</strong></td><td>re.match(r&#39;\W&#39;, &#39;?&#39;)    #  match!</td></tr><tr><td>\s</td><td>匹配任意空白字符</td><td>re.match(r&#39;\s&#39;, &#39; &#39;)    #  match!</td></tr><tr><td>\S</td><td>匹配任意非空字符</td><td>re.match(r&#39;\S&#39;, &#39;#&#39;)    #  match!</td></tr><tr><td>^</td><td>匹配字符串的开头</td><td>re.findall(&#39;^[abc]&#39;, &#39;xabc&#39;)    #  not found</td></tr><tr><td>$</td><td>匹配字符串的末尾</td><td>re.findall(&#39;[c]$&#39;, &#39;cc cc&#39;) #  found one</td></tr><tr><td><strong>\b</strong></td><td><strong>匹配一个单词边界</strong></td><td>re.findall(r&#39;\bc&#39;, &#39;cc cc&#39;) # c<strong>c</strong> c<strong>c</strong></td></tr><tr><td><strong>\B</strong></td><td><strong>匹配非单词边界</strong></td><td>re.findall(r&#39;c\B&#39;, &#39;cc cc&#39;) # <strong>c</strong>c <strong>c</strong>c</td></tr><tr><td>re*</td><td>匹配0个或多个的表达式</td><td>re.match(&#39;[abc]*&#39;, &#39;xabc&#39;)  #  match</td></tr><tr><td>re+</td><td>匹配1个或多个的表达式</td><td>re.match(&#39;[abc]+&#39;, &#39;abcd&#39;)  #  match</td></tr><tr><td>re?</td><td>匹配0个或1个表达式</td><td>re.match(&#39;[abc]?&#39;, &#39;xabc&#39;)  #  match</td></tr></tbody></table></figure><figure><table><thead><tr><th>re{n}</th><th>匹配n个前面的表达式</th><th>re.match(&#39;[abc]{2}&#39;, &#39;abc&#39;) #  match</th></tr></thead><tbody><tr><td>re{n, m}</td><td>匹配n到m次前面的表达式</td><td>re.match(&#39;[abc]{1,3}&#39;, &#39;abc&#39;)   #  match</td></tr></tbody></table></figure><p>&nbsp;</p><h4>2、分组</h4><ul><li>组匹配</li>
</ul><pre><code class='language-python' lang='python'>match=re.match(r&#39;(\d+)&#39;, &#39;123&#39;) groups=match.groups() #  groups is (&#39;123&#39;,)g1=match.group(1) #  g1 is &#39;123&#39;
#groups返回所有子组的元组集合match=re.match(r&#39;(\d)(\d)(\d)&#39;, &#39;123&#39;) groups=match.groups() #  groups is (&#39;1&#39;, &#39;2&#39;, &#39;3&#39;)
#group(x) 返回第x组的匹配项g1=match.group(1) #  g1 is &#39;1&#39;g2=match.group(2) #  g2 is &#39;2&#39;g3=match.group(3) #  g3 is &#39;3&#39;
</code></pre><ul><li>模式中<strong>引用自身的分组</strong>: <code>\1\2</code>, \1代表本模式中的分组1内容的重复。</li>
</ul><pre><code class='language-python' lang='python'>#引用本模式中的分组ma = re.match(r&#39;(\d(\d(\d)))\1\2\3&#39;, &#39;123123233&#39;)print(ma.groups())
</code></pre><h5>(重点) <strong>跳过分组</strong>,<code>?:</code> </h5><ul><li>消耗匹配,但不进入组序列</li>
</ul><pre><code class='language-python' lang='python'>#?:所在的分组会被匹配,但不会被纳入分组序列。ma = re.match(r&#39;(\d(?:\d(\d)))-\2&#39;,  &#39;647-7&#39;)print(ma.groups())
</code></pre><p>&nbsp;</p><h4>3、指定界定符</h4><ul><li>通过指定界定符</li>
</ul><figure><table><thead><tr><th>符号</th><th>说明</th><th>示例</th><th>解释</th></tr></thead><tbody><tr><td>?=</td><td>正向先行<strong>断言</strong>/前向肯定界定符</td><td>#matchre.match(r&#39;\w+(?=.com)&#39;, &#39;google.com&#39;) #not matchre.match(r&#39;\w+(?=.com)&#39;, &#39;google.net&#39;)</td><td>y(?=x),y后面必须能匹配到x,但是不消耗x</td></tr><tr><td>?!</td><td>反向先行<strong>断言</strong>/前向否定界定符</td><td>#matchre.match(r&#39;\w+(?!.com)&#39;, &#39;google.com&#39;) #not matchre.match(r&#39;\b\w+\b(?!.com)&#39;, &#39;google.com&#39;)</td><td>y(?!x),y后面不能匹配到x,不消耗x</td></tr><tr><td>?&lt;=</td><td>向后肯定界定符</td><td>#found:  1234 re.findall(r&#39;(?&lt;=800)\d+&#39;,  &#39;8001234&#39;)</td><td>(?&lt;=x)y,y前面必须能匹配到x,但是不消耗x</td></tr><tr><td>?&lt;!</td><td>向后否定界定符</td><td>#found:  8001234 re.findall(r&#39;(?&lt;!800)\d+&#39;,  &#39;8001234&#39;) #not found re.findall(r&#39;(?&lt;!800)\d{4}$&#39;,  &#39;8001234&#39;)</td><td>(?&lt;!x)y,y前面不能匹配到x,不消耗x</td></tr></tbody></table></figure><p>&nbsp;</p>

 

posted @ 2019-08-28 15:07  WhoYoung  阅读(521)  评论(0编辑  收藏  举报