gsc Posted October 26, 2023 Share Posted October 26, 2023 Hi, I have 2 lists that I want to perform a percentage calculation with: (setq lst1 '(("H608" 32) ("H609" 32) ("H610" 32) ("H611" 32) ("H612" 32) ("H613" 32) ("H614" 32))) (setq lst2 '(("H608" 30) ("H609" 24) ("H610" 21) ("H612" 20) ("H613" 25) ("H614" 22))) The numbers in lst1 are the denominator The numbers in lst2 are the numerator The first element (e.g. H608) of the pairs is the name of a location. The number in the denominator list (lst1) is the amount of calculations that were performed The number in the numerator list (lst2) is the amount of valid calculations If there are locations (e.g. H611) in the denominator list (lst1) and not in the numerator list (lst2), it means that there are no valid calculations for that location. This means the calculated percentage of that location is 0% How do I create a resulting list like this: (setq lst3 '(("H608" 30 32) ("H609" 24 32) ("H610" 21 32) ("H611" 0 32) ("H612" 20 32) ("H613" 25 32) ("H614" 22 32))) Quote Link to comment Share on other sites More sharing options...
exceed Posted October 26, 2023 Share Posted October 26, 2023 (edited) (defun c:test ( / lst1 lst2 lst3 index index2 a b c) (setq lst1 (list (list "H608" 32) (list "H609" 32) (list "H610" 32) (list "H611" 32) (list "H612" 32) (list "H613" 32) (list "H614" 32))) (setq lst2 (list (list "H608" 30) (list "H609" 24) (list "H610" 21) (list "H612" 20) (list "H613" 25) (list "H614" 22))) (setq index 0) (setq lst3 '()) (repeat (length lst1) (setq a (nth index lst1)) (setq index2 0) (setq c '()) (repeat (length lst2) (setq b (nth index2 lst2)) (if (= (car a) (car b)) (setq c (list (car a) (cadr b) (cadr a))) ) (setq index2 (+ index2 1)) ) (if (= c nil) (setq c (list (car a) 0 (cadr a))) ) (setq lst3 (cons c lst3)) (setq index (+ index 1)) ) (setq lst3 (reverse lst3)) (princ lst3) (princ) ) Edited October 26, 2023 by exceed Quote Link to comment Share on other sites More sharing options...
gsc Posted October 26, 2023 Author Share Posted October 26, 2023 Thanx man! Works like a charm....I thought this would be a mapcar lambda function thing which I find quite complicated to understand Quote Link to comment Share on other sites More sharing options...
j2lstaples Posted October 26, 2023 Share Posted October 26, 2023 12 hours ago, gsc said: Thanx man! Works like a charm....I thought this would be a mapcar lambda function thing which I find quite complicated to understand I usually use repeat or foreach whenever I do a recurring function. Sometimes I use mapcar and lambda if the list has lists. It's just a flavor, no wrong way in coding your process. The only gripe I have is people not putting comments in their code. All the time I feel like a lisp cryptographer whenever I see code strewn across these forums. Quote Link to comment Share on other sites More sharing options...
mhupp Posted October 28, 2023 Share Posted October 28, 2023 (edited) Here it is using foreach and assoc also added some more values for testing. (setq lst1 '(("H608" 32) ("H609" 32) ("H610" 32) ("H611" 32 33) ("H612" 32 33) ("H613" 32) ("H614" 32))) (setq lst2 '(("H608" 30) ("H609" 24 25 26) ("H610" 21) ("H612" 20) ("H613" 25) ("H614" 22) ("H615" 22))) ;;----------------------------------------------------------------------------;; ;; Combine two Lists (defun C2L (lst1 lst2 / item str a b lst3) (foreach item lst1 ;step thougth each mini list in list one (setq str (car item)) ;get the first value "H609" (if (setq b (cdr (setq a (assoc str lst2)))) ;check list 2 for this value and set a = ("H609" 24 25 26) b= (24 25 26) (foreach num b ;step thought b and add each item to the origonal list (setq item (append item (list num))) ;("H609" 32) + 24 +25 +26 = ("H609" 32 24 25 26) ) (setq item (append item (list 0))) ;if item isn't in lst two add a 0 ) (setq lst2 (vl-remove a lst2)) ;remove ("H609" 24 25 26) from lst2 (setq lst3 (append lst3 (list item))) ;add the new built list to lst3 ) (foreach item lst2 ;after lst1 is checked against lst2 if their is anything left in lst2 (setq item (append item (list 0))) ;add a 0 to the list and (setq lst3 (append lst3 (list item))) ;add that mini list to lst3 ) lst3 ) >(C2L lst1 lst2) >(("H608" 32 30) ("H609" 32 24 25 26) ("H610" 32 21) ("H611" 32 33 0) ("H612" 32 33 20) ("H613" 32 25) ("H614" 32 22) ("H615" 22 0)) --Edit comments for @j2lstaples >(C2L lst1 lst2) >(("H608" 32 30) ("H609" 32 24 25 26) ("H610" 32 21) ("H611" 32 33 0) ("H612" 32 33 20) ("H613" 32 25) ("H614" 32 22) ("H615" 22 0)) Edited October 28, 2023 by mhupp Quote Link to comment Share on other sites More sharing options...
marko_ribar Posted October 28, 2023 Share Posted October 28, 2023 (edited) : (setq lst1 '(("H608" 32) ("H609" 32) ("H610" 32) ("H611" 32) ("H612" 32) ("H613" 32) ("H614" 32))) (("H608" 32) ("H609" 32) ("H610" 32) ("H611" 32) ("H612" 32) ("H613" 32) ("H614" 32)) : (setq lst2 '(("H608" 30) ("H609" 24) ("H610" 21) ("H612" 20) ("H613" 25) ("H614" 22))) (("H608" 30) ("H609" 24) ("H610" 21) ("H612" 20) ("H613" 25) ("H614" 22)) : (setq pre (reverse (vl-member-if '(lambda ( x ) (= (car x) "H610")) (reverse lst2)))) (("H608" 30) ("H609" 24) ("H610" 21)) : (setq suf (vl-member-if '(lambda ( x ) (= (car x) "H612")) lst2)) (("H612" 20) ("H613" 25) ("H614" 22)) : (setq lst2 (append pre (list (list "H611" 0)) suf)) (("H608" 30) ("H609" 24) ("H610" 21) ("H611" 0) ("H612" 20) ("H613" 25) ("H614" 22)) : (mapcar '(lambda ( a b ) (if (= (car a) (car b)) (list (car a) (cadr b) (cadr a)))) lst1 lst2) (("H608" 30 32) ("H609" 24 32) ("H610" 21 32) ("H611" 0 32) ("H612" 20 32) ("H613" 25 32) ("H614" 22 32)) Edited October 29, 2023 by marko_ribar 1 Quote Link to comment Share on other sites More sharing options...
Tharwat Posted October 28, 2023 Share Posted October 28, 2023 Another. (mapcar '(lambda (j) (setq r nil) (mapcar '(lambda (k) (and (= (car j) (car k)) (setq r (cons (cadr j) r) r (cons (cadr k) r) ) ) ) lst2 ) (setq l (cons (if r (cons (car j) r) (list (car j) 0 (cadr j))) l)) ) lst1 ) (reverse l) 3 Quote Link to comment Share on other sites More sharing options...
gsc Posted October 30, 2023 Author Share Posted October 30, 2023 On 10/28/2023 at 10:26 PM, Tharwat said: Another. (mapcar '(lambda (j) (setq r nil) (mapcar '(lambda (k) (and (= (car j) (car k)) (setq r (cons (cadr j) r) r (cons (cadr k) r) ) ) ) lst2 ) (setq l (cons (if r (cons (car j) r) (list (car j) 0 (cadr j))) l)) ) lst1 ) (reverse l) This is what I mean...I see what the code is doing, like the first double repeat solution but for me difficult to write from scratch Thanx! Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted October 30, 2023 Share Posted October 30, 2023 (defun foo ( l m ) (mapcar '(lambda ( x ) (list (car x) (cond ((cadr (assoc (car x) m))) (0)) (cadr x))) l) ) _$ (foo lst1 lst2) (("H608" 30 32) ("H609" 24 32) ("H610" 21 32) ("H611" 0 32) ("H612" 20 32) ("H613" 25 32) ("H614" 22 32)) 1 Quote Link to comment Share on other sites More sharing options...
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.