python 排序序列融合heapq.merge

2019-09-29 20:23:49 浏览数 (1)

import heapq a = [1, 4, 7, 10] b = [2, 5, 6, 11] for c in heapq.merge(a, b): ... print(c) ... 1 2 4 5 6 7 10 11

传入的序列,必须是排序过的

with open('sorted_file_1', 'rt') as file1, open('sorted_file_2', 'rt') as file2, open('merged_file', 'wt') as outf:

代码语言:javascript复制
for line in heapq.merge(file1, file2):
    outf.write(line)

0 人点赞