只为记录一些python相关的特殊写法
增加元素
list1.append(elem) # list
list1.extend(list2)
list1 += list2
set1.add(elem) # set
删除元素
elem = list1.pop(n) # list pop(n)
list1.remove('abc') # list remove elem
val = dict1.pop(key) # dict pop(key)
set1.remove('abc') # set remove elem
heapq
import heapq
heapq.heapify(arr) # 堆排序arr
heapq.heappush(arr)
heapq.heappop(arr)
无穷大,无穷小,NAN
float('inf'), float('-inf'), float('nan')
判断字符的类型
isdigit(x)
isspace(x)
字符串拼接
'/'.join(['a','b','c']) # 'a/b/c'
格式转换,字符转整形
ord('a') # 97
chr(97) # 'a'
进制转换
int('101', base=2) # 5, int(string, base)
bin(5) # '0b101'
bin(5)[2:] # '101'
bin(5)[2:].zfill(8) # '00000101'
二维坐标数组[[x1,y1],[x2,y2]...]的排序,lambda表达式:按x排序
lists.sort(key=lambda x: x[0])
阶乘
from math import factorial
x = factorial(5) # 5!
排列组合
from math import comb, perm
perm(5,2) # 20
comb(5,2) # 10