Python列表详解

Python中有四种集合数据类型:

  • 列表:有序,可变序列,允许有重复的元素。
  • 元组:有序,不可变序列,允许有重复的元素。
  • 集合:无序,无索引,无重复的成员。
  • 字典:无序,有索引,可变序列,无重复的成员。

可变序列和不可变序列

可变序列:可以进行增、删、改操作,并且执行完增删改操作后,对象的地址不会发生更改。如列表、字典、集合。

不可变序列:没有增、删、改操作。如字符串、元组。

列表

python中没有数组类型,列表就相当于其他语言中的数组。可以存储N多个不同数据类型的元素。

变量存储的是一个对象的引用,而数组存储的是多个对象的引用。

a = 10  # 存储的是一个对象的引用

lst = ['hello','python',10]

print(id(lst))  # 1835963119872
print(type(lst))    # <class 'list'>
print(lst)      # ['hello', 'python', 10]
print(id(lst[0]),type(lst[0]),lst[0])   # 1835963075440 <class 'str'> hello
print(id(lst[1]),type(lst[1]),lst[1])   # 1835963075248 <class 'str'> python
print(id(lst[2]),type(lst[2]),lst[2])   # 1835961745936 <class 'int'> 10
图片[1]-Python列表详解-尤尤'blog
lst对象的内存示意图

列表的特点:元素有序排序;索引映射唯一数据;列表元素可以重复;可以多种类型的数据混存;根据需要动态分配和回收内存。

创建列表对象的三种方式

1)使用[],元素之间用英文版的逗号隔开。

#列表对象名  赋值号  [元素,元素,……]
lst = ['hello','python',10]

2)内置函数list()

lst1 = list(['hello','python',10])  # lst1实际上存储的是对象的引用

3)列表生成式

列表中元素有规则的才可以使用列表生成式。

语法格式:

图片[2]-Python列表详解-尤尤'blog

注意:表示列表元素的表达式中一般包含自定义变量。

lst = [i for i in range(2,11,2)]
print(lst)  # [2, 4, 6, 8, 10]

lst = [i+1 for i in range(0,9,2)]
print(lst)  # [1, 3, 5, 7, 9]

列表查询的四种方式

1)获取指定元素的索引 index()

若所查询的列表中有N个相同的元素,只返回相同元素的第一个元素的索引。如果查询的元素在列表中不存在,则抛出异常ValueError。

lst = ['hello','python',10,'hello',22]

# 若所查询的列表中有N个相同的元素,只返回相同元素的第一个元素的索引
print(lst.index('hello'))   # 0

# 如果查询的元素在列表中不存在,则抛出异常ValueError
# print(lst.index('world'))   # ValueError: 'world' is not in list

# 在指定的start和stop之间进行查找:这个查找范围不包含stop
# 在索引[1,4)之间查找'hello'
print(lst.index('hello',1,4))    # 3

2)获取单个元素

根据索引号获取单个元素。索引又分为正向索引(0~N-1)和逆向索引(-N~-1),如果指定的索引不存在,则抛出IndexError。

lst = ['hello','world',90,'hello','python',10]
# 正向索引0~N-1
print(lst[2])   # 90
# 逆向索引-N~-1
print(lst[-3])  # hello
# 如果指定的索引不存在,则抛出IndexError。
print(lst[8])   # IndexError: list index out of range

3)获取多个元素 切片操作

通过切片操作可以得到原列表片段的拷贝(新的列表对象)。它的范围是一个左闭右开的区间[start,stop)

图片[3]-Python列表详解-尤尤'blog
lst = [0,10,20,30,40,50,60,70,80,90]
# step为默认值1
print(lst[2:6]) # [20, 30, 40, 50]
# step为正数,从start开始往后计算
print(lst[2:8:2])   # [20, 40, 60]
# stop默认是列表的最后一个元素
print(lst[2::2])    # [20, 40, 60, 80]
# start默认是列表的第一个元素
print(lst[:8:2])    # [0, 20, 40, 60]

# step为负数,从start开始往前计算
print(lst[5:9:-1])  # []    从右往左切片,start要大于stop,否则切不出数据
print(lst[5:2:-1])  # [50, 40, 30]
# stop默认是列表的第一个元素
print(lst[6::-1])   # [60, 50, 40, 30, 20, 10, 0]
# start默认是列表的最后一个元素。
print(lst[:2:-2])   # [90, 70, 50, 30]
# 逆向索引
print(lst[-2:-6:-1])    # [80, 70, 60, 50]

