transcad 发表于 2022-7-6 08:12:28

a组列表

我有一个这样的列表(1 2 3 4 5 6 7 8 9 10 11…)
我如何在这个((1 2 3)(4 5 6)(7 8 9)…)中转换它
我的意思是,把三个元素分组。。?

MSasu 发表于 2022-7-6 08:26:44

不是最优雅的解决方案:

(setq MyNewList '()                                 ;empty lists to build on
   SubList   '())
(foreach item '(1 2 3 4 5 6 7 8 9 10 11)
(if (= (length SubList) 3)                           ;test if set is done (3 items)
(setq MyNewList (append MyNewList (list SubList))   ;add done set to main list
       SubList   '())                              ;reset
)
(setq SubList (append SubList (list item)))          ;build set of 3 items
)
(setq MyNewList (append MyNewList (list SubList)))    ;add last set, may not be complete
当做
米尔恰

prakashreddy 发表于 2022-7-6 08:47:53

使用这个

(defun GroupByNum ( l n / a b ) ; l as list & n as number
(while l
(repeat n
(setq a (cons (car l) a) l (cdr l))
)
(setq b (cons (reverse a) b) a nil)
)
(reverse b)
)

Stefan BMR 发表于 2022-7-6 08:52:06

从列表到点列表分组
(defun lst3 (lst)
   (if lst
   (cons
       (list (car lst) (cadr lst) (caddr lst))
       (lst3 (cdddr lst)))
   )
   )
(lst3 '(1 2 3 4 5 6 7 8 9 10 11 12)) -> ((1 2 3) (4 5 6) (7 8 9) (10 11 12))
(lst3 '(1 2 3 4 5 6 7 8 9 10 11)) -> ((1 2 3) (4 5 6) (7 8 9) (10 11 nil))

transcad 发表于 2022-7-6 09:02:26

谢谢大家!很好,斯特凡!

Lee Mac 发表于 2022-7-6 09:12:41

以下是我的解决方案:
 
http://lee-mac.com/groupbynum.html
 
为什么你从我的代码prakashreddy中删除了标题?
页: [1]
查看完整版本: a组列表