# List combination

## Recommended Posts

Hi, I need help with a complex (at least for me) list combination. I'm reading text from several files so I need to make this combination.

I have for example:
List1: [a b c]
List2: [(a n1) (c n3) (b n2)]
List3: [(n1 m1) (n1 m2) (n1 m3) (n2 m4) (n2 m5) (n3 m6)]
List4: [(m1 p1) (m2 p2) (m3 p3 (m4 p4) (m5 p5) (m6 p6)]

Number of abc... = n1n2n3...
Number of m=p
So difference is in number of n and m
EDIT: Also, the order is not always the same for elements in each list (in List1:abc, in List2 acb)

I need as the result list [(a p1) (a p2) (a p3) (b p4) (b p5) (c p6)]

Edited by lastknownuser
##### Share on other sites

```(setq List1 '(a b c)
list2 '((a n1) (c n3) (b n2))
list3 '((n1 m1) (n1 m2) (n1 m3) (n2 m4) (n2 m5) (n3 m6))
list4 '((m1 p1) (m2 p2) (m3 p3) (m4 p4) (m5 p5) (m6 p6))
)

(mapcar
'(lambda (x / y z)
(cond
((setq y (cadr (assoc (car x) (mapcar 'reverse list3))))
(cond
((setq z (cadr (assoc y (mapcar 'reverse list2))))
)
)
)
(x)
)
)
list4
)

-->((A P1) (A P2) (A P3) (B P4) (B P5) (C P6)) ```

##### Share on other sites

Another, for any depth of lists -

```(defun foo ( kys lst )
(defun bar ( kys lst )
(if lst
(bar
(apply 'append
(mapcar
'(lambda ( x )
(mapcar '(lambda ( y ) (list (car x) y))  (baz (cadr x) (car lst)))
)
kys
)
)
(cdr lst)
)
kys
)
)
(defun baz ( key lst / itm )
(if (setq itm (assoc key lst))
(cons (cadr itm) (baz key (cdr (member itm lst))))
)
)
(bar (mapcar 'list kys kys) lst)
)```

```(setq list1 '(a b c)
list2 '((a n1) (c n3) (b n2))
list3 '((n1 m1) (n1 m2) (n1 m3) (n2 m4) (n2 m5) (n3 m6))
list4 '((m1 p1) (m2 p2) (m3 p3) (m4 p4) (m5 p5) (m6 p6))
)
_\$ (foo list1 (list list2 list3 list4))
((A P1) (A P2) (A P3) (B P4) (B P5) (C P6))```

##### Share on other sites

Maybe not a list but abc = 731 may be a useful way around problem.

```; Alpha2Number - Converts Alpha string into Number
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
;   Str\$ = String to convert
; Syntax example: (Alpha2Number "ABC") = 731
;-------------------------------------------------------------------------------
(defun Alpha2Number (Str\$ / Num#)
(if (= 0 (setq Num# (strlen Str\$)))
0
(+ (* (- (ascii (strcase (substr Str\$ 1 1))) 64) (expt 26 (1- Num#)))
(Alpha2Number (substr Str\$ 2))
)
)
)

;-------------------------------------------------------------------------------
; Number2Alpha - Converts Number into Alpha string
; Function By: Gilles Chanteau from Marseille, France
; Arguments: 1
;   Num# = Number to convert
; Syntax example: (Number2Alpha 731) = "ABC"
;-------------------------------------------------------------------------------
(defun Number2Alpha (Num# / Val#)
(if (< Num# 27)
(chr (+ 64 Num#))
(if (= 0 (setq Val# (rem Num# 26)))
(strcat (Number2Alpha (1- (/ Num# 26))) "Z")
(strcat (Number2Alpha (/ Num# 26)) (chr (+ 64 Val#)))
)
)
);defun Number2Alpha```

##### Share on other sites

Thank you all guys! This will be very usefull to me. I just need help with one more list adjustment. So the result is:
(("0322" "1556") ("6373" "1372") ("3293" "1555") ("0096" "1088") ("0096" "1089") ("0096" "1090") ("1692" "1091"))

And I would need a function to combine second elements if the first one is the same, so I would get this:
(("0322" ("1556")) ("6373" ("1372")) ("3293" ("1555")) ("0096" ("1088" "1089" "1090")) ("1692" ("1091")))

Edited by lastknownuser

## 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.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.