Lee Mac 发表于 2022-7-6 14:42:59

从列表中删除项目

我只是想知道是否有比我目前拥有的更好的方法:
 

(defun remove_items(lst items)
(vl-remove-if
   '(lambda (j) (member (vl-position j lst) items)) lst))

David Bethel 发表于 2022-7-6 14:52:30

李,
 
下面是一种非常古老且相当缓慢的从列表中删除原子的方法:

(defun delatom (a l / tmp)
(while l
    (if (not (equal a (car l) 1e-11))
      (setq tmp (cons (car l) tmp)))
    (setq l (cdr l)))
(reverse tmp))

(setq tl '((0 . "LINE") 234 "ABC" 456.78 sym (10 0 0 1) "DEF"))

(prin1 (delatom "ABC" tl))
和时间测试程序:
 
大卫

Lee Mac 发表于 2022-7-6 14:57:19

谢谢David,
 
我最初想过使用(setq-lst(cdr-lst))方法在列表中乱序,但我发现这只对删除单个项目有用,如果列表中有重复项,它将删除重复项的两个实例,而不是请求的一个实例。
 
PS,我喜欢时间测试程序。。。非常新颖。

taner 发表于 2022-7-6 14:59:27

(defun remove-lst-items (lst itemlst / i)
(setq i -1)
(vl-remove-if
   '(lambda (x)
      (member (setq i (1+ i))
       itemlst
      )
    )
   lst
)
)

Lee Mac 发表于 2022-7-6 15:09:05

^^漂亮的Taner,很有创意

CAB 发表于 2022-7-6 15:15:23

其他方法:
http://www.theswamp.org/index.php?topic=28100.msg336941#msg336941

Lee Mac 发表于 2022-7-6 15:20:07

哇!
 
沼泽地的方法似乎比这里先进得多。。。大量递归函数(它们让我头疼)。

Se7en 发表于 2022-7-6 15:22:49

不,不要让它受伤。递归是最简单的循环。

Lee Mac 发表于 2022-7-6 15:31:51

我只是发现当使用递归过程时,很难计算出函数在做什么。

Se7en 发表于 2022-7-6 15:33:41

也许一些带有注释的代码会有所启发。
 
通过递归,我们可以通过一个列表进行迭代。例如,我们可以构建一个简单的递归过程来复制列表。
 
阅读评论以更好地理解操作过程中的“流程”。
 

(defun copy-tree (x)
;; Return an exact copy of a given list.
;; (nested lists are this procedures speciality.)
;;
;; EX: (copy-tree '(1 2 (3 (4 5) 6 (nil)) 7 (()))
;;> (1 2 (3 (4 5) 6 (nil)) 7 (())
(if (atom x)                   ; If "x" is only an item, not a list,
   x                            ; return the item...else,
   (cons (copy-tree (car x))    ; build a list, sending the next item
                              ; back thru itself along with
         (copy-tree (cdr x))    ; the remander of the items,
                              ; via backtrace.
                              ;
                              ; NOTE:
                              ; last fuction `cons' returns assembled
                              ; list of items or exact copy of what
                              ; was passed to this procedure.
         )
   )
)

 
利用上面的内容,我们可以构建一个稍微修改的版本来证明该列表中的数字。
 
5
 
这有帮助吗?
页: [1] 2
查看完整版本: 从列表中删除项目