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

推荐订阅源

L
LangChain Blog
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
博客园 - 司徒正美
罗磊的独立博客
D
Docker
Last Week in AI
Last Week in AI
爱范儿
爱范儿
M
MIT News - Artificial intelligence
V
V2EX
Google DeepMind News
Google DeepMind News
小众软件
小众软件
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Security Blog
Microsoft Security Blog
T
Tailwind CSS Blog
MyScale Blog
MyScale Blog
V
Visual Studio Blog
博客园 - 叶小钗
B
Blog RSS Feed
A
About on SuperTechFans
F
Fortinet All Blogs
T
The Blog of Author Tim Ferriss
Martin Fowler
Martin Fowler
P
Proofpoint News Feed

博客园 - 漫思

python使用.env构建开发和生产环境 python项目的构建 nodejs构建CICD时的思考  成为 AI 智能体工程师的 10 个步骤 es6的 yield python 中的 yield 笔记本的A壳 thinkpad 更换 reduce() in Python python的字段赋值和取值的操作 python的语法类似于lodash分组展开,合并分组操作 SelectMany C# lodash 数组的常用做法 lodash里面的常用方法 技术的边界 Reduce 和 Transduce 的含义 尤雨溪创办的 VoidZero 官宣加入 Cloudflare,前端 Vite 等保持开源 Ramda 函数库参考教程 ramda es 数组的方法 flatmap map object.entity的教程 Map与FlatMap:在数据处理中的区别与联系 flat、flatmap与map的用法区别1 flat、flatmap与map的用法区别 AI不是从天而降,它经历了七十年三起三落:读懂AI的第三课 Agent 17 种架构模式 分析 & 思考 只有踩过坑才懂:前端生成唯一 ID,别用 Date.now ()了!试试它crypto.randomUUID() FastAPI python并发 代码是 AI 写的,生产事故谁背锅? AI Agent 走出 Demo 幻觉的唯一解药:Harness Engineering
Python 里通常说“数组切片”,大多数时候指的是列表切片。基本...
漫思 · 2026-04-25 · via 博客园 - 漫思

列表[开始:结束:步长]

注意:包含开始位置,不包含结束位置。

nums = [10, 20, 30, 40, 50]

print(nums[1:4])   # [20, 30, 40]

常见写法:

nums = [10, 20, 30, 40, 50]

nums[0:3]    # [10, 20, 30]
nums[:3]     # [10, 20, 30],从开头到索引 3 之前
nums[2:]     # [30, 40, 50],从索引 2 到结尾
nums[:]      # [10, 20, 30, 40, 50],复制整个列表
nums[-1]     # 50,最后一个元素
nums[-3:]    # [30, 40, 50],最后 3 个元素

带步长:

nums = [10, 20, 30, 40, 50, 60]

nums[::2]    # [10, 30, 50],每隔 2 个取一个
nums[1::2]   # [20, 40, 60],从索引 1 开始每隔 2 个取一个

反转列表:

nums = [10, 20, 30, 40, 50]

nums[::-1]   # [50, 40, 30, 20, 10]

切片索引图可以这样理解:

nums = [10, 20, 30, 40, 50]
#       0   1   2   3   4
#      -5  -4  -3  -2  -1

所以:

nums[1:4]

image