5、Python 列表

本文介绍了Python列表的特点,如可变性、有序性,并详细阐述了如何创建、操作及修改列表,包括列表的切片、常用方法如append、extend、insert等,以及列表的排序和查找功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、列表的特点

  • 列表是一种容器,是序列的一种,可以被改变。
  • 列表是一系列特定元素组成的,元素之间可以没有任何的关联关系,但他们之间有先后顺序关系。。
  • 列表时有序的,可以按照索引检索,

2、列表的创建

  • 使用大括号 []
#1、创建一个空列表
list1 = []

#2、创建非空
list1 = [1,2,3,4,5]
  • 使用 list(iterable) 来创建
# list(iterable)
list1 = list("ABCD")
print(list1)
运行结果:['A', 'B', 'C', 'D']
  • 列表推导式  [表达式  for 变量 in 可迭代对象 [if 真值表达式]]
#1、简单的创建
list1 = [i for i in range(1,10) if i%2 == 0]
print(list1)
运行结果:[2,4,6,8]

#2、嵌套
list1 = [i * y for i in range(1,4) for y in range(1,4)]
print(list1)
运行结果:[1, 2, 3, 2, 4, 6, 3, 6, 9]

3、列表操作符与切片

  • list1 = [1,2,3]  list2 = [4,5,6]
操作符描述实例
+用于拼接列表list1+list2:   [1, 2, 3, 4, 5, 6]
+=列表拼接list2 += list1: [4, 5, 6, 1, 2, 3]
*生成重复列表list1 * 2:[1, 2, 3, 1, 2, 3]
*=生成重复列表list1 *= 3: [1, 2, 3, 1, 2, 3, 1, 2, 3]

== != >

< >= <=

列表比较运算list1 == list2 : False

 

  • 列表的切片 
  • 语法:列表[begin:end]  列表[开始:结束:步长]
  • 作用:可以改变原列表的排序,可以插入数据和修改数据。
  • 对于切片步长不等1的切片赋值,赋值运算符的右侧的可迭代对象提供的数据元素的个数一定要等与切片切除的段数。
  • 反转:L[::-1]
list1 = [1,2,3,4,5]
print(list1[0:3])
print(list1[:2])
print(list1[:])
print(list[0:5:2]
运行结果:
[1, 2, 3]
[1, 2]
[1, 2, 3, 4, 5]
[1,3,5]
list1 = [1,2,3,4]
list1[0:3]=[0,9,8]
print(list1)

运行结果:
[0, 9, 8, 4]

L = [1,2,3,4,5,6]
L[::2] = 'ABC' #正确
print(L) 
运行结果:
['A', 2, 'B', 4, 'C', 6]

4、列表的方法

  • 增加   list.append(obj) 列表末尾添加新的对象,无返回值,修改原列表。
  •           list.extend(seq) 在列表末尾一次性追加另一个序列中的多个值。seq 元素列表,可以是列表、元组、集合、字典,若为字典,则仅会将键(key)作为元素依次添加至原列表的末尾。
  •           list.insert(index, obj)  插入位置,插入值。无返回值,插入单个。
  •           list[:] = [] 利用切片进行增加
#1、list.append()
list1 = ['Google', 'Runoob', 'Taobao']
list1.append('Baidu')
print ( list1)
运行结果: ['Google', 'Runoob', 'Taobao', 'Baidu']

#2、list.extend()
list1 = ['Google', 'Runoob', 'Taobao']
list2=list(range(5)) # 创建 0-4 的列表
list1.extend(list2)  # 扩展列表
print (list1)
运行结果:['Google', 'Runoob', 'Taobao', 0, 1, 2, 3, 4]

list1 = [1,2,3]
dict1 = {"age":8,"sex":"boy"}
list1.extend(dict1)
print(list1)
运行结果:[1, 2, 3, 'age', 'sex']

#3、list.insert(index,value)
list1 = ['Google', 'Runoob', 'Taobao']
list1.insert(1, 'Baidu')
print (list1)
运行结果:['Google', 'Baidu', 'Runoob', 'Taobao']
  • 删除    list.pop([index=-1])  移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。
  •            list.remove(obj)  删除列表中某个值的第一个匹配项,如果没有会报错。
  •            list.clear()   用于清空列表,类似于 del a[:]
  •            del list[index] 也可以删除切片
#1、list.pop([index])
list1 = ['Google', 'Runoob', 'Taobao']
list1.pop() #默认移除最后一个
print (list1)
list1.pop(1)
print (list1)
运行结果:
['Google', 'Runoob']
['Google']

#2、list.remove()
list1 = ['Google', 'Runoob', 'Taobao']
list1.remove('Google')
print(list1)
运行结果:['Runoob','Taobao']

#3、list.clear()
list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']
list1.clear()
print (list1)
运行结果:[]

#4、del list[]
list1 = ['Google', 'Runoob', 'Taobao']
del list1[1]
print(list1)
运行结果:['Google', 'Taobao']


list1 = ['Google', 'Runoob', 'Taobao']
del list1[0:2]
print(list1)
运行结果:['Taobao']
  • 更新    list[index] = value  更改指定索引的值
list1 = ['Google', 'Runoob', 'Taobao']
list1[0] = "Baidu"
print(list1)
运行结果:['Baidu', 'Runoob', 'Taobao']
  • 查找    list[]  直接利用索引查看值。
  •            list.count(obj)  统计某个元素在列表中出现的次数,返回出现的次数。
  •            list.index(x[, start[, end]]) 从列表中找出某个值第一个匹配项的索引位置,返回索引位置。没有会报错ValueError。
  •            value in (not in ) list  查看一个值在不在列表中。
#1、list[index]
list1 = ['Google', 'Runoob', 'Taobao']
print(list1[1])
运行结果:Runoob

#2、list.count()
list1 = [1,1,1,'Google', 'Runoob', 'Taobao']
num = list1.count(1)
print(num)
运行结果:3

#3、list1.index()
list1 = ['Google', 'Runoob', 'Taobao']
print (list1.index('Runoob'))
print (list1.index('Taobao'))
运行结果: 1  2
  •  排序         list.sort( key=None, reverse=False)用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。
  •                               key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定                                  可迭代对象中的一个元素来进行排序。
  •                               reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)。
  •                 list.reverse() 用于反向列表中元素。
#1、list1.sort()
list1 = [3,5,9,1,2,4]
list1.sort()
print(list1)
运行结果:[1, 2, 3, 4, 5, 9]

#2、reverse()
list1 = [3,5,9,1,2,4]
list1.reverse()
print(list1)
运行结果:[4, 2, 1, 9, 5, 3]
  • 内置函数     向列表len(list)  max(list) min(list)  sum(list) any(list) all(list)真值测试  list(seq)元组转列表
list1 = [3,5,9,1,2,4,0]
print(len(list1))
print(max(list1))
print(min(list1))
print(sum(list1))
print(any(list1))
print(all(list1))
运行结果:
7
9
0
24
True
False

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值