Jump to content

Recommended Posts

Posted

Hi.

I wrote a function that convert the list.

The function deletes the sub list whose first element does not exist in the list 2.

Please comment.

(defun convert (L b / L a)
 (setq a (mapcar 'car L))
 (mapcar '(lambda (%) (setq a (mapcar '(lambda (e) (if (= e %) nil e)) a))) b)
 (setq a (vl-remove nil a))
 (mapcar '(lambda (%) (setq L (mapcar '(lambda (x) (if (= (car x) %) nil x)) L))) a)
 (vl-remove nil L)
 )

Example:

(convert lst1 lst2)

List 1:

'(("1" 1.7 1.7)
("2" 9.9 9.9)
("3" 9.8 9.
("4" 9.9 9.9)
("5" 1.4 1.4)
("6" 8.8 8.
("7" 1.3 1.3)
("8" 8.7 8.7)
("9" 9.5 9.5))

List 2:

'("1" "3" "9")

Result:

'(("1" 1.7 1.7)
("3" 9.8 9.
("9" 9.5 9.5))

Posted

Try this:

 

(defun convert (l1 l2 / l)
 (mapcar '(lambda (x) (if (member (car x) l2)(setq l (cons x l)))) l1)
 (if l (reverse l) (princ)
 )
)

Posted

I would suggest:

(defun convert ( k l )
   (vl-remove nil (mapcar '(lambda ( k ) (assoc k l)) k))
)

Example:

(convert
  '("1" "3" "9")
  '(
       ("1" 1.7 1.7)
       ("2" 9.9 9.9)
       ("3" 9.8 9.
       ("4" 9.9 9.9)
       ("5" 1.4 1.4)
       ("6" 8.8 8.
       ("7" 1.3 1.3)
       ("8" 8.7 8.7)
       ("9" 9.5 9.5)
   )
)
=> (("1" 1.7 1.7) ("3" 9.8 9. ("9" 9.5 9.5))

Posted

Tharwat thank you.

Lee Mac looking at your code and your way of thinking I know that I still have a lot to learn.

Posted

Hi guys,

I have another similar question, how it could work on reversing it ? So in LM's example would be returned associations with "2" "4" "5" "6" "7" and "8".

Posted
Lee Mac looking at your code and your way of thinking I know that I still have a lot to learn.

 

Thank you; I hope that my code proves useful to your studies.

 

how it could work on reversing it ? So in LM's example would be returned associations with "2" "4" "5" "6" "7" and "8".

 

(defun convert ( k l )
   (vl-remove-if '(lambda ( x ) (member (car x) k)) l)
)

Posted

Thanks Lee,

Kowal is not the only one who will learn from this.

Anyway nice thread, it inspired me on collectiing some "assoc list manipulation" subfunctons, as many exist but they work only for normal lists.

Posted

When you have first solution, you can build second request on first solution :

 

(defun convert ( k l )
 (vl-remove-if '(lambda ( x ) (vl-position x [b][color=darkred](vl-remove nil (mapcar '(lambda ( k ) (assoc k l)) k))[/color][/b])) 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...