LISP测试元素是否为
我正在寻找一个lisp来测试一个元素在列表中是否重复以及重复了多少次。例如
(A B C D A 1 2 12 2 XX)
A=2
2 = 2
我写了这个,但是。。。
谢谢 也许下面的代码可以帮助您:
(setq ItemCounter 0) ;occurrencies counter set to 0
(while (member theItem theList) ;test if item belong to list
(setq theList (cdr (member theItem theList)) ;if found retain list after occurrence
ItemCounter (1+ ItemCounter)) ; and index the counter
)
当做 这是我不久前写的。。。
(defun AT:ListDuplicates (lst)
;; Return all duplicates within list
;; lst - List to process
;; Alan J. Thompson, 11.01.09
(vl-remove-if-not (function (lambda (x) (member x (cdr (member x lst))))) lst)
)eg。
(at:listduplicates '(1 2 3 4 5 1 2 3)) -> (1 2 3 1 2 3)
这是我的2美分
(defun count_occurs (lst)
(if (car lst)
(cons (cons (car lst)
(length (vl-remove-if-not
(function (lambda (x)
(eq x (car lst))))
lst))
)
(count_occurs
(vl-remove-if
(function (lambda (x)
(eq x (car lst))))
lst)
)
)
)
)
~'J'~ 我误解了预期结果。。。
(defun _ListDuplicateCount (lst / c new)
;; Alan J. Thompson, 06.25.10
(foreach x (vl-remove-if-not (function (lambda (x) (member x (cdr (member x lst))))) lst)
(if (setq c (assoc x new))
(setq new (subst (cons x (+ (cdr c) 1)) c new))
(setq new (append new (list (cons x 1))))
)
)
) 谢谢fixo
谢谢Alan
谢谢马索
你们帮了大忙。 享受 有点晚了,但会加上我的5美分
(defun mip_MakeUniqueMembersOfListWithCount( lst / OutList head countelt)
;; ===== mip_MakeUniqueMembersOfListWithCount =====
;; Removes the same (duplicate) items from the list
;; Quantifying the number of occurrences of an element
;; Use:
;; (Mip_MakeUniqueMembersOfListWithCount '(1 2 3 1 2 3 1 1 2 2))
;; Return ((1. 4) (2. 4) (3. 2))
(while lst
(setq head (car lst)
countelt 0
lst (vl-remove-if '(lambda(pt)(if (equal pt head 1e-6)(setq countelt (1+ countelt)) nil)) lst)
OutList (append OutList (list (cons head countelt)))))
OutList
)
(defun _ListDuplicateCounter (lst / c new)
;; Alan J. Thompson, 06.26.10
(foreach x lst
(if (setq c (assoc x new))
(setq new (subst (cons x (1+ (cdr c))) c new))
(setq new (append new (list (cons x 1))))
)
)
) 嗨,艾伦,
I使用此代码
像这样
我得到了这个奇妙的结果
我需要区分重复奇数次的项目(实际上,重复一次、三次和五次的元素就足够了)。
使用命令“prc”的结果-我打印到文本文件
“1”重复3次
“2”重复3次
“3”重复3次
“4”重复1
“5”重复1
6排除且未打印。
谢谢:滚动:
页:
[1]
2