MJLM 发表于 2022-7-5 18:44:08

将列表分成两个子列表

我想把一个列表分成两个子列表,如下所示:
 
列表:(a b c d e f g)
 
然后得到
 
L1作为(a b c d)和L2作为(e f g)。
 
很容易得到第二个L2和(成员e ls),坦率地说,我可以得到带有“foreach”和“cons”函数的L1,但这似乎是一个代码。有没有像“member”这样的函数可以更快地获得L1,并且代码更少?可能是vl函数?
 
非常感谢。

a_67vdub 发表于 2022-7-5 18:49:40

在那里投几个反转怎么样?因此,获得L1就像获得L2:
 
(反向(成员d(反向ls)))
 
我绝对不是Lisp程序专家,所以对我的建议持保留态度。

ymg3 发表于 2022-7-5 18:57:55

Gile Chanteau的这一职能:
 
您提供的n是第一个列表中的元素数
我在名单上。
 

defun breakAt (n l / r)
(while (and l (< 0 n))
   (setq r (cons (car l) r)
l (cdr l)
n (1- n)
   )
)
(list (reverse r) l)
)

 
也可以使用函数trunc
作者:Gile Chanteau
 
您提供expr和列表。在你的情况下,expr将是e
 

(defun trunc        (expr lst)
(if (and lst
   (not (equal (car lst) expr))
   )
   (cons (car lst) (trunc expr (cdr lst)))
)
)

 
因此,您的第一个列表是:
 
(setq L1 (trunc elst))
 
第二个:
3)

Lee Mac 发表于 2022-7-5 19:00:53

另一个:
(defun breakatitem ( x l / r )
   (setq l (vl-member-if '(lambda ( y ) (setq r (cons y r)) (equal x y)) l))
   (list (reverse (cdr r)) l)
)
_$ (breakatitem 'e '(a b c d e f g))
((A B C D) (E F G))
_$ (breakatitem 'h '(a b c d e f g))
((A B C D E F) nil)
_$ (breakatitem 'a '(a b c d e f g))
(nil (A B C D E F G))

hanhphuc 发表于 2022-7-5 19:06:17

@ymg和LM
 
对不起,我的不在主题范围内,这段代码只是平分。
只是为了分享,如果其他人看到这个相似的线程。
用opt T或nil按除法或长度拆分

(defun split (lst len opt / ls l i) ; opt, T= by division or nil=by length
(setq i 1 l '() len (if opt (/ (length lst) len) len))
(while lst
   (setq l (appendl (list(car lst))))
   (if
   (zerop (rem i len))
(setq ls (cons l ls) l nil)
   )
   (setq i (1+ i) lst (cdr lst))
) ;_ end of foreach
(if l
   (append (reverse ls) (listl))
   (reverse ls)
) ;_ end of if
) ;_ end of defun

 
测试:

(setq a '(1 2 3 4 5 6 7 8 9 0))

; split by division
(split a 3 t)
;((1 2 3) (4 5 6) (7 8 9) (0))
(split a 2 t)
;((1 2 3 4 5) (6 7 8 9 0))
(split a 1 t)
;((1 2 3 4 5 6 7 8 9 0))

; split by length
(split a 3 nil)
;((1 2 3) (4 5 6) (7 8 9) (0))
(split a 2 nil)
;((1 2) (3 4) (5 6) (7(9 0))
(split a 1 nil)
;((1) (2) (3) (4) (5) (6) (7) ( (9) (0))

附言:代码标签字体小写“L”很像数字“1”,一眼就辨不出来?!

ziele_o2k 发表于 2022-7-5 19:08:51

你好
 
有人能帮我解决我的问题吗?
例如,我有如下列表:
(a 12 3 4 a 5 6 7 8)
我想得到这样的东西:
((1 2 3 4) (5 6 7 8 ))

Tharwat 发表于 2022-7-5 19:15:44

 
程序员应该考虑什么标准才能得出这个结果?

ziele_o2k 发表于 2022-7-5 19:17:09

我想用分隔符来划分列表。
 
更好的例子如下:
1) (setq mylist (list "sep" "a" "b" "c" "sep" "d" "e" "f")
or
2) (setq mylist (list "a" "b" "c" "sep" "d" "e" "f")
or
3) (setq mylist (list "a" "b" "c" "d" "e" "sep" "f")
现在我想写一个函数,用它通过分隔符分割mylist:
(List_sep mylist "sep")
函数List_sep的预期结果:
1) (("a" "b" "c")("d" "e" "f"))
or
2) (("a" "b" "c")("d" "e" "f"))
or
3) (("a" "b" "c" "d" "e")("f"))
 
我写了lisp,但它不起作用:

(defun LIST_sep (lista Sep / el res)
(setq el "")
(foreach % lista
        (if (wcmatch % (strcat Sep "*"))
                (setq
                        res (cons el res)
                        el ""
                )
                (if (atom el)
                        (setq el (cons % (list el)))
                        (setq el (cons % el))
                )
        )
)
(setq res (cons el res))
(vl-remove "" res)
)

Tharwat 发表于 2022-7-5 19:22:47

试试这个:
 

(defun _div:by:sep (lst sep / n f out all)
;; Tharwat 19.08.2015        ;;
(setq n 0)
(repeat (length lst)
   (while (and (/= (setq f (nth n lst)) sep)
               f
               )
   (setq out (append out (list f))
         n   (1+ n)
         )
   )
   (if out
   (setq all (cons out all)
         out nil
         )
   )
   (setq n (1+ n))
   )
(reverse all)
)

ziele_o2k 发表于 2022-7-5 19:25:47

非常感谢。
我必须做出一些更改,lisp将在如下所示的情况下工作:
(setq mylist (list "sepa" "a" "b" "c" "sepa" "d" "e" "f"))
(_div:by:sep mylist "se")
结果:
(("a" "b" "c")("d" "e" "f"))
页: [1] 2
查看完整版本: 将列表分成两个子列表