Interleave different length lists, elimating duplicates, and preserve order

后端 未结 6 2291
青春惊慌失措
青春惊慌失措 2021-02-11 22:00

I have two lists, let\'s say:

keys1 = [\'A\', \'B\', \'C\', \'D\', \'E\',           \'H\', \'I\']
keys2 = [\'A\', \'B\',           \'E\', \'F\', \'G\', \'H\',            


        
6条回答
  •  孤独总比滥情好
    2021-02-11 23:03

    I suspect that you may be asking for a solution to the shortest common supersequence problem, which I believe is NP-hard in the general case of an arbitrary number of input sequences. I'm not aware of any libraries for solving this problem, so you might have to implement one by hand. Probably the quickest way to get to working code would be to take interjay's answer using difflib and then use reduce to run it on an arbitrary number of lists (make sure to specify the empty list as the 3rd argument to reduce).

提交回复
热议问题