python将多个排序列表逐个组合成一个大的排序列表

时间:2013-10-30 15:29:18

标签: python list sorting

我有几个排序列表,我想将它们一起添加到一个大的排序列表中。最有效的方法是什么?

这是我要做的,但效率太低了:

big_list=[]
for slist in sorted_lists: # sorted_lists is a generator, so lists have to be added one by one
    big_list.extend(slist)
big_list.sort()

以下是sorted_lists的示例:

sorted_lists的大小= 200

sorted_lists的第一个元素的大小= 1668

sorted_lists=[
['000008.htm_181_0040_0009', '000008.htm_181_0040_0037', '000008.htm_201_0041_0031', '000008.htm_213_0029_0004', '000008.htm_263_0015_0011', '000018.htm_116_0071_0002', '000018.htm_147_0046_0002', '000018.htm_153_0038_0015', '000018.htm_160_0060_0001', '000018.htm_205_0016_0002', '000031.htm_4_0003_0001', '000032.htm_4_0003_0001', '000065.htm_5_0013_0005', '000065.htm_8_0008_0006', '000065.htm_14_0038_0036', '000065.htm_127_0016_0006', '000065.htm_168_0111_0056', '000072.htm_97_0016_0012', '000072.htm_175_0028_0020', '000072.htm_188_0035_0004'….],
['000018.htm_68_0039_0030', '000018.htm_173_0038_0029', '000018.htm_179_0042_0040', '000018.htm_180_0054_0021', '000018.htm_180_0054_0031', '000018.htm_182_0025_0023', '000018.htm_191_0041_0010', '000065.htm_5_0013_0007', '000072.htm_11_0008_0002', '000072.htm_14_0015_0002', '000072.htm_75_0040_0021', '000079.htm_11_0005_0000', '000079.htm_14_0006_0000', '000079.htm_16_0054_0006', '000079.htm_61_0018_0012', '000079.htm_154_0027_0011', '000086.htm_8_0003_0000', '000086.htm_9_0030_0005', '000086.htm_11_0038_0004', '000086.htm_34_0031_0024'….],
['000001.htm_13_0037_0004', '000008.htm_48_0025_0006', '000008.htm_68_0025_0008', '000008.htm_73_0024_0014', '000008.htm_122_0034_0026', '000008.htm_124_0016_0005', '000008.htm_144_0046_0030', '000059.htm_99_0022_0012', '000065.htm_69_0045_0017', '000065.htm_383_0026_0020', '000072.htm_164_0030_0002', '000079.htm_122_0030_0009', '000079.htm_123_0049_0015', '000086.htm_13_0037_0004', '000109.htm_71_0054_0029', '000109.htm_73_0035_0005', '000109.htm_75_0018_0004', '000109.htm_76_0027_0013', '000109.htm_101_0030_0008', '000109.htm_134_0036_0030']]

修改

感谢您的回答。我想我应该更明确地说我没有模拟排序列表但我正在迭代一些大文件来获取它们。所以,我需要逐个添加它们,正如我在上面的粗略代码中所示。

2 个答案:

答案 0 :(得分:5)

标准库为此目的提供了heapq.merge

>>> a=[1,3,5,6]
>>> b=[2,4,6,8]
>>> c=[2.5,4.5]
>>> list(heapq.merge(a,b,c))
[1, 2, 2.5, 3, 4, 4.5, 5, 6, 6, 8]
>>> 

或者,在您的情况下:

big_list = list(heapq.merge(*sorted_lists))

请注意,您不必创建列表,因为heapq.merge会返回一个可迭代的内容:

for item in heapq.merge(*sorted_lists):

引用文档:

  
    

sorted(itertools.chain(*iterables))类似,但返回一个iterable,不会一次将数据全部拉入内存,并假设每个输入流已经排序(从最小到最大)。

  

答案 1 :(得分:3)

使用heapq module跟踪从以下位置选择下一个排序值的列表:

import heapq

def merge(*iterables):
    h = []
    for it in map(iter, iterables):
        try:
            next = it.next
            h.append([next(), next])
        except StopIteration:
            pass
    heapq.heapify(h)

    while True:
        try:
            while True:
                v, next = s = h[0]
                yield v
                s[0] = next()
                heapq._siftup(h, 0)
        except StopIteration:
            heapq.heappop(h)
        except IndexError:
            return

这会将所有列表推送到堆中,并按其下一个值进行排序。每次产生最低值时,都会使用所使用的迭代中的下一个值更新堆,并再次重新排序堆。

这本质上保留了[next_value, iterable]列表的列表,这些列表由next_value有效排序。

用法:

for value in merge(*sorted_lists):
    # loops over all values in `sorted_lists` in sorted order

big_list = list(merge(*sorted_lists))

创建一个新的大型列表,其中所有值都有效排序。

这个确切的实现已作为heapq.merge() function添加到heapq模块中,因此您可以这样做:

from heapq import merge

big_list = list(merge(*sorted_lists))