列表是在一个连续的内存空间,使用索引查找值非常快,不建议插入和删除,可以最后加入,不影响
创建列表
l1 = []
l2 = list(range(3))
l3 = [1,2,‘abc’,str]
l4 = [1,[2,3],None]
使用索引查找值(注意超界问题)
l5 = [1,2,3,4,5]
l5[0] #从左边往右边,从0开始,最长为len(l5)-1
1
l5[4]
5
l5[-1] #从右边往做左边,从-1开始,最长为-len(l5)
5
l5[5] #由于l5列表长度为5,所以正索引最长为4
IndexError Traceback (most recent call last) in ----> 1 l5[5]IndexError: list index out of range
l5[-6] #由于l5列表长度为5,所以正索引最长为-5
IndexError Traceback (most recent call last) in ----> 1 l5[5]IndexError: list index out of range
查看列表值的索引号 (效率低,建议少用,如果查找的值不在列表中,直接抛异常)
l6 = [1,2,3,‘abc’,3]
l6.index(3)
2
l6.index(‘abc’)
3
l6.index(‘bcd’)
---------------------------------------------------------------------------ValueError Traceback (most recent call last) in ----> 1 l6.index(‘bcd’)ValueError: ‘bcd’ is not in list
计算列表值相同的值(效率低,建议少用)
l6.count(3)
2
计算列表长度 #只计算一级列表长度,嵌入的算1个,所以如下列表长度为4
l7 = [1,2,[3,4],5]
len(l7)
4
列表追加 #只能单个值追加到最后,或者作为一个整体的列表
l8 = [1,2,3]
l8.append(4)
l8
[1, 2, 3, 4]
l8.append([5,6])
l8
[1, 2, 3, 4, [5, 6]]
列表值的替换(赋值,修改)
l8 = [1, 2, 3, 4, [5, 6]]
l8[-1] = [7,8,9]
l8
[1, 2, 3, 4, [7, 8, 9]]
l8[-1] = [10,]
l8
[1, 2, 3, 4, [10]]
列表插入值 (效率低,建议少用,有超出索引值的情况插入,右端超界,相当于尾部追加,左端超界,那就是在0索引位置插入)
l9 = [1,2,3,‘abc’]
l9.insert(0,100)
l9
[100, 1, 2, 3, ‘abc’]
l9 = [100, 1, 2, 3, ‘abc’] #在l9列表索引为100(超出左边索引)的位置插入值100,实际为最后面插入
l9.insert(100,100)
l9
[100, 1, 2, 3, ‘abc’, 100]
l10 = [100, 1, 2, 3, ‘abc’, 100]
l10.insert(-100,200) #在l10列表索引为-100(超出左边索引)的位置插入值200,实际为最前面插入
l10
[200, 100, 1, 2, 3, ‘abc’, 100]
列表扩展(尾部追加多个元素,可迭代对象)
l11 = [1,2,3]
l12 = [5,6,[7,8]]
l11.extend(l12) #扩展可迭代对象列表l12
l11
[1, 2, 3, 5, 6, [7, 8]]
l11.extend([9,10]) #扩展可迭代对象列表[9,10]
l11
[1, 2, 3, 5, 6, [7, 8], 9, 10]
列表+ 列表 组合新列表,原列表不变化 (少做,会增加内存使用数量,实际相当于得到一个新的列表)
l13 = [1,2,3]
l14 = [4,5]
l15 = l13 + l14
print(l13)
print(l14)
print(l15)
[1, 2, 3]
[4, 5]
[1, 2, 3, 4, 5]
列表数字 列表重复(注意嵌套列表是浅拷贝,改一个,改所有)
l16 = [1,2,3]
l17 = l16 * 2
l17
[1, 2, 3, 1, 2, 3]
l18 = [1,2,[3,4]]
l19 =l18 * 2
l18[2][0] = 5 #修改嵌套的列表值时,使用得到的列表全部会变
l19
[1, 2, [5, 4], 1, 2, [5, 4]]
l18
[1, 2, [5, 4]]
列表拷贝 copy(注意嵌套列表,只拷贝指针,都是浅拷贝;需要深拷贝需要看copy模块)
l20 = [1,2,[3,4]]
l21 = l20.copy()
l21
[1, 2, [3, 4]]
l21[2][0] = 5 #修改嵌套列表中的值
l21
[1, 2, [5, 4]] #copy过来的值已经被修改
l20
[1, 2, [5, 4]] #原始的列表值也被修改了,
移除元素(效率低,建议少用)remove(value)
l22 = [1,2,3,4,5]
l22.remove(4)
l22
[1, 2, 3, 5]
移除索引值对应的值(效率低,建议少用) pop(index) list.pop()–>直接最后一个删除
l23 = [1,2,3,4,5]
l23.pop(0) #指定值,则删除值,然后列表后续值往前移动
l23
[2, 3, 4, 5]
l23.pop() # 不带值,则直接删除列表最后一个值
l23
[2, 3, 4]
清空列表 clear
l24 = [1,2,3,4,5]
l24.clear() #直接清空列表,列表为空
l24
[]
列表翻转(效率低,因为将列表每个元素都重新写了,建议少用)reverse()
l25 = [1,2,3,4,5]
l25.reverse()
l25
[5, 4, 3, 2, 1]
排序(效率低,建议少用,同类型排序,带KEY参数转换后比较)sort()
l26 = [1,6,3,9,2]
l26.sort() #默认是升序排列
l26
[1, 2, 3, 6, 9]
l27 = [1,6,3,9,2]
l27.sort(reverse=True) #带参数reverse=True时,则为降序排列
l27
[9, 6, 3, 2, 1]
l28 = [1,2,3,‘abc’,[1,2]] #列表中有不同类型的,则排序会出现问题
l28.sort(reverse=True)
l28
---------------------------------------------------------------------------TypeError Traceback (most recent call last) in 1 l28 = [1,2,3,‘abc’,[1,2]]----> 2 l28.sort(reverse=True) 3 l28 TypeError: ‘<’ not supported between instances of ‘str’ and ‘list’
l28 = [1,2,3,‘abc’,[1,2]]
l28.sort(key=str,reverse=True) #强制将列表中的所有元素转换成str进行比较
l28
[‘abc’, [1, 2], 3, 2, 1]
in(是否存在之内) 返回bool型
l28 = [1,2,3,‘abc’,[1,2]]
1 in l28
True
l28 = [1,2,3,‘abc’,[1,2]]
[1,2] in l28 #[1,2]这个列表在l28列表中
True
l28 = [1,2,3,‘abc’,[1,2]]
[1,3,2] in l28 #[1,3,2]列表不在列表l28中
False
random模块,随机数
import random
random.randint() 随机生成数值中间的整数
random.randint(1,3)
返回1或者2 ,是[1,2,3) 是一个前开后闭的随机数
random.randrange() 随机生成数值中间的整数,和range比较,前包后不包
random.randrange(5)
返回值为[0,1,2,3,4,5) 随机值前开后闭的整数
random.choice(iterable) 随机取1个
l29 = [1,2,3,‘abc’]
random.choice(l29)
返回值为列表l29中的随机的一个值
random.sample() 取样,1个值不能取2次,和choice比对
l29 = [1,2,3,‘abc’]
random.sample(l29,2) #返回值为列表l2随机2个
[3, 1]
l29 = [1,2,3,‘abc’]
random.sample(l29,5) #列表l29元素之有4个,如果取样5个则会报错
---------------------------------------------------------------------------ValueError Traceback (most recent call last) in 1 l29 = [1,2,3,‘abc’]----> 2 random.sample(l29,5)c:\users\marc\appdata\local\programs\python\python38\lib\random.py in sample(self, population, k) 361 n = len(population) 362 if not 0 <= k <= n:–> 363 raise ValueError(“Sample larger than population or is negative”) 364 result = [None] * k 365 setsize = 21 # size of a small set minus size of an empty listValueError: Sample larger than population or is negative
random.shuffle() 打乱,重新排列
l29 = [1,2,3,‘abc’]
random.shuffle(l29) #将列表l29重新排列
l29
[‘abc’, 3, 1, 2]