banner
NEWS LETTER

Python竞赛输入模板

Scroll down

简洁优先

1
2
3
4
5
6
7
n, m = map(int, input().split())
a = list(map(int, input().split()))

cmd_raw, args_raw = input().split(maxsplit=1)
cmd = int(cmd_raw)
args = list(map(int, args_raw.split()))

优化读入

速度优化

1
2
3
4
5
input=sys.stdin.readline

# Example
n, m = map(int, input().split())
a = list(map(int, input().split()))

防Invalid Input(II)

最简洁

1
2
3
4
5
6
import sys
inp = map(int, filter(bool, sys.stdin.read().split()))

# Example
n, m = next(inp), next(inp)
a = [next(inp) for _ in range(n)]

省内存

1
2
3
4
5
6
7
def reader(func=int):
while line := sys.stdin.readline():
yield from map(func, line.split())

# Example
n, m = next(inp), next(inp)
a = [next(inp) for _ in range(n)]

内存优化(即使不防II)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from collections.abc import Generator
import sys


def chunk_reader(func=int, chunk_size=1 << 15) -> Generator[int, None, None]:
inp_cache = ""
while True:
chunk = sys.stdin.read(chunk_size) # 读入一块
if not chunk: # 判断是否为结尾
if inp_cache: # 判断缓存内是否还有内容
yield func(inp_cache) # 输出
del inp_cache # 删除缓存
break # 跳出循环

for c in chunk:
if c.isspace(): # 判断当前字符是否是空格或者换行
if inp_cache: # 如果当前缓存有内容, 即代表此内容已经到了结尾
yield func(inp_cache) # 输出
del inp_cache
inp_cache = "" # 删除缓存并重新初始化
else:
inp_cache += c # 否则把字符串加入缓存
del c # 删除字符
del chunk # 删除区块


inp = chunk_reader(chunk_size=1 << 18)
n, m = next(inp), next(inp)
a = [next(inp) for _ in range(n)]

我很可爱,请给我钱

其他文章
cover
Python ST表
  • 25/03/26
  • 14:51
  • 128
  • 1
目录导航 置顶
  1. 1. 简洁优先
  2. 2. 优化读入
    1. 2.1. 速度优化
    2. 2.2. 防Invalid Input(II)
      1. 2.2.1. 最简洁
      2. 2.2.2. 省内存
      3. 2.2.3. 内存优化(即使不防II)