目录
[Ⅸ.python 的异常输出](#Ⅸ.python 的异常输出)
[Ⅹ.for-else 和while-else](#Ⅹ.for-else 和while-else)
Ⅰ.os操作
Ⅱ.时间库(很重要)





Ⅲ.基本单位换算(ms,min,h的单位换算)
基本单位换算
1 秒 = 1000 毫秒
1 分钟 = 60 秒
1 小时 = 60 分钟 = 3600 秒
转换步骤
将毫秒转换为秒:
毫秒数除以 1000,得到总秒数。
例如,46800999 毫秒 = 46800999 // 1000 = 46800 秒。
计算小时:
总秒数除以 3600(1 小时 = 3600 秒),得到小时数。
例如,46800 // 3600 = 13 小时。
计算剩余的分钟:
总秒数对 3600 取模,得到剩余的秒数。
剩余的秒数除以 60(1 分钟 = 60 秒),得到分钟数。
例如,46800 % 3600 = 0 秒,0 // 60 = 0 分钟。
计算剩余的秒:
剩余的秒数对 60 取模,得到秒数。
例如,0 % 60 = 0 秒。
代码中的转换逻辑
将毫秒转换为秒
total_seconds = milliseconds // 1000
#milliseconds // 1000:将毫秒数除以 1000,得到总秒数。
PYTHON计算小时、分钟和秒
hours = (total_seconds // 3600) % 24
minutes = (total_seconds % 3600) // 60
seconds = total_seconds % 60 当然也可以seconds = (total_seconds%3600) % 60return f"{hours:02d}:{minutes:02d}:{seconds:02d}"
Ⅳ.时间戳
定义
-
时间戳:记录时间的数字,单位可以是秒、毫秒等。---一般是毫秒
-
参考时间 :通常是 Unix 纪元(1970-01-01 00:00:00 UTC)。
-
用途:标记事件发生的时间,方便计算时间差。
*判断相邻毫秒时间戳小于1s---即两个相邻时间戳差 ≤ 1000 毫秒。
Ⅴ.文件读取
with open("log.txt", "r") as file:
lines = file.readlines()
-
with open("log.txt", "r") as file::
-
open("log.txt", "r"):
-
打开名为 "log.txt" 的文件。
-
"r":以只读模式(read)打开。
-
-
with ... as file::
-
使用 with 语句,确保文件操作完后自动关闭。
-
file 是文件对象,代表打开的 "log.txt"。
-
-
-
lines = file.readlines():
-
file.readlines():
-
读取 file 的所有行。
-
返回一个列表,每个元素是一行文本(包含换行符 \n)。
-
-
lines:
- 变量,存储读取的行列表。
-

Ⅵ.堆

heap[0]----堆顶元素
将列表转换为最大堆如果需要将现有列表转换为最大堆,可以使用 heapq.heapify(),同时将元素取反:
import heapq
原始列表
nums = [3, 1, 2]
将元素取反后转换为堆
heap = [-x for x in nums]
heapq.heapify(heap)
查看堆顶元素(取反还原)
max_item = -heap[0]
print("堆顶元素:", max_item) # 输出:3
弹出元素(取反还原)
max_item = -heapq.heappop(heap)
print("弹出元素:", max_item) # 输出:3
print("堆中剩余元素:", [-x for x in heap]) # 输出:[2, 1]
最大堆的常用操作
以下是最大堆的常用操作及其实现方式:
1. 插入元素 heapq.heappush(heap, -item) 2. 弹出元素 max_item = -heapq.heappop(heap) 3. 查看堆顶元素 max_item = -heap[0] 4. 将列表转换为最大堆 heap = [-x for x in nums] heapq.heapify(heap)

Ⅶ.math操作





Ⅷ.range()方法单独使用


Ⅸ.python 的异常输出

Ⅹ.for-else 和while-else

