python leetcode 笔记

只为记录一些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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值