文章目录
列表生成式
range(stop) [0, 1, …, stop - 1]
range(start, stop[, step])
python 3 为了防止生成的列表没有被使用,不会立即生成列表
列表推导式
[表达式 for 变量 in 列表 if 条件]
nums = [1, 2, 3, 4, 5]
resultList = [num ** 2 for num in nums]
print(resultList)
[1, 4, 9, 16, 25]
nums = [1, 2, 3, 4, 5]
resultList = [num ** 2 for num in nums if num % 2 != 0]
print(resultList)
[1, 9, 25]
增
append()
在末尾追加
insert()
insert(index, object)
nums = [1, 2, 3, 4, 5]
nums.insert(1, 6)
print(nums)
[1, 6, 2, 3, 4, 5]
extend()
往列表中扩展另外一个可迭代序列
extend(iterable)
和 append的区别:extend拼接两个集合,append把元素追加到一个集合
nums = [1, 2, 3, 4, 5]
nums2 = ['a','b', 'c']
nums.extend(nums2)
print(nums)
[1, 2, 3, 4, 5, ‘a’, ‘b’, ‘c’]
乘法运算
[‘a’] * 3
nums = [1, 2]
print(nums * 3)
[1, 2, 1, 2, 1, 2]
加法运算
与extend的区别: 只能列表类型和列表类型相加
nums = [1, 2]
nums2 = [1, 2]
print(nums + nums2)
[1, 2, 1, 2]
删
del语句
可以删除一个指定的元素(对象)
nums = [1, 2, 3, 4, 5]
del nums[1]
print(nums)
del nums
[1, 3, 4, 5]
pop
移除并返回列表中指定索引对应元素
l.pop(index=-1)
nums = [1, 2, 3, 4, 5]
result = nums.pop()
print(result, nums)
result = nums.pop(1)
print(result, nums)
5 [1, 2, 3, 4]
2 [1, 3, 4]
remove
移除列表中的指定元素
remove(object)
元素不存在会报错
不能在遍历列表过程中remove 元素
nums = [1, 2, 3, 2, 4, 5]
nums.remove(2)
print(nums)
[1, 3, 2, 4, 5]
改
通过索引来操作指定元素
names[index] = 666
查
index()
获取元素的索引
nums = [1, 2, 3, 2, 4, 5]
idx = nums.index(2)
print(idx)
1
count()
获取指定元素的个数
nums = [1, 2, 3, 2, 4, 5]
c = nums.count(2)
print(c)
2
获取多个元素
切片 items[start : end : step]
nums = [1, 2, 3, 2, 4, 5]
pic = nums[::]
print(pic)
pic = nums[1:4:]
print(pic)
pic = nums[::-1]
print(pic)
[1, 2, 3, 2, 4, 5]
[2, 3, 2]
[5, 4, 2, 3, 2, 1]
遍历
for item in list:
for index in range(len(list)):
遍历枚举对象
通过枚举函数,生成一个新的对象,函数用于将一个可遍历的数据对象组合为一个索引序列,同时列出数据下表和数据。
enumerate(sequence[, start=0])
values = ['a', 'b', 'c', 'd', 'e']
print(enumerate(values))
print(list(enumerate(values)))
# for tupleValue in enumerate(values):
for idx, val in enumerate(values, 1):
print(idx, val)
<enumerate object at 0x03A86F58>
[(0, ‘a’), (1, ‘b’), (2, ‘c’), (3, ‘d’), (4, ‘e’)]
1 a
2 b
3 c
4 d
5 e
使用迭代器进行遍历
可迭代对象
判定依据:能作用于for in
判定方法:
import collections
isinstance(obj, collections.Iterable)
import collections
nums = [1, 2, 3]
result = isinstance(nums, collections.Iterable)
print(result)
d:\python\python\test3.py:4: DeprecationWarning: Using or importing the ABCs from ‘collections’ instead of from ‘collections.abc’ is deprecated, and in 3.8 it will stop working
result = isinstance(nums, collections.Iterable)
True
迭代器
是可以记录遍历位置的对象,从第一个元素开始,往后通过next()函数,进行遍历,只能往后,不能往前
判定依据: 能作用于next()函数
判定方法:
import collections
isinstance(obj, collections.Iterator)
import collections
nums = [1, 2, 3]
result = isinstance(nums, collections.Iterator)
print(result)
d:\python\python\test3.py:4: DeprecationWarning: Using or importing the ABCs from ‘collections’ instead of from ‘collections.abc’ is deprecated, and in 3.8 it will stop working
result = isinstance(nums, collections.Iterator)
False
迭代器也是可迭代对象,所以也可以作用于for in
为啥会产生迭代器?
仅仅在迭代到某个元素时才处理该元素,在此之前,元素可以不存在,在此之后元素可以被销毁,特别适合用于遍历一些巨大的或是无限的集合。
提供了一个统一的访问集合的接口,iter(Iterable)
因为迭代器比较常用,所以在python中,可以直接作用与for in
如果取出完毕再继续取,则会报错StopIteration
迭代器不能多次迭代,只能迭代一次。
import collections
l = [1, 2, 3, 4, 5]
it = iter(l)
# print(next(it))
for v in it:
print(v)
1
2
3
4
5
判定
元素 in 列表
元素 not in 列表
比较
==
>
<
result = [2, 3, 4] > [2, 3, 3]
print(result)
True
排序
内建函数 sorted
可以对所有可迭代对象进行排序
sorted(iterable, key=None,reverse=False)
s = 'jfklwoaensd'
s = [1, 4, 7, 3, 6]
result = sorted(s)
print(result)
[1, 3, 4, 6, 7]
s = [('sz', 18), ('sz2', 19), ('sz1', 11), ('sz3', 20)]
def getKey(x):
return x[1]
result = sorted(s, key=getKey, reverse=True)
print(result)
[(‘sz3’, 20), (‘sz2’, 19), (‘sz’, 18), (‘sz1’, 11)]
列表对象方法list.sort
list.sort(key=None, reverse=False)
l = [1, 5, 3, 8, 2, 9]
res = l.sort()
print(res, l)
None [1, 2, 3, 5, 8, 9]
乱序
import random
l = [1, 5, 3, 8, 2, 9]
res = random.shuffle(l)
print(res, l)
None [1, 9, 3, 8, 2, 5]
反转
import random
l = [1, 5, 3, 8, 2, 9]
res = l.reverse()
print(res, l)
None [9, 2, 8, 3, 5, 1]
切片反转:
l = [1, 5, 3, 8, 2, 9]
res = l[::-1]
print(res, l)
[9, 2, 8, 3, 5, 1] [1, 5, 3, 8, 2, 9]