【笔记-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]
相关推荐
Kt&Rs2 小时前
从问题驱动技术的角度对k8s技术的思维层面理解
云原生·容器·kubernetes
明知道的博客4 小时前
解决WSL环境下DeepSeek-OCR运行时内存不足问题
python·ocr·deepseek·deepseek-ocr
gzr_csdn4 小时前
【报错解决】VMware 嵌套虚拟化问题
linux·容器
FreeCode5 小时前
LangGraph1.0智能体开发:Graph API概念与设计
python·langchain·agent
袖手蹲5 小时前
树莓派 5 Trixie 镜像 + Docker 架设 Eclipse Mosquitto 与 ESP32 双向 MQTT 交互
运维·docker·容器
test管家5 小时前
如何在Python中使用SQLite数据库进行增删改查操作?
python
人工智能训练6 小时前
在Windows系统Docker中使用wsl2、容器、windows文件路径三种不同挂载方式的区别和性能差异
运维·服务器·人工智能·windows·docker·容器·wsl2
yangmf20407 小时前
APM(三):监控 Python 服务链
大数据·运维·开发语言·python·elk·elasticsearch·搜索引擎
yangmf20407 小时前
APM(二):监控 Python 服务
大数据·python·elasticsearch·搜索引擎
CoderJia程序员甲7 小时前
GitHub 热榜项目 - 日榜(2025-11-23)
python·开源·github·mcp