JSYoung81 发表于 2022-7-5 21:20:03

组合列表

我正在努力合并两个不同的列表。比如说,列表1中的元素包含1、2、3,而列表2中的元素是B、C、D。主列表如下所示:
1 A
1 B
1 C
1 D
2 A
2 B
2 C
 
等等我想我需要使用FOREACH函数,但只是不确定。任何帮助都会很好。

BIGAL 发表于 2022-7-5 21:26:37

您可以使用nth获得每个单独的值,然后使用Strcat生成新值。
 

(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))
)

JSYoung81 发表于 2022-7-5 21:34:12

它们是不相等的列表

BlackBox 发表于 2022-7-5 21:36:55

我不确定我是否理解所需的输出,因此这里有两种自适应:
 

(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")
)

 
 
 
需要更多信息。

JSYoung81 发表于 2022-7-5 21:40:33


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]]
 
最终结果将是一个如下所示的列表:
 

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

 
基本上,我想把range_列表中的每个元素添加到part_size_列表中的每个元素,在master_列表中创建一个全新的元素。

BIGAL 发表于 2022-7-5 21:47:41

试试这个,这是使用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)20) " " (nth Y list2))) ; dont need rtos if strings
(setq x (+ x 1))
)

hmsilva 发表于 2022-7-5 21:54:09

也许是这样的。。。

(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))

 
亨里克

Lee Mac 发表于 2022-7-5 21:57:42

也许 吧:
(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)
或者,如果需要单个列表:
(apply 'append (mapcar '(lambda ( x ) (mapcar '(lambda ( y ) (append x y)) a)) b))

hmsilva 发表于 2022-7-5 22:00:44

 
 
做得好。
 
亨里克

Lee Mac 发表于 2022-7-5 22:05:08

 
谢谢
页: [1] 2
查看完整版本: 组合列表