Merge sorted lists in python

前端 未结 9 986
花落未央
花落未央 2020-12-19 00:50

I have a bunch of sorted lists of objects, and a comparison function

class Obj :
    def __init__(p) :
        self.points = p
def cmp(a, b) :
    return a.p         


        
9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-19 00:56

    Use the bisect module. From the documentation: "This module provides support for maintaining a list in sorted order without having to sort the list after each insertion."

    import bisect
    
    def magic(*args):
        r = []
        for a in args:
            for i in a:
                bisect.insort(r, i)
        return r
    

提交回复
热议问题