在Python中合并两个排序的项目列表

时间:2018-07-30 07:04:09

标签: python merge

我的问题与找出合并2个排序项目的不同方式有关? 我试图找到一种简单的方法来合并2个排序的项目。

def merge(arr1, arr2):
    return sorted(arr1 + arr2)

# Example: merge([1, 4, 7], [2, 3, 6, 9]) => [1, 2, 3, 4, 6, 7, 9]

我不确定是否要使其复杂化。这使用了a built-in function,这意味着更难弄清实现细节。

我还发现我可以使用cypthon's heapq中的merge()函数。

想知道是否有使用其他方法的想法,例如:

Gist for Merge in python

5 个答案:

答案 0 :(得分:1)

使用heapq中的merge

>>> l1 = [1, 4, 7]
>>> l2 = [2, 3, 6, 9]
>>> from heapq import merge
>>> list(merge(l1,l2))
[1, 2, 3, 4, 6, 7, 9]

答案 1 :(得分:1)

由您决定使用哪种实现。考虑的是您需要干净的代码还是需要性能。

对于干净的代码,您可以使用:

  1. 已排序(l1 + l2)
  2. 从heappq合并

这两者的复杂度均为O(nlogn)

此实现为https://gist.github.com/Jeffchiucp/9dc2a5108429c4222fe4b2a25e35c778,算法复杂度为O(n)。

答案 2 :(得分:0)

您不想连接两个排序列表并再次对其进行排序,这是不需要的。有O(n)个用于合并排序列表的算法,由于再次排序,您的算法将是O(n log n)。使用here中提取的Priority queue合并两个排序的列表:

from Queue import PriorityQueue
class Solution(object):
    def mergeKLists(self, lists):
        dummy = ListNode(None)
        curr = dummy
        q = PriorityQueue()
        for node in lists:
            if node: q.put((node.val,node))
        while q.qsize()>0:
            curr.next = q.get()[1]
            curr=curr.next
            if curr.next: q.put((curr.next.val, curr.next))
        return dummy.next

答案 3 :(得分:0)

这是我的解决方法:

BUILD FAILED
C:\Users\Seren\Documents\IdeaProjects\TMCProjects\mooc-2013-OOProgrammingWithJava-PART1\week1-002.HelloWorld\nbproject\build-impl.xml:603: The following error occurred while executing this line:
C:\Users\Seren\Documents\IdeaProjects\TMCProjects\mooc-2013-OOProgrammingWithJava-PART1\week1-002.HelloWorld\nbproject\build-impl.xml:245: Error running javac.exe compiler

Total time: 0 seconds


-pre-init:

-init-private:

-init-user:

-init-project:

-init-macrodef-property:

-do-init:

-post-init:

-init-check:

-init-ap-cmdline-properties:

-init-macrodef-javac-with-processors:

-init-macrodef-javac-without-processors:

-init-macrodef-javac:

-init-macrodef-junit:

-init-debug-args:

-init-macrodef-nbjpda:

-init-macrodef-debug:

-init-macrodef-java:

-init-presetdef-jar:

-init-ap-cmdline-supported:

-init-ap-cmdline:

init:

-deps-jar-init:
   [delete] Deleting: C:\Users\Seren\Documents\IdeaProjects\TMCProjects\mooc-2013-OOProgrammingWithJava-PART1\week1-002.HelloWorld\build\built-jar.properties

deps-jar:

-warn-already-built-jar:
[propertyfile] Updating property file: C:\Users\Seren\Documents\IdeaProjects\TMCProjects\mooc-2013-OOProgrammingWithJava-PART1\week1-002.HelloWorld\build\built-jar.properties

-check-automatic-build:

-clean-after-automatic-build:

-verify-automatic-build:

-pre-pre-compile:

-pre-compile:

-copy-persistence-xml:

-compile-depend:

-do-compile:
    [javac] Compiling 1 source file to C:\Users\Seren\Documents\IdeaProjects\TMCProjects\mooc-2013-OOProgrammingWithJava-PART1\week1-002.HelloWorld\build\classes

答案 4 :(得分:0)

def cleanMerge(self, list1, list2):
    if len(list1)!=len(list2):
        less = list1 if len(list1)<len(list2) else list2
        chosenList = list2 if len(list1)<len(list2) else list1
    chosenList = list1
    less = list2
    l1,l2,r1,r2 = 0,0,len(chosenList),len(less)
    while l2<r2 and l1<r1:
        if chosenList[l1]>less[l2]:
            chosenList[l1+1:r1+1] = chosenList[l1:r1]
            chosenList[l1] = less[l2]
            l2+=1
            l1+=1
            r1+=1
        else:
            l1+=1
            continue
    if l2<r2:
        for item in less[l2:r2]:
            chosenList.append(item)
    return chosenList