python笔记

python笔记

一月 30, 2026

Python笔记


前言,python里莫得define,所以很多程序我打算只写一部分,不写全了


一些基本的数组/链表

本笔记中将全部使用增删改查的方式展示

: list.append(值)

: list.pop(位置) 不能多选位置?

list.remove(值)删除第一个出现的该值

del list[0]删除开头一般给list作为tree或者队列的时候使用

del可以拓展多选下标

del list[0:2]即为删除下标0-1

: 嘶,直接赋值吧

: list.index(值)查询第一个出现的,注意index如果没查到则报错

1
2
3
4
5
6
7
from collections import deque
dq = deque([1,2,3,4.1])
dq.popleft();dq.appendleft(2)
try:
print(dq.index(0))
except:
print("没有")

/特殊的查询:if not list(查询是否为空的,算是一种符合ACM模式?)

: list.sort()内置的一种方法,或者新列表 = sorted(列表)不改变原列表的一种函数

: list.reverse()如图所示.png1 (369×552)

: count神教,count(查询数量的值)/len查询个数


队列(queue / 双端队列 deque)

queue

1
2
3
4
5
6
from queue import Queue
q = Queue()
for i in range(1,10+1):
q.put(i)
while not q.empty():
print(q.get(),end=" ")

deque

1
2
3
from collections import deque
dq = deque([1,2,3,4,5,6])
print(dq)

queue:

: q.put(值)

: q.get顺手的事,输出并移出,先进先出

: 应该是不能改

: empty()查看是否为空,如果是空输出True

: 无

: 无

: q.qsize()

deque

: dq.append(值)在队尾增,dq.appendleft(值)在队头增,deque中其他的同理

: dq.pop()队尾,dq.popleft()队头

: 赋值

: if dq判断是否为空以及index

: sorted方法

: reverse

: len

优先队列

也就是最优数据一直位于队头PriorityQueue

1
2
3
4
5
6
7
8
from queue import PriorityQueue
pq = PriorityQueue()
pq.put((10, "低优先"))
pq.put((5, "中优先"))
pq.put((0, "高优先"))
while not pq.empty():
curr_priority,curr_value = pq.get()
print("当前优先级:"+f"{curr_priority}"+'\n'+"当前值:"+f"{curr_value}")