Error in user YAML: (<unknown>): could not find expected ':' while scanning a simple key at line 3 column 1
---
- oeasy Python 0096
- 这是 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文件运行时的系统参数
- sys.argv
- 通过sys.argv就可以接收从命令行来的参数了
- 可以通过索引来获得第n个参数
- 这就是索引(index)的作用
- 处理了可能出现的
- IndexError
- ValueError
- 列表(list)还有什么方法呢?🤔
clist = list("oeasyo2zo3z")
clist
- 总共有3个
'o'
clist.index("o")
- 我们可以通过index方法
- 得到列表中
- 第1个"o"的位置
- 那如何 才能 得到
- 第2个、第3个"o"的位置呢?
first = clist.index("o")
first
second = clist.index("o",first + 1)
second
third = clist.index("o",second + 1)
third
- 逐个往后
| 序号 | 位置 |
|---|---|
| 第0个 | 下标0 |
| 第1个 | 下标5 |
| 第2个 | 下标8 |
- 还能继续找吗?
fourth = clist.index("o", third + 1)
- 如果此时
- 再从9开始
- 去查找"o"的索引
- 就找不到了
- 总共有3个"o"
- 有什么更快的方法吗?
- 先统计一下有多少个'o'
clist = list("oeasyo2zo3z")
clist.count("o")
- 总共3个
- 这个count是什么意思呢?
help(list.count)
- 统计参数出现的次数
- count 是 计数函数
- len 也是
- 有什么区别吗?
len(clist)
clist.count("o")
- 结果
| len | count |
|---|---|
| 容器总长度 | 指定列表项的 数量 |
| builtins的内置函数 | 列表类对象的 方法 |
| 列表 作为 参数 | 列表对象 作为 主调对象 |
- append 对于 count结果有影响吗?
clist = list("oeasy")
clist.count("o")
clist.append("o")
clist.count("o")- append之后
- count计数结果会变化
- remove呢?
clist = list("oeasy")
clist.count("o")
clist.remove("o")
clist.count("o")- 删除 对 计数
- 也会有影响
-
问题是remove
- 每次都 从开始位置 删
- 先删 第1个"o"
-
我想让他
- 先删除最后一个"o"
- 怎么办?
clist = list("oeasyo2z")
count = clist.count("o")
pos = 0
for num in range(count):
pos = clist.index("o", pos)
pos = pos + 1
print(pos)- 先找到最后o的索引序号
- 位置找到了
- 第6个 列表项
- 其实 索引号 应该 是 5
clist = list("oeasyo2z")
count = clist.count("o")
i = 0
for num in range(count):
i = clist.index("o", i)
i = i + 1
i = i - 1
print(i)- 这样可以得到最后一个o的索引值
- 找到了 之后
- 怎么
删除呢?
- 怎么
- 我要删除第5个列表项
- remove方法没有start参数
- 怎么办??
- 找到了这个元素下标为6
- 就先替换了
- 然后再删除
clist[5] = "sth special!"
clist
clist.remove("sth special!")
clist
- 确实删除成功
- 还有更快的办法吗?
clist = list("oeasyo2z")
del clist[5]
print(clist)
- 确实可以👍
- 回忆del
- del 是
- 关键字
- keyword
a
a = 0
a
del a
a
- 以前是删除 声明过的变量
- 现在是 删除 列表中 被索引的列表项
del clist[5]
- 除了 列表类 有 count方法之外
- 字符串也有count方法吗?
s = "oeasyo2z"
s.count("o")
s.count("easy")
- 确实也有
help(str.count)
- 帮助手册和列表的差不多
板凳宽,扁担长,板凳比扁担宽,扁担比板凳长,扁担要绑在板凳上,板凳不让扁担绑在板凳上,扁担偏要板凳让扁担绑在板凳上。
- 问
- 扁担 出现次数多?
- 还是 板凳 出现次数多?
- 我们去总结一下
-
这次研究了 计数函数count
- 统计 列表中 某个列表项 出现次数
- 统计 某字符串 在 字符串中 出现的次数
-
count与len不同
- count统计 列表项出现次数
- len统计 列表的长度
- count 这个词怎么来的呢?🤔
- 下次再说 👋
- 配套视频
- 本文来自 oeasy Python 系统教程。
- 想完整、扎实学 Python,
- 搜索 oeasy 即可。






















