a组列表
我有一个这样的列表(1 2 3 4 5 6 7 8 9 10 11…)我如何在这个((1 2 3)(4 5 6)(7 8 9)…)中转换它
我的意思是,把三个元素分组。。? 不是最优雅的解决方案:
(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
当做
米尔恰 使用这个
(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)
)
从列表到点列表分组
(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)) 谢谢大家!很好,斯特凡! 以下是我的解决方案:
http://lee-mac.com/groupbynum.html
为什么你从我的代码prakashreddy中删除了标题?
页:
[1]