1.python 内置了两种序列结构
容器类型:list,tuple, collections.deque
扁平类型:str, bytes, bytearray,
区别 : 容器类型存储的是对象的引用,而扁平类型存放的是值。
2.列表推到式
一般的情况下,列表推导式一般用于创建列表,可以对列表进行加工并重新创建一个列表。
numbers = [i for i in range(10)]
print(numbers)
结果:
C:\python35\python.exe E:/pytest/pytest8.5.py
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Process finished with exit code 0
补充:在使用list.sort方法时,我们可以进行排序,但是这个排序是在其原来的对象上进行排序的。
sorted()则会创建一个新的对象进行排序。
3.元组拆包
可以使用_作为占位符
import os
_,log_name = os.path.split("/var/openstack/nova.log") #_作为占位符使用
print(log_name)
C:\python35\python.exe E:/pytest/pytest8.5.py
nova.log
Process finished with exit code 0
4.*处理多余的元素
a, b, *rest = range(6)
print(a, b, rest)
C:\python35\python.exe E:/pytest/pytest8.5.py
0 1 [2, 3, 4, 5]
Process finished with exit code 0