1、作用:按位置存放多个值
2、定义
l=[1,1.2,'a'] # l=list([1,1.2,'a'])
print(type(l)) #<class 'list'>
3、类型转换: 但凡能够被for循环遍历的类型都可以当做参数传给list()转成列表
res=list(‘hello’)
print(res) # ['h', 'e', 'l', 'l', 'o']
res=list({'k1':111,'k2':222,'k3':3333})
print(res) #结果:['k1', 'k2', 'k3']
4、内置方法
优先掌握的操作:
①按索引存取值(正向存取+反向存取):即可以取也可以改
l=[111,'egon','hello']
# 正向取
print(l[0])
# 反向取
print(l[-1])
# 可以取也可以改:索引存在则修改对应的值
l[0]=222
print(l) # [222, 'egon', 'hello']
# 无论是取值操作还是赋值操作:索引不存在则报错
l[3]=333
5、切片(顾头不顾尾,步长)
l = [111, 'egon', 'hello', 'a', 'b', 'c', 'd', [1, 2, 3]]
print(l[0:3]) #[111, 'egon', 'hello']
print(l[0:5:2]) # 0 2 4 [111, 'hello', 'b']
print(l[0:len(l)])
print(l[:])
new_l=l[:] # 切片等同于拷贝行为,而且相当于浅copy
print(id(l)) # 140507966157000
print(id(new_l)) #140507966111560
l[-1][0]=1111111
print(l)
print(new_l)
print(l[::-1])
msg1='hello:egon:<>:18[]==123'
msg2=msg1[:]
print(msg1,id(msg1)) # id 139948397731680
print(msg2,id(msg2)) # id 139948397731680
6、长度
print(len([1, 2, 3])) # 3
7、成员运算in和not in
print('aaa' in ['aaa', 1, 2]) #True
print(1 in ['aaa', 1, 2]) #True
8、往列表中添加值
8.1 追加 append()
默认在最后位置追加
l=[111,'egon','hello']
l.append(3333)
l.append(4444)
print(l) #[111, 'egon', 'hello', 3333, 4444]
8.2、插入值 insert()
指定索引,插入
l=[111,'egon','hello']
l.insert(0,'alex')
print(l) # ['alex', 111, 'egon', 'hello']
8.3、extend添加值
#案例1:append添加列表(只能添加完整的列表,而不能将列表中的元素拆开添加)
new_l=[1,2,3]
l=[111,'egon','hello']
l.append(new_l)
print(l) #[111, 'egon', 'hello', [1, 2, 3]]
案例2:通过append将 list2的元素添加到list1中
new_l=[1,2,3]
l=[111,'egon','hello']
for item in new_l:
l.append(item)
print(l) # [111, 'egon', 'hello', 1, 2, 3]
案例3:extend实现上述代码
new_l=[1,2,3]
l=[111,'egon','hello']
l.extend(new_l)
print(l) # [111, 'egon', 'hello', 1, 2, 3]
l.extend('abc')
print(l) #[111, 'egon', 'hello', 1, 2, 3, 'a', 'b', 'c']
案例4:同上
备注:extend() 方法将 list2 的所有元素(4,5,6)添加到了 list1 的末尾,而没有创建新的列表。同时,list2 本身并没有改变。
# 创建两个列表
list1 = [1, 2, 3]
list2 = [4, 5, 6]
# 使用 extend() 方法将 list2 的元素添加到 list1 的末尾
list1.extend(list2)
print(list1) # 输出: [1, 2, 3, 4, 5, 6]
# 注意 list2 并没有被改变
print(list2) # 输出: [4, 5, 6]
9、删除 del()
方式一:通用的删除方法,只是单纯的删除、没有返回值
l = [111, 'egon', 'hello']
del l[1]
x =del l[1] # 抛出异常,不支持赋值语法
print(l)
方式二:l.pop()根据索引删除,会返回删除的值,默认删除最后一个
l = [111, 'egon', 'hello']
l.pop() # 不指定索引默认删除最后一个
l.pop()
print(l)
res=l.pop(1)
print(l)
print(res) #报错,IndexError: pop index out of range
#方式三:l.remove()根据元素删除,返回None
l = [111, 'egon', [1,2,3],'hello']
l.remove([1,2,3])
print(l) #[111, 'egon', 'hello']
l1 = [111, 'egon', 'hello']
res=l1.remove('egon')
print(res) # None
解释:为什么最后print(res)会返回None? 因为remove()没有返回值,返回None,将值赋予给了res,打印后才为None。
不进行赋值,重新打印列表,如:
l1 = [111, 'egon', 'hello']
l1.remove('egon')
print(l1)
#循环
反面案例:下面的这段代码可读性不强,不建议使用下面的方式写代码。请记住,好的代码是python初学者看也能一目了然。
l=[1,'aaa','bbb']
for x in l:
l.pop(1)
print(x)
需要掌握操作
↓↓↓↓↓↓↓↓↓↓↓↓↓
10.1、l.index() :
解释:index() 是列表(list)、元组(tuple)和字符串(str)等序列类型的一个方法,用于查找某个元素在序列中首次出现的索引位置(即下标)。如果元素不存在于序列中,index() 方法将抛出一个 ValueError 异常。
10.1.1 index查找列表List中的某个元素
my_list = [1, 2, 3, 4, 5]
index = my_list.index(3) # 查找元素 3 的索引
print(index) # 输出: 2
# 如果元素不存在,会抛出 ValueError 异常
try:
index = my_list.index(6)
print(index)
except ValueError:
print("元素 6 不在列表中")
10.1.2 index查找元组(Tuple)中的某个元素
my_tuple = (1, 2, 3, 4, 5)
index = my_tuple.index(4) # 查找元素 4 的索引
print(index) # 输出: 3
10.1.3 index查找字符串(String)中的某个元素
my_string = "hello"
index = my_string.index('l') # 查找字符 'l' 的索引(注意,这里默认查找第一个 'l')
print(index) # 输出: 2
# 查找特定位置的字符
index = my_string.index('l', 2) # 从索引 2 开始查找字符 'l'
print(index) # 输出: 2(因为从索引 2 开始,第一个 'l' 就在索引 2)
index = my_string.index('l', 3) # 从索引 3 开始查找字符 'l'
print(index) # 输出: 3(从索引 3 开始,下一个 'l' 就在索引 3)
10.2、l.count()
解释1:count() 方法用于计算序列中某个元素出现的次数,而不是查找元素的索引位置;
解释2:count() 方法同样被列表(list)、元组(tuple)和字符串(str)等序列类型所支持
#案例1:列表(List)
my_list = [1, 2, 2, 3, 4, 2]
count = my_list.count(2) # 计算元素 2 出现的次数
print(count) # 输出: 3
#案例2:元组(Tuple)
my_tuple = (1, 2, 2, 3, 4, 2)
count = my_tuple.count(2) # 计算元素 2 出现的次数
print(count) # 输出: 3
#案例3:字符串(String)
my_string = "hello"
count = my_string.count('l') # 计算字符 'l' 出现的次数
print(count) # 输出: 2
10.3、l.clear()
解释:lear() 是一个常用于列表(list)、集合(set)和字典(dict)等集合类型的方法,用于移除集合中的所有元素。
#案例1:列表(List)
l = [1, 'aaa', 'bbb','aaa','aaa']
l.clear()
print(l) #结果:[]
#案例2:集合(Set),移除集合中的所有元素
my_set = {1, 2, 3, 4, 5}
my_set.clear()
print(my_set) # 输出: set()
#案例3:字典(Dict),移除字典中的所有键值对
my_dict = {'a': 1, 'b': 2, 'c': 3}
my_dict.clear()
print(my_dict) # 输出: {}
10.5、l.reverse()
解释:不是排序,是将列表倒过来
l = [1, 'egon','alex','lxx']
l.reverse()
print(l)
10.6、l.sort()
解释:列表内元素必须是同种类型才可以排序
#案例1:
l=[11,-3,9,2,3.1]
l.sort() # 默认从小到大排,称之为升序
print(l) #[-3, 2, 3.1, 9, 11]
l.sort(reverse=True) # 从大到小排,设置为降序
print(l) #[11, 9, 3.1, 2, -3]
案例2:不用数据类型的列表元素无法进行排序,无法直接进行比较
#整数和字符串之间是无法直接比较的,因为它们属于不同的数据类型。
#当你尝试对包含整数和字符串的列表进行排序时,Python会抛出一个 TypeError,告诉你这些元素之间无法进行排序。
l=[11,'a',12]
l.sort() #结果:抛出异常
案例3:字符串类型的元素间进行比较
l=['c','e','a']
l.sort()
print(l) # ['a', 'c', 'e']
了解①:
字符串可以比大小,按照对应的位置的字符依次pk
字符串的大小是按照ASCI码表的先后顺序加以区别,表中排在后面的字符大于前面的
print('a'>'b')
print('abz'>'abcdefg')
了解②:
列表也可以比大小,原理同字符串一样,但是对应位置的元素必须是同种类型
l1=[1,'abc','zaa']
l2=[1,'abc','zb']
print(l1 < l2) #True
补充
1、队列:FIFO,先进先出
入队操作
l=[]
l.append('first')
l.append('second')
l.append('third')
print(l) # ['first', 'second', 'third']
出队操作
print(l.pop(0)) # first
print(l.pop(0)) # second
print(l.pop(0)) # third
2、堆栈:LIFO,后进先出
入栈操作
l=[]
l.append('first')
l.append('second')
l.append('third')
print(l)
出队操作
print(l.pop()) # third
print(l.pop()) # second
print(l.pop()) # first