Error in user YAML: (<unknown>): found a tab character that violate indentation while scanning a plain scalar at line 3 column 3
---
- oeasy Python 0106
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
- 本教程同步发布在:
- 个人网站: `https://oeasy.org`
- 蓝桥云课: `https://www.lanqiao.cn/courses/3584`
- GitHub: `https://github.com/overmind1980/oeasy-python-tutorial`
- Gitee: `https://gitee.com/overmind1980/oeasypython`
---- 配套视频
- 上次研究了
回环文- 绕圈读
- 文字
重叠反复
-
这些都是由 列表构成的
- 列表 还能怎么玩呢?🤔
-
可以根据切片
- 删除列表项 吗?🤔
python
- 进入游乐场
a
a = 1
a
del a
a- 删了变量 就 找不到了
- 删除del
- 删除的是
- 变量名字
- 和具体存储空间之间的绑定关系
- 删除的是
- 可以更明确地演示吗?
a = 1
print(a)
del a
print("hello")
- 可以一步步观察程序执行的过程
-
执行过程
- 对a声明并赋值之后
- Global Frames
- 全局程序栈的
帧(frame)上 - 就有了 变量a
- 具体值 为 1
- del a之后
- 全局程序栈的
帧(frame)上 - 变量a就消失了
- 全局程序栈的
- 对a声明并赋值之后
-
列表中 的
列表项可以删 吗?
nlist = list(range(5))
nlist
nlist[2]
del nlist[2]
nlist
- 效果
- del后的列表 还在
原来的内存地址 吗?
- 使用id函数 观察地址
id(nlist)
del nlist[2]
id(nlist)
- nlist 还是
原来的 地址- 但 里面的列表项
- 被删(del)了
- list类 的 remove方法
- 也能 删除
- 两者有什么区别吗?
- 建立字符列表clist
clist = list("oeasy")
clist
clist.remove("y")
clist
del clist[2]
clist
- 两者都可以删除
| remove | del | |
|---|---|---|
| 删除依据 | 具体列表项 | 索引序号 |
| 类型 | 列表类方法 | 关键字 |
| 删除范围 | 某个列表项 | 切片范围 |
- 怎么删除
切片呢?
- 建立数字列表nlist
nlist = list(range(5))
nlist
nlist[1:3]
del nlist[1:3]
nlist
- 删除其中的切片
- nlist[1:3]
- 就像 切鱼一样
- 把 中段切掉
- 首尾 对接
- 吃鱼看性格
- 第一筷子 选
- 月牙肉
- 鱼唇
- 第一筷子 选
- 这些部分 最好吃
- 显示出 从小受宠
- 可能 自我中心
- 我们来拾掇拾掇鱼
- 一条鱼切成三段
fish = ["head", "body", "tail"]
- 掐头
- 去尾
- 删除支持步长么?
nlist = list(range(10))
nlist[3:6]
nlist[3:]
nlist[:6]
nlist[::1]
nlist[::2]
nlist[::3]
nlist[1::3]
nlist[2::3]
nlist[3::3]
nlist[3:9:3]
- 切片效果
nlist = list(range(10))
nlist
nlist[2::2]
del nlist[2::2]
nlist
- 效果
- del支持 步长
- 在此基础上 再删除
nlist
nlist[::5]
del nlist[::5]
nlist
- 结果
- 字符串可以通过del删除切片吗?
s = "oeasy"
s[1:3]
del s[1:3]
- 字符串 支持切片
- 但 不支持切片删除
- 怎么办呢?
- 字符串 不支持 del关键字
- 但是
列表支持 - 先把 字符串
转化为列表
- 但是
s = "oeasy"
clist = list(s)
del clist[1:3]
clist
s = ""
for c in clist:
s = s + c
print(s)
- 最后 再把 列表
转化回字符串- 直着 过不去
- 绕过去
- 这次研究的是del 删除
- 可以 删除列表项
- 也可以 删除切片
- 就像择菜一样
- 择出去的菜 搁
哪儿了呢?🤔 - 下次再说 👋
- 配套视频
- 本文来自 oeasy Python 系统教程。
- 想完整、扎实学 Python,
- 搜索 oeasy 即可。


















