Kowal Posted June 10, 2016 Posted June 10, 2016 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)) Quote
Tharwat Posted June 10, 2016 Posted June 10, 2016 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) ) ) Quote
Lee Mac Posted June 10, 2016 Posted June 10, 2016 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)) Quote
Kowal Posted June 14, 2016 Author Posted June 14, 2016 Tharwat thank you. Lee Mac looking at your code and your way of thinking I know that I still have a lot to learn. Quote
Grrr Posted June 14, 2016 Posted June 14, 2016 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". Quote
Lee Mac Posted June 14, 2016 Posted June 14, 2016 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) ) Quote
Grrr Posted June 14, 2016 Posted June 14, 2016 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. Quote
marko_ribar Posted June 14, 2016 Posted June 14, 2016 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) ) 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.