惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
V
V2EX
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
博客园 - 司徒正美
Microsoft Security Blog
Microsoft Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
Vercel News
Vercel News
The Register - Security
The Register - Security
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
N
Netflix TechBlog - Medium
WordPress大学
WordPress大学
小众软件
小众软件
L
Lohrmann on Cybersecurity
GbyAI
GbyAI
P
Privacy & Cybersecurity Law Blog
T
Tor Project blog
AWS News Blog
AWS News Blog
美团技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
K
Kaspersky official blog
B
Blog RSS Feed
G
Google Developers Blog
量子位
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
Scott Helme
Scott Helme
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
I
Intezer
雷峰网
雷峰网
Martin Fowler
Martin Fowler
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
IT之家
IT之家
F
Full Disclosure
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】
The Hacker News
The Hacker News
U
Unit 42
S
SegmentFault 最新的问题
I
InfoQ
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
罗磊的独立博客
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes

博客园 - YiYezc

Python的if语句 Python元组 Python列表管理 Python列表元素管理 Python字符串 Python变量 centos安装autossh bash相关 Linux的bash快捷键 Vim程序编辑器 Linux下光盘镜像生成和刻录 Linux文件系统备份dump Linux下文件压缩与打包 Linux挂载 Linux磁盘分区 Linux文件访问和日志 block、inode、superblock详解 Linux文件系统的详解 Linux的权限对于文件与目录的意义 命令与文件的查询 ctime, atime与mtime释疑 Linux下文件特殊权限 umask相关内容
Python列表操作
YiYezc · 2026-04-23 · via 博客园 - YiYezc

1.列表遍历

nums = ['one','two','three','four','five']

for num in nums:

    print(num)

执行后得到:

one
two
three
four
five

2.for循环注意点

①不要忘记:

①:下一行的循环执行内容要进行缩进四个空格

②循环结束后,空一行,不缩进

2.创建数值列表

使用range( )函数

①range(stop)  从0开始,到stop-1结束

print(list(range(5))) 

执行得到:[0, 1, 2, 3, 4]
print(list(range(3)))

执行得到:[0, 1, 2]

②range(start, stop)  从start开始,到stop-1结束

print(list(range(2, 6))) 

执行得到: [2, 3, 4, 5]
print(list(range(-2, 3))) 

执行得到: [-2, -1, 0, 1, 2]

③range(start, stop, step)  指定步长

print(list(range(0, 10, 2)))

执行得到:[0, 2, 4, 6, 8] (步长为2)
print(list(range(10, 0, -1)))

执行得到: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] (倒序)

3.列表推导式

nums = [num**2 for num in range(1,11)]

print(nums)

执行得到:[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

3.切片

列表[起始索引:结束索引:步长]

得到的切片包含起始索引元素,不包含结束索引元素

列表切片主要用来提取、操作和复制列表(或其他序列)的一部分,切片是原列表的浅拷贝,对切片的处理不会影响到原列表

nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

①用切片获取列表部分元素

# 获取第2到第4个元素(索引1到3)
print(nums[1:4])

执行得到:[1, 2, 3]

# 从开始到索引4(不包含4)
print(nums[:4])

执行得到:[0, 1, 2, 3]

# 从索引5到结束
print(nums[5:])

执行得到:[5, 6, 7, 8, 9]

#得到列表最后三个元素

print(nums[-3:0])

执行得到:[7, 8, 9]

②编列切片

nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

for num in nums[:4]:

    print(num)

执行得到:

0
1
2
3

③复制列表

nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

nums_copy=nums[ : ]

print(nums)

print(nums_copy)

nums_copy.append(10)

print(nums)

print(nums_copy)

执行后得到:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

如果列表包含嵌套对象,对使用切片复制,对复制列表操作仍然会影响到原列表,需要使用深度拷贝

列表复制能不能使用nums_copy=nums这种方式,因为这样只是复制了引用,并没有创建新的列表