Python学习笔记:Python高级特性
学自廖雪峰巨佬的Python3教程:
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014317568446245b3e1c8837414168bcd2d485e553779e000
1.Mark下大佬的一句话:在Python中,代码不是越多越好,而是越少越好。代码不是越复杂越好,而是越简单越好。1行代码能实现的功能,决不写5行代码。请始终牢记,代码越少,开发效率越高。笔者概括起来就是:Less is More
2.切片:即取一个list或tuple的部分元素
笨方法:用循环取
机智的方法:使用Python提供的切片操作符
# 一个list L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack'] # 从索引0开始取,直到索引3为止,但不包括索引3,如果第一个索引是0,还可以忽略 >>> L[:3] ['Michael', 'Sarah', 'Tracy'] # 从索引1开始取到3 >>> L[1:3] ['Sarah', 'Tracy'] #从后开始取两个 >>> L[-2:] ['Bob', 'Jack'] # 取倒数第二个 >>> L[-2:-1] ['Bob'] # 另一个list >>> L = list(range(100)) >>> L [0, 1, 2, 3, ..., 99] # 前10个数,每两个取一个 >>> L[:10:2] [0, 2, 4, 6, 8] # 所有数,每5个取一个 >>> L[::5] [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95] # 原样复制一个list >>> L[:] [0, 1, 2, 3, ..., 99] # 对tuple切片 >>> (0, 1, 2, 3, 4, 5)[:3] (0, 1, 2) # 对字符串切片 >>> 'ABCDEFG'[:3] 'ABC' >>> 'ABCDEFG'[::2] 'ACEG'
可以看到切片操作符简直无所不能切,只是tuple切出来还是tuple,字符串切完还是字符串
非递归版本:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def trim(s):
# 注意:用切片的时候,tail代表的那一位是不取的
head = 0
tail = len(s)
# 如果字符串长度为0,则直接返回空
if tail == 0:
return ''
# 第一个循环里的head<tail用于从前往后遍历,如果出现有全为空格的,则head最终会等于tail,第二个循环就不会运行
while head < tail and s[head] == ' ':
head += 1
while head < tail and s[tail - 1] == ' ':
tail -= 1
return s[head: tail]
# 测试:
if __name__ == "__main__":
if trim('hello ') != 'hello':
print('测试失败!')
elif trim(' hello ') != 'hello':
print('测试失败!')
elif trim(' hello world ') != 'hello world':
print('测试失败!')
elif trim('') != '':
print('测试失败!')
elif trim(' ') != '':
print('测试失败!')
else:
print('测试成功!')
输出结果:
递归版本:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def trim(s):
# 如果字符串长度为0,或者每轮递归完,检查开头结尾是否为空格
if len(s) == 0 or s[0] != ' ' and s[-1] != ' ':
return s
# 如果第一个字符是空格,则从第二个字符开始取后面全部,最终递归完成时,前面的空格会全部清除完毕
if s[0] == ' ':
return trim(s[1:])
# 如果倒数第一个是空格,则从倒数第二个字符开始往前取全部,最终递归完成时,后面的空格会全部清除完毕
if s[-1] == ' ':
return trim(s[:-1])
# 测试:
if __name__ == "__main__":
if trim('hello ') != 'hello':
print('测试失败!')
elif trim(' hello ') != 'hello':
print('测试失败!')
elif trim(' hello world ') != 'hello world':
print('测试失败!')
elif trim('') != '':
print('测试失败!')
elif trim(' ') != '':
print('测试失败!')
else:
print('测试成功!')
输出结果:
3.迭代:即遍历一个list或tuple,在Python中,是通过for..in来完成的,字符串也是可迭代对象,每次迭代的就是字符串里的字符。
如何判断一个对象是可迭代对象呢?方法是通过collections的Iterable类型判断:
>>> from collections import Iterable
>>> isinstance('abc', Iterable) # str是否可迭代
True
>>> isinstance([1,2,3], Iterable) # list是否可迭代
True
>>> isinstance(123, Iterable) # 整数是否可迭代
False
如果要对list实现类似于Java的下标循环,可以通过Python内置的enumerate函数把一个list里的元素变成索引-元素对,这样就可以在for循环里同时迭代索引和元素本身
>>> for i, value in enumerate(['A', 'B', 'C']):
... print(i, value)
...
0 A
1 B
2 C
使用dict的items()可以同时迭代key和value
>>> d = {'x': 'A', 'y': 'B', 'z': 'C' }
>>> for k, v in d.items():
... print(k, '=', v)
...
y = B
x = A
z = C
代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def findMinAndMax(L):
# 取正负无穷做边界值
Min = float('inf')
Max = float('-inf')
if len(L) == 0:
return (None, None)
# 迭代list获取最大值最小值
for i in L:
if i < Min:
Min = i
if i > Max:
Max = i
return (Min, Max)
if __name__ == '__main__':
# 测试
if findMinAndMax([]) != (None, None):
print('测试失败!')
elif findMinAndMax([7]) != (7, 7):
print('测试失败!')
elif findMinAndMax([7, 1]) != (1, 7):
print('测试失败!')
elif findMinAndMax([7, 1, 3, 9, 5]) != (1, 9):
print('测试失败!')
else:
print('测试成功!')
4.列表生成式,即List Comprehensions,是Python内置的创建list的生成式。
# 生成1-10的list
>>> list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# 按照表达式x*x生成list
>>> [x * x for x in range(1, 11)]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# 使用if判断对生成的数进行筛选
>>> [x * x for x in range(1, 11) if x % 2 == 0]
[4, 16, 36, 64, 100]
# 还可以使用两层循环,生成全排列
>>> [m + n for m in 'ABC' for n in 'XYZ']
['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ']
# 列出当前目录下所有文件和目录名
>>> import os # 导入os模块
>>> [d for d in os.listdir('.')] # os.listdir可以列出文件和目录
['.emacs.d', '.ssh', '.Trash', 'Adlm', 'Applications', 'Desktop', 'Documents', 'Downloads', 'Library', 'Movies', 'Music', 'Pictures', 'Public', 'VirtualBox VMs', 'Workspace', 'XCode']
# 使用两个变量来生成list
>>> d = {'x': 'A', 'y': 'B', 'z': 'C' }
>>> [k + '=' + v for k, v in d.items()]
['y=B', 'x=A', 'z=C']
# 把一个list中所有的字符串变成小写
>>> L = ['Hello', 'World', 'IBM', 'Apple']
>>> [s.lower() for s in L]
['hello', 'world', 'ibm', 'apple']
代码:
L2=[s.lower() for s in L1 if isinstance(s,str)]
5.生成器:用于按照某种算法在循环中不断推算出后续的元素,这样就不必使用列表生成式创建完整的list从而节省大量的空间,这种一边循环一边计算的机制,称为生成器generator。
创建generator有两种方式:
第一种方法很简单,只要把一个列表生成式的[]改成(),就创建了一个generator
>>> L = [x * x for x in range(10)]
>>> L
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> g = (x * x for x in range(10))
>>> g
<generator object <genexpr> at 0x1022ef630>
打印generator的每一个元素需要使用next(g),用于获得generator的下一个返回值,没有更多的元素时,抛出StopIteration的错误。但正确的方法是使用for循环打印,因为generator也是可迭代对象。
如果推算的算法比较复杂,用for循环无法实现的时候,就可以用函数来实现
函数实现斐波那契数列
def fib(max):
n, a, b = 0, 0, 1
while n < max:
print(b)
a, b = b, a + b
n = n + 1
return 'done'
如果要将fib函数变成generator,只需要将print(b)改为yield b就可以了
这就是创建generator的另一种方法,如果一个函数定义中包含yield关键字,那么这个函数就不再是一个普通函数,而是一个generator,但是跟函数的执行流程不一样,函数是顺序执行,遇到return语句或者最后一行函数语句就返回,而变成generator的函数,在每次调用next()的时候执行,遇到yield语句就返回,再次执行时从上次返回的yield语句处继续执行,下面的例子很好的说明了这一点
def odd():
print('step 1')
yield 1
print('step 2')
yield(3)
print('step 3')
yield(5)
>>> o = odd()
>>> next(o)
step 1
1
>>> next(o)
step 2
3
>>> next(o)
step 3
5
>>> next(o)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
大佬的代码如下:
def triangles():
N = [1]
while len(N) != 11:
yield N
s = N
s.append(0)
N = [s[i - 1] + s[i] for i in range(len(s))]
算法分析:
每一行的两端都为1,中间的值为上一行的值从头两两相加所得,上一行元素为n个的话,那么中间的值有n-1个,因此每一行比上一行多一个数(n-2+1),因此中间的值很好求,即遍历上一行,使其元素前后两两相加,那么递推公式是:
[s[i]+s[i+1] for i in range(len(s)-1)]
两头再加一个1,杨辉三角就出来了对不对,所以笔者的代码是这样的
def triangles():
N = [1]
while len(N) != 11:
yield N
s = N
N = [s[i] + s[i + 1] for i in range(len(s) - 1)]
N.append(1)
N.insert(0, 1)
那么大佬的代码是怎么对两端的1做处理的呢,以N=[1,2,1]为例,s=N,s.append(0)=>[1,2,1,0],len(s)=4,range(4)=[0,1,2,3],用i去遍历的时候,当i=0,s[i-1]+s[i]=0+1=1,当i=1,s[0]+s[1]=3,当i=2,s[1]+s[2]=3,当i=3,s[2]+s[3]=1,最后得出[1,3,3,1],此处对这个加0操作给予膜拜ORZ
6.迭代器:可以直接作用于for循环的数据类型有以下几种,一类是集合数据类型,如list、tuple、dict、set、str等,一类是generator,包括生成器和带yield的函数。前面有提到生成器是可以被next()调用并返回下一个值的,那么,可以被next()函数调用并不断返回下一个值的对象称为迭代器Iterator,生成器都是Iterator对象,但list、dict、str虽然是Iterable,但不是Iterator,如果要把Iterable变成Iterator,可以使用iter()函数
Python的Iterator对象表示的是一个数据流,被next()函数调用时候会不断返回下一个数据,因此可以表示一个无限大的数据流,而且Iterator的计算是惰性的,只有在需要返回下一个数据时它才会计算。