删除一对特定项目
我需要一些代码从列表中删除2个特定的连续项。例:从列表(a b c d e f g h)中,我需要删除a和b并获取
'(c d e f g h),但仅当b在a之后。如果a或b是列表中的独立元素,则不应删除它们。
任何帮助都将不胜感激。非常感谢。 使用“第n步”遍历列表查找“a”If,然后获取下一个第n个If b,然后从列表中删除
(setq mylst (list "x" "a" "b" "C" "d"))
(setq numb (length mylst))
(setq x 0)
(repeat numb
(princ (nth x mylst))
(setq x (+ x 1))
)
不指望重复元素,所以
编辑:如果意图是“配对后列表的其余部分”
(Defun rem2rest (e1 e2 lst)
(mapcar '(lambda (x y)
(if (and (eq x e1)(eq y e2))
(setq lst (cdr (member y (member x lst))))
)
)
lst (cdr lst))
lst
)
小心pbe
_$ (rem2 'a 'b '(c b d e a b c))
(D E A B C)
如果我理解了OP,也许:
(defun rem2 ( e1 e2 lst )
(apply 'append
(mapcar
(function
(lambda ( a b c )
(if
(not
(or (and (equal a e1) (equal b e2))
(and (equal b e1) (equal c e2))
)
)
(list b)
)
)
)
(cons nil lst)
lst
(append (cdr lst) '(()))
)
)
)
_$ (rem2 'a 'b '(c b d e a b c))
(C B D E C)
现在,你是对的“从列表中删除一对特定项目”。。我认为这是配对后的列表中的其余部分
快速修复
(Defun rem2pBe (e1 e2 lst / l lst l2)
(while (and (setq x (car lst))(setq b (cdr lst))(null l2))
(setq y (car b))
(if (and (eq x e1)(eq y e2))
(setq l2 (append (reverse l)
(cdr (member y (member x lst)))) l3 l2)
(setq l (cons x l)))
(setq lst b)
)
(if (> (length lst) 1) (rem2pBe e1 e2 l2) l3)
)
编辑:递归版本 小心pBe。。。
抱歉
不用担心,李,现在我坚信“欲速则不达”。我会关注并重新发布代码
编辑:差不多了。现在我只需要找出重复对。。。。
编辑:代码更新(递归) 7 很好Stefan 谢谢你,李
页:
[1]
2