Removing elements from a nested list

Removing elements from a nested list

Browning_Zed
Advocate Advocate
1,166 Views
3 Replies
Message 1 of 4

Removing elements from a nested list

Browning_Zed
Advocate
Advocate

Hi.
What way is there to remove certain elements from lists included in a nested list?
If the original list is like this:
'((1 2 3 4) (1 2 3 4) (1 2 3 4))
how to remove elements (nth 1) and (nth 2) from the list to transform it:
'((1 4) (1 4) (1 4))

0 Likes
Accepted solutions (2)
1,167 Views
3 Replies
Replies (3)
Message 2 of 4

hak_vz
Advisor
Advisor
Accepted solution

Try this

 

 

(defun rem_nth (lst ntlst / a i ret)
	(foreach e lst
	   (setq i -1)
	   (while (< (setq i (1+ i)) (length e))
			(if (not(member i ntlst)) (setq a (cons (nth i e) a)))
	   )
	   (setq ret (cons (reverse a) ret) a nil)
	)
	(reverse ret)
)
Example
(setq mm '((1 2 3 4) (5 6 7 8 )  (9 10 11 12)))
(rem_nth mm '(0 1))
;-->((3 4) (7 8 ) (11 12))
Example 2:
Command: (setq mm '((1 2 3 4) (5 6) (9 10 12)))
((1 2 3 4) (5 6) (9 10 12))
Command: (rem_nth mm '(3 4))
((1 2 3) (5 6) (9 10 12))

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 3 of 4

pbejse
Mentor
Mentor
Accepted solution

Another 

 

(defun _remover (l o)
  (if l
    (cons
      (repeat (setq i (length (car l)))
	      (if (not (vl-position (setq i (1- i)) o))
		(setq nl (cons (nth i (car l)) nl))
	      ) nl
	    )
      (_remover (cdr l) (progn (setq nl nil) o))
      )
    )
  )

 

(defun _remove (lst n / a i nl l)
  (while (setq a (car lst))
    (setq nl  nil lst (Cdr lst))
    (repeat (setq i (length a))
      (if (not (vl-position (setq i (1- i)) n))
	(setq nl (cons (nth i a) nl))
      )
    )
    (setq l (cons nl l))
  )
  (reverse l)
)

 HTH

Message 4 of 4

ronjonp
Mentor
Mentor

Based on your example lists are you wanting to remove duplicate entries ?

(setq l '((1 2 3 4) (1 2 3 4) (1 2 3 4)))
(cons (car l) (vl-remove (car l) l))

This too.

0 Likes