明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4537|回复: 15

怎么删掉表中连续相同的元素,谢谢

  [复制链接]
发表于 2009-7-27 16:02:00 | 显示全部楼层 |阅读模式
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2009-7-27 17:24:00 | 显示全部楼层
(defun test (lst / a lst2)
  (while (setq a (car lst) lst2 (cons a lst2) lst (vl-remove a lst)))
  (reverse lst2)
)
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2009-7-27 17:29:00 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2009-7-27 17:48:00 | 显示全部楼层
本帖最后由 作者 于 2009-7-27 20:00:59 编辑

:)
以下两段是我写的代码,写的不怎么样,你试试看行不,只能1维消重,多维的大概得递归吧
  1. (defun test(lst / lst1 i res)
  2. (setq lst1 (cons (list (car lst)) lst) i -1)
  3. (foreach x lst
  4.   (setq i (1+ i))
  5.   (if (/= x (nth i lst1)) (setq res (append res (list x))))
  6. ) )
  7. res
  8. )
  1. (defun test1(lst / lst1 i res)
  2. (setq lst1 (cdr lst) i -1)
  3. (foreach y lst1
  4.   (setq i (1+ i))
  5.   (if (/= y (nth i lst)) (setq res (append res (list y))))
  6. )
  7. (cons (car lst) res)
  8. )
在咨询了Evgeniy大师之后,他的回答是
1. (ACET-LIST-REMOVE-ADJACENT-DUPS lst )

2. 多维递归的(要看楼主是否需要递归了)
  1. (defun rem-adj2 (l)
  2.   (if (cdr l)
  3.     (if (eq (car l) (cadr l))
  4.       (rem-adj2 (cdr l))
  5.       (cons (car l) (rem-adj2 (cdr l)))
  6.     )           
  7.     l
  8.   )           
  9. )
发表于 2009-7-27 18:00:00 | 显示全部楼层

我回复了

(defun tt(o / i n j)(while (< (1+ (setq i (if (not i) -1 (1+ i)))) (length o))(while (= j (setq j (nth (setq i (1+ i)) o))))(setq n (append n (list j))))(cons (car o) n))

命令: (tt '(a b c c c d c o))
(A B C D O)

发表于 2009-7-27 19:17:00 | 显示全部楼层
qjchen发表于2009-7-27 17:48:00:)一写的很烂的代码,你试试看行不,只能1维消重,多维的大概得递归吧(defun test(lst / lst1 i res) (setq lst1 (cons (list (car lst)) lst) i -1) (foreach x lst  (setq i

发表于 2009-7-27 19:20:00 | 显示全部楼层
  1. ;清除列表中连续重复元素
  2. ;'( 1 2 3 1 1 nil nil nil 4 4 5 6 nil nil) -> '( 1 2 3 1 nil 4 5 6 nil)
  3. (defun EF-List-Fix (lst1 / e lst2 )
  4.   (setq lst2 (list (setq e (car lst1))))
  5.   (while (setq lst1 (cdr lst1))
  6.     (if (not (equal e (car lst1)))
  7.       (setq lst2 (cons (car lst1) lst2))
  8.       )
  9.     (setq e (car lst1))
  10.     )
  11.   (reverse lst2)
  12.   )
我现在用的
发表于 2009-7-27 19:54:00 | 显示全部楼层
:)
问了Evgeniy,现在修正原来的代码了:)
  1. (defun rem-adj2 (l)
  2.   (if (cdr l)
  3.     (if (eq (car l) (cadr l))
  4.       (rem-adj2 (cdr l))
  5.       (cons (car l) (rem-adj2 (cdr l)))
  6.     )           
  7.     l
  8.   )           
  9. )
发表于 2009-7-28 08:22:00 | 显示全部楼层

测试结果如下:

(rem-adj2 '(nil nil 1 2 3 3 3 3 32 1 1 1 nil nil))
(nil 1 2 3 32 1 nil)

发表于 2009-7-28 08:29:00 | 显示全部楼层

(defun test (lst / a lst2)                                          
  (while (setq a (car lst) lst2 (cons a lst2) lst (vl-remove a lst)))
  (reverse lst2)                                                    
)  

测试结果如下: 

(test '(a b c c c d c o))

(a b c d o)                                                       

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-5-23 12:33 , Processed in 0.204105 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表