Pythonic way to merge two overlapping lists, preserving order

后端 未结 8 1598
鱼传尺愫
鱼传尺愫 2021-02-03 22:40

Alright, so I have two lists, as such:

  • They can and will have overlapping items, for example, [1, 2, 3, 4, 5], [4, 5, 6, 7].
  • The
8条回答
  •  独厮守ぢ
    2021-02-03 22:55

    First of all and for clarity, you can replace your while loop with a for loop:

    def merge(master, addition):
        for n in xrange(1, len(master)):
            if master[-n:] == addition[:n]:
                return master + addition[n:]
        return master + addition
    

    Then, you don't have to compare all possible slices, but only those for which master's slice starts with the first element of addition:

    def merge(master, addition):
        indices = [len(master) - i for i, x in enumerate(master) if x == addition[0]]
        for n in indices:
            if master[-n:] == addition[:n]:
                return master + addition[n:]
        return master + addition
    

    So instead of comparing slices like this:

    1234123141234
                3579
               3579
              3579
             3579
            3579
           3579
          3579
         3579
        3579
       3579
      3579
     3579
    3579
    

    you are only doing these comparisons:

    1234123141234
      |   |    |
      |   |    3579
      |   3579
      3579
    

    How much this will speed up your program depends on the nature of your data: the fewer repeated elements your lists have, the better.

    You could also generate a list of indices for addition so its own slices always end with master's last element, further restricting the number of comparisons.

提交回复
热议问题