合并两个多维列表,同时保留重复项

时间:2017-12-10 04:27:08

标签: python python-2.7

我有两个列表如下:

    a = [[1,128,"11:18:23.456",2,0,0,"P"],[2,1345,"22:18:23.456",2,1,0,"Q"],[3,1365,"24:18:23.456",1,0,0,"N"]]
    b = [[1,128,"11:18:23.456",2,0,0,"P"],[2,1345,"22:18:23.456",2,1,0,"Q"],[2,1345,"22:18:23.456",2,1,0,"Q"]]

我需要合并它们(并且还根据每个列表中的第二个条目(即128,1345,1365)对它们进行排序,以便获得以下合并列表:

    c_before_sort = [[1,128,"11:18:23.456",2,0,0,"P"],[2,1345,"22:18:23.456",2,1,0,"Q"],[3,1365,"24:18:23.456",1,0,0,"N"],[1,128,"11:18:23.456",2,0,0,"P"],[2,1345,"22:18:23.456",2,1,0,"Q"],[2,1345,"22:18:23.456",2,1,0,"Q"]]

    c_after_sort = [[1,128,"11:18:23.456",2,0,0,"P"],[1,128,"11:18:23.456",2,0,0,"P"],[2,1345,"22:18:23.456",2,1,0,"Q"],[2,1345,"22:18:23.456",2,1,0,"Q"],[2,1345,"22:18:23.456",2,1,0,"Q"],[3,1365,"24:18:23.456",1,0,0,"N"]]

2 个答案:

答案 0 :(得分:1)

这看起来很容易:

c = a[:]
c.extend(b)
c.sort(key=lambda lst: lst[2])

答案 1 :(得分:0)

只需将-;; tab indents -(setq-default indent-tabs-mode nil) -(setq-default tab-width 2) -(setq indent-line-function 'insert-tab) +;; PDFLaTeX mode and source correlation +(setq TeX-PDF-mode t) +; (setq TeX-source-correlate-method 'synctex) +; (add-hook 'LaTeX-mode-hook 'TeX-source-correlate-mode) -;; increase default font size -(set-face-attribute 'default nil :height 100) +;;************************************************************ +;; configure HTML editing +;;************************************************************ +;; -;; DEL is deleting the wrong way -;; https://www.gnu.org/software/emacs/manual/html_node/emacs/DEL-Does-Not-Delete.html -(normal-erase-is-backspace-mode 1) +;; Test +(add-to-list 'auto-mode-alist '("\\.js\\'" . javascript-mode)) +(autoload 'javascript-mode "javascript" nil t) +(setq js-indent-level 2) 列表合并在一起,然后根据每个列表的第二个元素operator.itemgetter()进行排序:

git diff