Python 之 列表 list 的深入浅出

1、概念

  • 列表是一个队列,一个排列整齐的队伍
  • 列表内的个体称作元素,由若干个元素组成列表
  • 元素可以是任意对象(数字 字符串 对象 列表等)
  • 列表内元素有顺序,可以使用索引
  • 列表是线性的数据结构
  • 列表使用[]表示
  • 列表是可变的,所以不可hash

2、列表 list 定义初始化

  • list()返回一个空列表

  • []也代表一个空列表

  • list(iterable=())由可迭代对象生成一个列表哦

    list1 = list(range(5))
    list2 = list()
    list3 = []
    list4 = [list1, list2]
    for i in [list1, list2, list3, list4]:
        print(i, type(i))
    
    [0, 1, 2, 3, 4] <class 'list'>
    [] <class 'list'>
    [] <class 'list'>
    [[0, 1, 2, 3, 4], []] <class 'list'>
    

3、列表 list 操作

3.1 列表索引访问

  • 索引:也叫下标

  • 正索引:从左至右,从0开始,依次+1,为列表中每一个元素编号

  • 负索引:从右至左,从-1开始,依次-1

  • 正负索引均不可以超界,否则引发异常 IndexError

  • 列表通过索引访问list[index] index 就是索引,使用中括号访问

    list1 = list(range(6))
    print(list1)
    print(list1[0], list1[3], list1[5])
    print(list1[-1], list1[-3], list1[-6])
    
    [0, 1, 2, 3, 4, 5]
    0 3 5
    5 3 0
    

3.2 列表查询

3.2.1 idnex(value, [start, [stop]])

  • 通过value值,从指定区间查找列表内的元素是否匹配

  • 匹配到第一个值就立即返回索引

  • 匹配不到,抛出ValueError

  • 时间复杂度是0(n)

  • 随着列表数据规模的增大,而效率下将

    list2 = [0, 1, 2, 3, 2, 3]
    list2.index(2)  # 2
    list2.index(2, 2, 5)  # 2
    list2.index(2, 3, 5)  # 4
    list2.index(2, 5)  # ValueError 2 is not in list
    

3.2.2 count(value)

  • 返回列表中匹配value的次数

  • 时间复杂度是0(n)

  • 随着列表数据规模的增大,而效率下将

    list2 = [0, 1, 2, 3, 2, 3]
    list2.count(2)  # 2
    list2.count(0)  # 1
    list2.count(6)  # 0
    

3.2.3 len()

  • 返回列表长度,即列表内含有的元素个数

    list2 = [0, 1, 2, 3, 2, 3]
    len(list2)  # 长度为 6
    

3.3 列表元素修改

3.3.1 list[index] = value

  • 通过索引访问进行value修改

  • 索引不能超界

    list2 = [0, 1, 2, 3, 2, 3]
    list2[0] = 6
    list2  # [6, 1, 2, 3, 2, 3]
    list2[6] = 6  # IndexError  list assignment index out of range
    

3.3.2 append(object) -> None

  • 列表尾部追加元素,返回None

  • 返回None就意味着没有新的列表产生,就地修改

  • 时间复杂度是O(1)

    list2 = [0, 1, 2, 3, 2, 3]
    list2.append(1)  # 就地修改,返回 None
    list2  # [0, 1, 2, 3, 2, 3, 1]
    

3.3.3 insert(index, objext) -> None

  • 在指定的索引index处插入元素objext

  • 返回None就意味着没有新的列表产生,就地修改

  • 时间复杂度是O(n)

  • 索引可以超界:超越上界,尾部追加;超越下界,头部追加

    list2 = [0, 1, 2, 3, 2, 3]
    list2.insert(0, 9)  
    list2  # [9, 0, 1, 2, 3, 2, 3]
    

3.3.4 extend(iterable) -> None

  • 将可迭代对象的元素追加进来,返回 None

  • 就地修改

    list2 = [0, 1, 2, 3, 2, 3]
    list2.extend([1, 2])
    list2  # [0, 1, 2, 3, 2, 3, 1, 2]
    

3.3.5 + -> list

  • 连接操作,将两个列表连接起来

  • 产生新的列表,原列表不变

  • 本质上调用的是魔术方法__add__

    list3 = [1, 2]
    list4 = [3, 5]
    list5 = list3 + list4
    list5  # [1, 2, 3, 5]
    

3.3.6 * -> list

  • 重复操作,将本列表元素重复n次,返回新的列表

    list3 = [1, 2]
    list5 = list3 * 3
    list5  # [1, 2, 1, 2, 1, 2]
    

3.3.7 列表相乘时注意事项

