【笔记-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]
相关推荐
薛定谔的猫-菜鸟程序员3 小时前
2小时智能体开发一个智能体?我用CodeArts Agent 和 AtomCode 开发了一个适老化智能体。
人工智能·python·agent
半旧夜夏3 小时前
【保姆级】微服务组件环境搭建(Docker Compose版)
java·linux·spring cloud·微服务·云原生·容器
bigfootyazi3 小时前
python爬虫-基本库-urllib库(常用速查)
开发语言·爬虫·python
瑶总迷弟4 小时前
使用 mis-tei 在昇腾310P上部署 bge-m3模型
pytorch·python·华为·语言模型·自然语言处理·cnn·unix
belong_my_offer4 小时前
认识到精通函数
开发语言·python
卡次卡次15 小时前
vibecoding起步注意点:插件、Skills、MCP、Hooks
服务器·数据库·python·oracle
我的xiaodoujiao5 小时前
API 接口自动化测试详细图文教程学习系列24--如何用Pytest去设计接口测试用例并执行
python·学习·测试工具·pytest
宇明一不急6 小时前
k8s HPA storageclass configmap
云原生·容器·kubernetes
zhangfeng11336 小时前
ai 模型加密,强化版终极防盗方案 支持烧录的显卡列表
人工智能·pytorch·python
半个落月6 小时前
深入理解 Python dict 与 set:从哈希表底层到高性能实战
python