import sys import re pat = re.compile(r"\[(\d+),(-?\d+)\] (.*)") outputs = {} for i, line in enumerate(sys.stdin.readlines()): m = pat.match(line) if not m: # print(line) continue p = int(m.group(1)) n = int(m.group(2)) rest = m.group(3) if (p, n) not in outputs: outputs[p, n] = (i, []) outputs[p, n][1].append(rest) for ((p, n), (i, lines)) in sorted(outputs.items(), key=lambda v: (-v[0][1], v[0][0])): # lines = outputs[p, n] print(f"---- {p} [{n}] ----") for line in lines: print(line) print() print("Done.")