4)in/not in 判断指定元素在列表中是否存在

元素 in 列表名 元素在列表中则返回True,否则返回False。

元素 not in 列表名 元素不在列表中返回True,否则返回False。

lst = ['hello','python',10,'hello',22]

print(10 in lst)    # True
print('world' in lst)   # False

print('python' not in lst)  # False
print(99 not in lst)    # True

列表元素的遍历

使用for循环来遍历

'''
for 迭代变量 in 列表名:
    操作
'''
lst = ['hello','python',10,'hello',22]
for i in lst:
    print(i)

获取列表的长度

使用len()获取列表的长度

lst = [10,20,30,40,50,60,70,80]
print(len(lst)) # 8

列表添加元素的五种方式

append()、extend()和insert()添加元素不会产生新的列表对象。

1)append() 在列表的末尾添加一个元素

lst = [10,20,30,40]
print('添  加 之   前:',lst,id(lst))    # 添  加 之   前: [10, 20, 30, 40] 2090343772032
lst.append(66)
print('append添加之后:',lst,id(lst))    # append添加之后: [10, 20, 30, 40, 66] 2090343772032

2)extend() 在列表的末尾至少添加一个元素(可以添加任何可迭代的元素)

向末尾一次添加多个元素。

lst = [10,20,30,40]
print('添  加 之   前:',lst,id(lst))    # 添  加 之   前: [10, 20, 30, 40] 1879945238016
lst1 = ['hello','python']

# lst.append(lst1)    # append是将lst1作为一个元素添加到了lst末尾
# print('append添加之后:',lst,id(lst))    # append添加之后: [10, 20, 30, 40, ['hello', 'python']] 2014321235456

# 把lst1的每个元素都添加到lst的末尾
lst.extend(lst1)
print('extend添加之后:',lst,id(lst))    # extend添加之后: [10, 20, 30, 40, 'hello', 'python'] 1879945238016

3)insert() 在指定位置添加元素

lst = [10,20,30,40]
print(id(lst))  # 2229385805888

lst.insert(1,'hello')   # 在索引为1的位置上添加元素'hello'

print(id(lst))  # 2229385805888
print(lst)  # [10, 'hello', 20, 30, 40]

4)+

会产生新的列表对象。

lst = [10,20,30,40]
lst1 = ['hello',99]
print(id(lst))  # 2623322576384
print(lst+lst1,id(lst+lst1)) # [10, 20, 30, 40, 'hello', 99] 2623325796160

5)切片

如果切片中的start和stop在列表的长度范围内,就是覆盖。

lst = [10,20,30,40,50]
lst[15:] = [66,77]   # 超出了列表长度,就是将其他元素添加到列表末尾
print('切片添加',lst)   # 切片添加 [10, 20, 30, 40, 50, 66, 77]
lst[len(lst):] = [99,77]
print(lst)  # [10, 20, 30, 40, 50, 66, 77, 99, 77]

列表删除元素的五种方式

1)remove()

一次删除一个元素,元素重复则删除第一个。如果元素不存在就抛出异常ValueError。

lst = ['hello','world',90,'hello','python',10]
# 一次只删除一个元素,如果重复则删除第一个
lst.remove('hello')
print(lst)  # ['world', 90, 'hello', 'python', 10]
# 如果要删除的元素不存在则抛出异常ValueError
lst.remove(66)  # ValueError: list.remove(x): x not in list

2)pop()

删除一个指定索引位置上的元素,如果不指定索引,则删除列表的最后一个元素。指定的索引不存在,抛出异常IndexError。

lst = ['hello','world',90,'hello','python',10]
# 删除索引为2的元素
lst.pop(2)
print(lst)  # ['hello', 'world', 'hello', 'python', 10]
# 若不指定索引,删除列表最后一个元素
lst.pop()
print(lst)  # ['hello', 'world', 'hello', 'python']
# 如果指定索引不存在,则抛出异常
lst.pop(8)  # IndexError: pop index out of range

3)clear() 清空列表

清空列表元素,只留下一个空列表,列表对象还在。

lst = ['hello','world',90,'hello','python',10]
lst.clear()     # 清空列表元素
print(lst)  # []

4)del关键字

可以用于删除指定索引的元素,也可以直接删除整个列表对象。

lst = [10,20,30,40,50]
# 删除指定索引的元素
del lst[2]      # [10, 20, 40, 50]
print(lst)
# 删除列表对象
del lst
print(lst)  # NameError: name 'lst' is not defined. Did you mean: 'lst1'?

