文章目录
- 一、基础知识
-
- 1.基本输入输出
- 2.字符列表连接
- 3.字母的大小写转换
- 4.匿名函数lambda
- 5.进制转换
- 6.字符与整型之间的转换
- 7.格式化保留小数点后几位小数
- 8.列表排序
- 9.str的内建函数
- 10.list的内建函数
- 二、常用内置模块
-
- 1.阶乘factorial
- 2.计数器Counter
- 3.默认字典defaultdict
- 4.双端队列deque
- 5.全排列permutations
- 6.组合combinations
- 7.累加accumulate
- 8.堆heapq
- 时间库datetime
- 三、常用算法模板
-
- 1.最大公因数与最小公倍数
- 2.质数的判断,质数个数的求解
- 3.快速幂
- 4.二分查找与插入
- 5.获取一个数每一位的值
- 6.动态规划
-
- 最长上升子序列问题
- 最长公共子串
- 最长公共子序列
- 最长公共上升子序列
- 7.图论
-
- 7.1.关于建图
- 7.2 图中的最短路径问题
- 7.3 并查集算法
- 8.其他
-
- 更新时间
- 参考链接
一、基础知识
1.基本输入输出
num1 = input()
num2 = int(input())
a, b, c = map(int, input().split())
lst1 = list(map(int, input().split()))
print(num1)
print(num2)
print(a,b,c)
print(lst1)
lst12 = [int(input()) for _ in range(3)]
print(lst12)
lst13 = [list(map(int, input().split())) for _ in range(3)]
print(lst13)
12
45
12 23 34
56 66 77
1
2
3
1 2 3
4 5 6
7 8 9
12
45
12 23 34
[56, 66, 77]
[1, 2, 3]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
2.字符列表连接
lst2 = ['hello', 'world']
print(''.join(lst2))
helloworld
3.字母的大小写转换
str1 = 'hello'
str2 = 'WORLD'
print(str1)
print(str1.upper())
print(str2)
print(str2.lower())
hello
HELLO
WORLD
world
4.匿名函数lambda
def square(a):
return a * a
print(list(map(square, [1, 2, 3])))
print(list(map(lambda x: x*x, [1, 2, 3])))
lst3 = [[2, 4],
[8, 9],
[4, 5],
[5, 10]]
print(sorted(lst3, key=lambda x: x[0]))
[1, 4, 9]
[1, 4, 9]
[[2, 4], [4, 5], [5, 10], [8, 9]]
5.进制转换
print(hex(16), oct(16), bin(16))
0x10 0o20 0b10000
6.字符与整型之间的转换
print(chr(97), ord('a'))
a 97
7.格式化保留小数点后几位小数
num3 = 3.1415926
print(f'{
num3:.3f}')
3.142
8.列表排序
lst4 = [3, 1, 45, 67, 21]
lst5 = sorted(lst4, reverse=True)
print(lst4)
print(lst5)
lst4.sort(reverse=True)
print(lst4)
[3, 1, 45, 67, 21]
[67, 45, 21, 3, 1]
[67, 45, 21, 3, 1]
9.str的内建函数
str3 = 'hello world'
str4 = 'HELLO WORLD'
str5 = 'Hello World'
print(str3.upper())
print(str4.lower())
print(str5.swapcase())
print(str3.capitalize())
print(str3.title())
print(str3.find('l', 0, -1))
print(str3.index('l'))
print(str3.rfind('l', 0, -1))
print(str3.count('ll'))
print(str3.replace('l', '*', 3))
print(str3.strip('h'))
print(str3.split())
print('*'.join(str3))
print(str3)
print(str4)
print(str5)
HELLO WORLD
hello world
hELLO wORLD
Hello world
Hello World
2
2
9
1
he**o wor*d
ello world
['hello', 'world']
h*e*l*l*o* *w*o*r*l*d
hello world
HELLO WORLD
Hello World
10.list的内建函数
lst14 = [1, 2, 3]
lst15 = [3, 4, 5]
print(lst14 + lst15)
str5 = 'hello world'
lst14.append(str5)
print(lst14)
lst14.extend(str5)
print(lst14)
lst14.insert(3, 'hello')
print(lst14)
lst14.remove('l')
print(lst14)
print(lst14.index('l'))
二、常用内置模块
1.阶乘factorial