【笔记-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]
相关推荐
小兔崽子去哪了1 天前
XGBoost,朴素贝叶斯,特征降维,聚类算法
python·机器学习
隔壁大炮1 天前
08. PyTorch_张量基本创建方式
人工智能·pytorch·python
遨游xyz1 天前
BM算法(Boyer-Moore)
开发语言·python
期待のcode1 天前
docker将镜像推送到阿里云镜像仓库与私有镜像仓库
阿里云·docker·容器
vm321 天前
02:Agent Loop 深度剖析:ReAct 循环的工程实现
人工智能·python
List<String> error_P1 天前
经典回溯算法解析
python·算法
清水白石0081 天前
依赖注入的优雅:不用框架,在 Python 中实现轻量级依赖注入
开发语言·python
AC赳赳老秦1 天前
多模态 AI 驱动办公智能化变革:DeepSeek 赋能图文转写与视频摘要的高效实践
java·ide·人工智能·python·prometheus·ai-native·deepseek
weixin_440401691 天前
Python数据分析-合并清洗与转换(concat+lambda函数+apply+删除drop/替换数据replace)
开发语言·python·数据分析
Dxy12393102161 天前
Python如果遇见乱码可以通过二进制判断是什么编码吗?
开发语言·python