Author Topic: Combining two lists by key number  (Read 136 times)

0 Members and 1 Guest are viewing this topic.

Lastknownuser

  • Mosquito
  • Posts: 5
Combining two lists by key number
« on: October 07, 2021, 01:43:26 PM »
I need help with list combining. Example, I have two lists:
("1" "2" "3")
(("1" "a") ("2" "b") ("2" "c") ("2" "d") ("3" "e"))

And I'd like to get: (("1" ("1" "a")) ("2" ("2" "b") ("2" "c") ("2" "d")) ("3" ("3" "e"))
So all with the same number would be nested like that, (number (number letter1) (number letter2)...

So far I have this, but I can't figure out how to group all with the same number in just one list: ("2" ("2" "b") ("2" "c") ("2" "d")) instead of  ("2" ("2" "b")) ("2" ("2" "c")) ("2" ("2" "d")) that I'm getting with my code

Code: [Select]
(defun c:test ( / n m len1 len2 list3 list4)
  (setq list1 (list "1" "2" "3"))
(setq list2 (list '("1" "a") '("2" "b") '("2" "c") '("2" "d") '("3" "e") ))
        (setq n 0)
        (setq len1 (length list1))
        (setq len2 (length list2))
        (while (< n len1)
    (setq m 0)
    (while (< m len2)
    (if (= (nth n list1) (nth 0 (nth m list2)) )
(progn
   (setq list3 (list (nth n list1) (nth m list2)))
   (setq list4 (cons list3 list4))
);progn
);if
(setq m (+ m 1))
);while
(setq n (+ n 1))
);while
(reverse list4)
);defun
« Last Edit: October 07, 2021, 02:17:24 PM by Lastknownuser »

Lee Mac

  • Seagull
  • Posts: 12526
  • London, England