Jump to content

Recommended Posts

Posted

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.

Posted

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?

Posted

Try this ... :)

 


(foreach pc lista
 (if (not (member (car pc) (mapcar 'car lista1)))
   (setq lista1 (cons pc lista1)))
 )
(reverse lista1)

Posted

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)))

Posted

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)
)

Posted

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)

Posted

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)
)

Posted

Another one for fun . :D

 

(defun fun (lst / l)
 (mapcar '(lambda (j)
            (if (not (member (car j) (mapcar 'car l)))
              (setq l (cons j l))
            )
          )
         lst
 )
 (reverse l)
)

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...