【笔记-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]
相关推荐
曾阿伦2 分钟前
Python 时间格式化指南
python
The_Ticker3 分钟前
日股实时行情接口使用指南
java·经验分享·笔记·python·算法·区块链
m0_560396478 分钟前
用Python创建一个Discord聊天机器人
jvm·数据库·python
m0_569881479 分钟前
使用Flask快速搭建轻量级Web应用
jvm·数据库·python
2401_873204659 分钟前
用Pandas处理时间序列数据(Time Series)
jvm·数据库·python
2301_7765087210 分钟前
定时任务专家:Python Schedule库使用指南
jvm·数据库·python
泡沫·13 分钟前
docker的基本认识
运维·docker·容器
2301_7638919515 分钟前
使用Python控制Arduino或树莓派
jvm·数据库·python
2401_8747325322 分钟前
实战:用Python分析某电商销售数据
jvm·数据库·python
全栈凯哥22 分钟前
26.Python os.path 完全指南
python