Jump to content

Combining Lists


JSYoung81

Recommended Posts

I am struggling over combining 2 different lists. Elements from list 1 contains, lets say, 1 2 3, while elements from list 2 are A B C D. The master list would look something like this:

1 A

1 B

1 C

1 D

2 A

2 B

2 C

 

And so on. I think I need to use the FOREACH function, but just not sure. Any help would be great.

Link to comment
Share on other sites

You can use nth to get each individual value and then say Strcat to make the new value.

 

(setq list1 (list 1 2 3 4 5 6 7))
(setq list2 (list "a" "b" "c" "d" "e" "f" "g"))
(setq len (length list1)) ; I would check here that list1 is same number as list2
(setq x 0)
(repeat len
(Princ (strcat "\n" (rtos (nth x list1) 2 0) " " (nth x list2))) 
(setq x (+ x 1))
)

Link to comment
Share on other sites

Not sure I understand the desired output, so here are two adaptations:

 

(mapcar
 (function
   (lambda (x y) (cons x y))
 )
 '(1 1 1 1 2 2 2)
 '("A" "B" "C" "D" "A" "B" "C")
)

(mapcar
 (function
   (lambda (x y) (list x y))
 )
 '(1 1 1 1 2 2 2)
 '("A" "B" "C" "D" "A" "B" "C")
)

 

 

 

More information is needed.

Link to comment
Share on other sites

RANGE_LIST = [["RANGE 1" 1.0 ] ["RANGE 2" 2.0 ] ["RANGE 3" 3.0 ] ["RANGE 4" 4.0 ] ["RANGE 5" 5.0 ]]

PART_SIZE_LIST = [["AC" 0.1] ["PVC" 0.5]]

 

End result would be a list that would look like this:

 

MASTER_LIST = [["AC" 0.1 "RANGE 1" 1.0 ] ["AC" 0.1 "RANGE 2" 2.0 ].......

 

Basically I want to add each element of the range_list to each element of the part_size_list to create a brand new element in the master_list.

Link to comment
Share on other sites

Try this it is a variation on using nth

 

(setq list1 (list 1 2 3 4 5 6 7))
(setq list2 (list "a" "b" "c" "d" "e" "f" "g"))
(setq len (length list1)) ; I would check here that list1 is same number as list2
(setq x 0)
(setq Y (- (Getint "\nEnter item Number from list2") 1))
(repeat len
(Princ (strcat "\n"  (rtos (nth x list1)2  0) " " (nth Y list2))) ; dont need rtos if strings 
(setq x (+ x 1))
)

Link to comment
Share on other sites

Something like this, perhaps...

(setq a '(("RANGE 1" 1.0)("RANGE 2" 2.0)("RANGE 3" 3.0)("RANGE 4" 4.0)("RANGE 5" 5.0))
     b '(("AC" 0.1) ("PVC" 0.5))
)
(foreach x b
 (setq n -1)
 (while (setq y (nth (setq n (1+ n)) a))
   (setq c (cons (append x y) c))
 )
)
(setq c (reverse c))

 

Henrique

Link to comment
Share on other sites

Maybe:

(setq a '(("RANGE 1" 1.0)("RANGE 2" 2.0)("RANGE 3" 3.0)("RANGE 4" 4.0)("RANGE 5" 5.0))
     b '(("AC" 0.1) ("PVC" 0.5))
)

(mapcar '(lambda ( x ) (mapcar '(lambda ( y ) (append x y)) a)) b)

Or, if a single list is required:

(apply 'append (mapcar '(lambda ( x ) (mapcar '(lambda ( y ) (append x y)) a)) b))

Link to comment
Share on other sites

Or, if a single list is required:

(apply 'append (mapcar '(lambda ( x ) (mapcar '(lambda ( y ) (append x y)) a)) b))

 

 

Nicely done. :)

 

Henrique

Link to comment
Share on other sites

Maybe:

(setq a '(("RANGE 1" 1.0)("RANGE 2" 2.0)("RANGE 3" 3.0)("RANGE 4" 4.0)("RANGE 5" 5.0))
     b '(("AC" 0.1) ("PVC" 0.5))
)

(mapcar '(lambda ( x ) (mapcar '(lambda ( y ) (append x y)) a)) b)

Or, if a single list is required:

(apply 'append (mapcar '(lambda ( x ) (mapcar '(lambda ( y ) (append x y)) a)) b))

 

 

 

Lee as always you are brilliant, works just as I need it to, for the most part, just have to reverse a few things. But perfect! Thank you very much!

Link to comment
Share on other sites

Lee as always you are brilliant, works just as I need it to, for the most part, just have to reverse a few things. But perfect! Thank you very much!

 

Thank you for your flattering comments JSYoung, I'm happy to help :)

Link to comment
Share on other sites

No problem, I am sure I am going to need a bit more help by the time I am done this LISP. The entire idea behind this LISP is to be able to have a complete pipe depth list, sorted by ranges and different types of pipe used.

Link to comment
Share on other sites

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