# 列表里的元素列表是引用地址,改一个元素会影响其它引用此地址的变量
x = [[1, 2, 3]] * 3
print(x)
x[0][1] = 20
print(x)
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
[[1, 20, 3], [1, 20, 3], [1, 20, 3]]
y = [1] * 5
print(y)
y[0] = 6
y[1] =7
print(y)
[1, 1, 1, 1, 1]
[6, 7, 1, 1, 1]

3.4 列表删除元素

3.4.1 remove(value) -> None

  • 从左至右查找第一个匹配value的值,并移除该元素,返回None

  • 就地修改

    list2 = [0, 1, 2, 3, 2, 3]
    print(list2.remove(2)) # None 返回 None,就地修改
    list2  # [0, 1, 3, 2, 3]
    

3.4.2 pop(index) -> iterm

  • 不指定索引index,就从列表尾部弹出一个元素

  • 指定索引index,就从索引处弹出一个元素,索引超界抛出IndexError错误

    list2 = [0, 1, 2, 3, 2, 3]
    list2.pop()  # 3
    list2.pop(0)  # 0
    list2  # [1, 2, 3, 2]
    

3.4.3 clear() -> None

  • 清除列表中所有的元素,返回一个空列表

    list2 = [0, 1, 2, 3, 2, 3]
    list2.clear()
    list2  # []
    

3.5 列表其它操作

3.5.1 reverse() -> None

  • 将列表元素反转,返回None

  • 就地修改

    list2 = [0, 1, 2, 3, 2, 3]
    list2.reverse()  # 返回 None 就地修改
    list2  # [3, 2, 3, 2, 1, 0]
    

3.5.2 sort(key=None, reverse=False) -> None

  • 对列表元素进行排序,就地修改,默认升序

  • reverseTrue,反转,降序

  • key是一个函数,指定key如何排序

    list2 = [0, 1, 2, 3, 2, 3]
    list2.sort()
    list2  # [0, 1, 2, 2, 3, 3]
    
    list2 = [0, '1', 'a', 'A', 2, 3]
    # list2.sort()  # TypeError  '<' not supported between instances of 'str' and 'int'
    list2.sort(key=str)
    list2  # [0, '1', 2, 3, 'A', 'a']
    

3.5.3 in成员身份判断

[3, 4] in [1, 2, 3, 4]  # False
[3, 4] in [1, 2, [3, 4]] # True
[4, 3] in [1, 2, [3, 4]] # False
1 in [1, 2, 3, 4]  # True
for x in [1, 2]:print(x, end=' ')  # 1 2 

3.5.4 == is判断

list0 = list(range(4))
list2 = list(range(4))
print(list0, list2)  # [0, 1, 2, 3] [0, 1, 2, 3]
print(list0 == list2)  # True
list1 = list0 
print(list1 == list0, list1 is list0)  # True True
print(id(list1), id(list0)) # 125343432 125343432
list1[2] = 10
print(list0, list1)  # [0, 1, 10, 3] [0, 1, 10, 3]

3.6 列表复制

list1 = [0, 1, 2, 3, 2, 3]
print(list1 == list2)  # True
list2 = list1.copy()
print(list2)  # [0, 1, 2, 3, 2, 3]
print(id(list1), id(list2))  # 117074440 117186760
# 两个独立的列表

3.6.1 浅拷贝shadow copy

  • 影子拷贝,也叫浅拷贝,遇到引用类型,只是复制了一个引用地址而已

    # 影子拷贝,也叫浅拷贝,遇到引用类型
    # 只是复制了一个引用地址而已
    list1 = [1, [2, 3, 4], 5]
    list2 = list1.copy()
    print(list1, list2, id(list1), id(list2))
    print(list1 == list2, list1 is list2)
    list1[1][1] = 30
    print(list1, list2, id(list1), id(list2))
    print(list1 == list2, list1 is list2)
    
    [1, [2, 3, 4], 5] [1, [2, 3, 4], 5] 117373320 121948616
    True False
    [1, [2, 30, 4], 5] [1, [2, 30, 4], 5] 117373320 121948616
    True False
    

在这里插入图片描述

3.6.2 深拷贝deepcopy

  • copy模块提供了deepcopy

    # 深拷贝
    import copy
    list1 = [1, [2, 3, 4], 5]
    list2 = copy.deepcopy(list1)
    print(list1, list2, id(list1), id(list2))
    print(list1 == list2, list1 is list2)
    list1[1][1] = 30
    print(list1, list2, id(list1), id(list2))
    print(list1 == list2, list1 is list2)
    
    [1, [2, 3, 4], 5] [1, [2, 3, 4], 5] 82383176 82454728
    True False
    [1, [2, 30, 4], 5] [1, [2, 3, 4], 5] 82383176 82454728
    False False
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值