robierzo Posted April 5, 2013 Posted April 5, 2013 Hello. I need to remove the sub-lists whose first element is repeated. I use this, but it does not work. (setq lista1 (vl-remove-if '(lambda (x y) (= (car x) (car y))) lista)) Example: I have ---->(setq lista '(("K" 10 12 13) ("J" 15 14 75) ("P" 4 2 3) ("J" 5 6 7) ("H" 2 2 3) ("K" 4 5 6) ("M" 4 1 2))) Result----> (setq lista1 '(("K" 10 12 13) ("J" 15 14 75) ("P" 4 2 3) ("H" 2 2 3) ("M" 4 1 2))) Regards. Quote
pBe Posted April 5, 2013 Posted April 5, 2013 How can you tell which one of the duplicates remain? like ("K" 10 12 13) and ("K" 4 5 6) , the first in order of sequence? Quote
Tharwat Posted April 5, 2013 Posted April 5, 2013 Try this ... (foreach pc lista (if (not (member (car pc) (mapcar 'car lista1))) (setq lista1 (cons pc lista1))) ) (reverse lista1) Quote
marko_ribar Posted April 5, 2013 Posted April 5, 2013 Another : (defun foo ( lst / lstn ) (foreach char (acet-list-remove-duplicates (mapcar 'car lst) nil) (setq lstn (cons (assoc char lst) lstn)) ) (reverse lstn) ) (foo '(("K" 10 12 13) ("J" 15 14 75) ("P" 4 2 3) ("J" 5 6 7) ("H" 2 2 3) ("K" 4 5 6) ("M" 4 1 2))) Quote
Lee Mac Posted April 5, 2013 Posted April 5, 2013 Quickly written: (defun f ( l ) (if l (cons (car l) (f (vl-remove-if '(lambda ( x ) (= (caar l) (car x))) (cdr l))))) ) _$ (setq lista '(("K" 10 12 13) ("J" 15 14 75) ("P" 4 2 3) ("J" 5 6 7) ("H" 2 2 3) ("K" 4 5 6) ("M" 4 1 2))) (("K" 10 12 13) ("J" 15 14 75) ("P" 4 2 3) ("J" 5 6 7) ("H" 2 2 3) ("K" 4 5 6) ("M" 4 1 2)) _$ (f lista) (("K" 10 12 13) ("J" 15 14 75) ("P" 4 2 3) ("H" 2 2 3) ("M" 4 1 2)) Alternatively, (defun f2 ( l / r ) (foreach x l (if (not (assoc (car x) r)) (setq r (cons x r)) ) ) (reverse r) ) Quote
pBe Posted April 5, 2013 Posted April 5, 2013 Another: (defun _remsubif (l l2) (if (setq a (car l)) (progn (if (not (vl-position (car a) (mapcar 'car l2))) (setq l2 (cons a l2)) ) (_remsubif (cdr l) l2) ) (reverse l2) ) ) I'm waiting on the OP's reply if the result are based on the rest of the elements values. ("K" 10 12 13) Quote
Lee Mac Posted April 5, 2013 Posted April 5, 2013 One more, (defun f3 ( l / x r ) (while l (setq x (car l) r (cons x r) l (vl-remove-if '(lambda ( y ) (= (car x) (car y))) (cdr l)) ) ) (reverse r) ) Quote
Tharwat Posted April 5, 2013 Posted April 5, 2013 Another one for fun . (defun fun (lst / l) (mapcar '(lambda (j) (if (not (member (car j) (mapcar 'car l))) (setq l (cons j l)) ) ) lst ) (reverse l) ) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.