【笔记-Python】内置容器-list

list为python内置容器,其特点:可变、成员有序;

常用操作

创建list
复制代码
# 使用字面量创建list
numbers = [0,1,2,3]

# 使用内置函数list(iterable)将可迭代对象转换为列表
characters = list('hello world!')
遍历list
复制代码
characters = list('hello world!')
# 仅遍历元素
for character in characters:
    print(character)

# enumerate()适用于任何"可迭代对象",接收一个可选参数start用于指定循环下标初始值(默认为0)
# 遍历过程中获取下标
for index,character in enumerate(characters):
    print(index, character)

# 从指定下标开始遍历
for index,character in enumerate(characters, start=1):
    print(index, character)
删除成员
复制代码
characters = list('hello world!')
# 删除list第1个成员
del characters[1]

# 删除list一段成员
del characters[1:]
list推导式
复制代码
numbers = [1,2,3,4,5,6]
# 遍历numbers列表: for number in numbers
# 进行成员过滤: if number%2==1
# 对成员进行操作: number+10
# 构建新的list
results = [number+10 for number in numbers if number%2==1]
相关推荐
u***324331 分钟前
使用python进行PostgreSQL 数据库连接
数据库·python·postgresql
青瓷程序设计3 小时前
动物识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
tobebetter95273 小时前
How to manage python versions on windows
开发语言·windows·python
F_D_Z3 小时前
数据集相关类代码回顾理解 | sns.distplot\%matplotlib inline\sns.scatterplot
python·深度学习·matplotlib
daidaidaiyu4 小时前
一文入门 LangGraph 开发
python·ai
不知更鸟5 小时前
前端报错:快速解决Django接口404问题
前端·python·django
4***72135 小时前
【玩转全栈】----Django模板语法、请求与响应
数据库·python·django
梁正雄5 小时前
1、python基础语法
开发语言·python
ituff6 小时前
微软认证考试又免费了
后端·python·flask
梁正雄7 小时前
2、Python流程控制
开发语言·python