import collections
q = list.pop(0) # index=0, 返回后,a=0, list=[剩余元素]
# list.remove(element)
# list.append(element)
q = collections.deque()
q.popleft()
q.pop() # 从right pop
q.append(1)
q.insert(0, 1)
q.extend(list) # 两个list concat, 不返回
l = sorted(l)
l = sorted(l, key = lambda x: x[0])
l = sorted(l, reverse=True)
l.sort() # 注意这是in-place
# 多个key
array.sort(key=lambda element: (element[1],element[2]), reverse=True) #多个sorting key
# 双list排 或者直接 循环时排
[x for _, x in sorted(zip(Y, X), key=lambda pair: pair[0])]
# 自定义
cars = ['Ford', 'Mitsubishi', 'BMW', 'VW']
def myFunc(e):
return len(e)
cars.sort(reverse=True, key=myFunc)
range(len(nums) - 1, -1, -1)
# 左闭右开
# 从右边取,注意为0会取全体值是否是想要的
list[-1:] # 取最后一个
list[-0:] # 取全体list
list[::2] # 每间隔2个取
# BFS常用
from collections import deque
queue = deque([1,2,3])
queue.append(4)
queue.popleft()
matrix = [[0 for _ in range(col)] for _ in range(row)]
matrix1 = [[0] * col for _ in range(row) ]
# 错误,都是引用导致值相同
[[0] * col] * row
dict(sorted(x.items(), key=lambda item: item[1]))
# 注意如果返回sorted, 返回的是list[tuple], 如[(1, 3), (2, 2), (3, 1)]
del my_dict[key]
value = my_dict.pop(key, None)
list(frequency.keys())[:k]
list(frequency.values())[:k]
import heapq
data = [2,3,5,1]
heapq.heapify(data)
heapq.heappush(data, 6)
print(heapq.heappop(data))
# 根据head / head.next / head & head.next 来决定迭代终点
while head is not None and head.next is not None:
pass
# ord
ord(s[0])/ s[0].isdigit()
# 小写
s.lower()
正确
while s[l] == s[r] and l >= 0 and r < len(s)
错误
while l >= 0 and r < len(s) and s[l] == s[r]
float('inf')
float('-inf')
a & b
a | b
~a # NOT
a ^ b # Exclusive OR 异或
1 << bit # 左移1位相当于乘2,x << n等价于x * (2 ^ n)