数据结构【2】:列表专题

数据结构【2】:列表专题

一、列表定义

在Python中,列表是一种有序、可变、允许重复元素 的数据结构。它是由一组元素组成的,这些元素可以是不同数据类型的对象 ,包括数字、字符串、布尔值、其他列表,甚至是自定义对象。列表是用方括号\[\]括起来的,元素之间用逗号分隔。

二、列表的特点

  1. 有序性 :列表中的元素按照它们被加入的顺序排列,每个元素在列表中都有一个固定的位置索引。
  2. 可变性:列表是可变的,可以动态地增加、删除和修改列表中的元素。
  3. 元素类型 :列表可以包含任意类型的元素,包括数字、字符串、对象等。
  4. 长度 :列表的长度是指其中包含的元素个数,可以通过内置方法获取列表的长度。

三、列表的基本操作

1、append(x):在列表末尾添加一个新元素 x。

python 复制代码
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)  # 输出: [1, 2, 3, 4]

2、insert(i, x):在索引 i 处插入一个新元素 x。

python 复制代码
my_list = [1, 2, 3]
my_list.insert(1, 5)
print(my_list)  # 输出: [1, 5, 2, 3]

3、len(list) 方法返回列表元素个数。

python 复制代码
#!/usr/bin/python3

list1 = ['Google', 'W3CSchool', 'Taobao']
print (len(list1))	# 输出:3
list2=list(range(5)) # 创建一个 0-4 的列表
print (len(list2))	# 输出:5

4、extend(iterable):将 可迭代对象 中的元素追加到列表的末尾

python 复制代码
my_list = [1, 2, 3]
other_list = [4, 5, 6]
my_list.extend(other_list)
print(my_list)  # 输出: [1, 2, 3, 4, 5, 6]

5、remove(x):移除列表中第一个出现的值为 x 的元素

python 复制代码
my_list = [1, 2, 3, 2]
my_list.remove(2)
print(my_list)  # 输出: [1, 3, 2]

6、pop(i):移除 并返回列表中指定位置的 元素,默认为末尾元素。

python 复制代码
my_list = [1, 2, 3]
x = my_list.pop(1)
print(x)  # 输出: 2
print(my_list)  # 输出: [1, 3]

7、index(x, start, end):返回 第一个值为 x 的元素的索引,可指定搜索的起始和结束位置。

python 复制代码
my_list = [1, 2, 3, 2]
index = my_list.index(2)
print(index)  # 输出: 1

8、count(x):返回值为 x 的元素在列表中出现的次数。

python 复制代码
my_list = [1, 2, 3, 2, 2]
count = my_list.count(2)
print(count)  # 输出: 3

9、max(list):返回列表元素中的最大值。

python 复制代码
my_list = [1, 2, 3, 2, 2]
count = max(my_list)
print(count)  # 输出: 3

10、min(list):返回列表元素中的最小值。

python 复制代码
my_list = [1, 2, 3, 2, 2]
count = min(my_list)
print(count)  # 输出: 1

11、list( seq ):用于将序列(元组,集合,字符串等)转换为列表。
注:元组与列表是非常类似的,区别在于元组的元素值不能修改,元组是放在括号中 ,列表是放于方括号中 。集合和列表也是很相似的,区别在于集合不能有重复值 。只有与列表结构相似的序列才能转换为列表,字典只能分别将他们的键和值转换成列表。

python 复制代码
#!/usr/bin/python3

aTuple = (123, 'Google', 'W3CSchool', 'Taobao')
list1 = list(aTuple)
print ("列表元素 : ", list1)

str="Hello World"
list2=list(str)
print ("列表元素 : ", list2)
===结果===
列表元素 :  [123, 'Google', 'W3CSchool', 'Taobao']
列表元素 :  ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']

12、list.reverse():函数用于反向列表中元素。

python 复制代码
#!/usr/bin/python3

list1 = ['Google', 'W3CSchool', 'Taobao', 'Baidu']
list1.reverse()
print ("列表反转后: ", list1)	# 列表反转后:  ['Baidu', 'Taobao', 'W3CSchool', 'Google']

参考链接:

python 复制代码
https://www.w3cschool.cn/python3/python3-att-list-sort.html
https://blog.csdn.net/bb8886/article/details/129529427
相关推荐
默_笙5 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
小羊没烦恼!5 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
rockmelodies5 天前
# Windows Server2008 R2 Standard(SP1镜像U盘)重置本地管理员密码【完整详细步骤】
windows·密码破解
qq_426003965 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫5 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技5 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读5 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时5 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
m0_547486665 天前
《数据结构教程》全套 PPT课件2026
数据结构
奇思妙想聪明勤奋的小羊5 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型