5)切片

将不要的部分切走,用空列表替代不要的那部分。可用作删除多个对象。

lst = [0,10,20,30,40,50,60,70,80,90]
print('原列表:',lst,id(lst))   # 原列表: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90] 2197633947136
lst[1:4] = []       # 用空列表替代不要的那部分元素
print('切片后:',lst,id(lst))   # 切片后: [0, 40, 50, 60, 70, 80, 90] 2197633947136

列表修改元素的两种方式

1)索引

给指定索引的元素赋予一个新值。一次修改一个值。

lst = [10,20,30,40,50]
# 将索引为2的元素修改为999
lst[2] = 999
print(lst)  # [10, 20, 999, 40, 50]

2)切片

可以用于修改多个值。

lst = [10,20,30,40,50]
# 将[1,3)的元素修改为111,222,333
lst[1:3] = [111,222,333]
print(lst)  # [10, 111, 222, 333, 40, 50]

列表的两种排序方式

1)sort()方法

列表中所有元素默认从小到大的顺序进行排序(升序),可以通过设置参数 reverse=True 进行降序排序。

lst = [40,10,32,22,54]
print(id(lst),lst)  # 1974048511488 [40, 10, 32, 22, 54]
lst.sort()  # 默认升序
print(id(lst),lst)  # 1974048511488 [10, 22, 32, 40, 54]
# 通过设置参数进行降序排序
lst.sort(reverse=True)
print(id(lst),lst)  # 1974048511488 [54, 40, 32, 22, 10]

lst = ['A','a','hello','python']
lst.sort()
print(lst)  # ['A', 'a', 'hello', 'python']

sort()函数还可以指定参数

list_name.sort(key=None,reverse=False)

# reverse默认是False

key是用来比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素进行排序。

2)内置函数sorted()

默认升序,可以通过设置参数 reverse=True 进行降序排序。产生新的列表对象。

lst = [40,10,32,22,54]
print(id(lst),lst)      # 2175533887360 [40, 10, 32, 22, 54]
# 默认升序
new_lst = sorted(lst)
print(id(new_lst),new_lst)  # 2175530096128 [10, 22, 32, 40, 54]
# 通过设置参数进行降序排序
new_lst1 = sorted(lst,reverse=True)
print(id(new_lst1),new_lst1)    # 2175534036800 [54, 40, 32, 22, 10]

sort()和sorted()区别:sort()是在原列表上进行排序的,不产生新的列表对象。而sorted()会产生新的列表对象,原列表不变。

创建空列表

# 使用[]
lst = []
# 使用内置函数
lst1 = list()

列表复制的三种方法

1)list2 = list1

这里的list2只是对list1的引用,在list1中做的更改,list2也会跟着变化。他们两个指向的内存地址相同。

lst = [10,20,30,40,50]
lst2 = lst
print(id(lst2),lst2,lst,id(lst))    # 2857576429376 [10, 20, 30, 40, 50] [10, 20, 30, 40, 50] 2857576429376
lst.pop()
print(id(lst2),lst2,lst,id(lst))    # 2857576429376 [10, 20, 30, 40] [10, 20, 30, 40] 2857576429376

2)copy()方法

产生一个新的列表对象。

lst = [10,20,30,40,50]
lst1 = lst.copy()
print(id(lst1),lst1,lst,id(lst))    # 2279989424000 [10, 20, 30, 40, 50] [10, 20, 30, 40, 50] 2279989573824

3)内置函数list()

产生一个新的列表对象。

lst = [10,20,30,40,50]
lst3 = list(lst)
print(id(lst3),lst3,lst,id(lst))    # 2279989606400 [10, 20, 30, 40, 50] [10, 20, 30, 40, 50] 2279986576000

列表合并的三种方法

1)+ 会产生新的内存对象

list1 = [20,30,40]
list2 = ['hello','python']
print(list1+list2)  # [20, 30, 40, 'hello', 'python']

2)append() 把list2中的元素一个一个追加到list1中

list1 = [20,30,40]
list2 = [333,444,555]
print(id(list1),list1)  # 2826464849920 [20, 30, 40]
for i in list2:
    list1.append(i)
print(id(list1),list1)  # 2826464849920 [20, 30, 40, 333, 444, 555]

3)extend() 把list2添加到list1末尾

list1 = [20,30,40]
list2 = ['hello','python']
list1.extend(list2)
print(list1)    # [20, 30, 40, 'hello', 'python']
© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容