乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 82|回复: 19

[编程交流] LISP测试元素是否为

[复制链接]

34

主题

123

帖子

90

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
175
发表于 2022-7-6 10:50:08 | 显示全部楼层 |阅读模式
我正在寻找一个lisp来测试一个元素在列表中是否重复以及重复了多少次。
 
例如
(A B C D A 1 2 12 2 XX)
 
A=2
2 = 2
 
我写了这个,但是。。。
 
 
 
 
谢谢
回复

使用道具 举报

35

主题

2471

帖子

2447

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
174
发表于 2022-7-6 10:55:17 | 显示全部楼层
也许下面的代码可以帮助您:
 
  1. (setq ItemCounter 0)                            ;occurrencies counter set to 0
  2. (while (member theItem theList)                 ;test if item belong to list
  3. (setq theList (cdr (member theItem theList))   ;if found retain list after occurrence
  4.       ItemCounter (1+ ItemCounter))            ; and index the counter
  5. )

 
当做
回复

使用道具 举报

54

主题

3755

帖子

3583

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
438
发表于 2022-7-6 10:58:51 | 显示全部楼层
这是我不久前写的。。。
 
  1. (defun AT:ListDuplicates (lst)
  2. ;; Return all duplicates within list
  3. ;; lst - List to process
  4. ;; Alan J. Thompson, 11.01.09
  5. (vl-remove-if-not (function (lambda (x) (member x (cdr (member x lst))))) lst)
  6. )
eg。
  1. (at:listduplicates '(1 2 3 4 5 1 2 3)) -> [color=Red](1 2 3 1 2 3)[/color]
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2022-7-6 11:02:47 | 显示全部楼层
 
这是我的2美分
  1. (defun count_occurs (lst)
  2. (if (car lst)
  3.    (cons (cons (car lst)
  4. (length (vl-remove-if-not
  5.     (function (lambda (x)
  6.          (eq x (car lst))))
  7.     lst))
  8. )
  9.   (count_occurs
  10.     (vl-remove-if
  11.       (function (lambda (x)
  12.     (eq x (car lst))))
  13.       lst)
  14.     )
  15.   )
  16.    )
  17. )

 
~'J'~
回复

使用道具 举报

54

主题

3755

帖子

3583

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
438
发表于 2022-7-6 11:04:47 | 显示全部楼层
我误解了预期结果。。。
 
  1. (defun _ListDuplicateCount (lst / c new)
  2. ;; Alan J. Thompson, 06.25.10
  3. (foreach x (vl-remove-if-not (function (lambda (x) (member x (cdr (member x lst))))) lst)
  4.    (if (setq c (assoc x new))
  5.      (setq new (subst (cons x (+ (cdr c) 1)) c new))
  6.      (setq new (append new (list (cons x 1))))
  7.    )
  8. )
  9. )
回复

使用道具 举报

34

主题

123

帖子

90

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
175
发表于 2022-7-6 11:07:12 | 显示全部楼层
谢谢fixo
谢谢Alan
谢谢马索
 
你们帮了大忙。
回复

使用道具 举报

54

主题

3755

帖子

3583

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
438
发表于 2022-7-6 11:11:54 | 显示全部楼层
享受
回复

使用道具 举报

VVA

1

主题

308

帖子

308

银币

初来乍到

Rank: 1

铜币
8
发表于 2022-7-6 11:14:16 | 显示全部楼层
有点晚了,但会加上我的5美分
  1. (defun mip_MakeUniqueMembersOfListWithCount  ( lst / OutList head countelt)
  2. ;; ===== mip_MakeUniqueMembersOfListWithCount =====
  3. ;; Removes the same (duplicate) items from the list
  4. ;; Quantifying the number of occurrences of an element
  5. ;; Use:
  6. ;; (Mip_MakeUniqueMembersOfListWithCount '(1 2 3 1 2 3 1 1 2 2))
  7. ;; Return ((1. 4) (2. 4) (3. 2))  
  8. (while lst
  9.    (setq head (car lst)
  10.   countelt 0
  11.          lst (vl-remove-if '(lambda(pt)(if (equal pt head 1e-6)(setq countelt (1+ countelt)) nil)) lst)
  12.          OutList (append OutList (list (cons head countelt)))))
  13. OutList
  14. )
回复

使用道具 举报

54

主题

3755

帖子

3583

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
438
发表于 2022-7-6 11:18:14 | 显示全部楼层
  1. (defun _ListDuplicateCounter (lst / c new)
  2. ;; Alan J. Thompson, 06.26.10
  3. (foreach x lst
  4.    (if (setq c (assoc x new))
  5.      (setq new (subst (cons x (1+ (cdr c))) c new))
  6.      (setq new (append new (list (cons x 1))))
  7.    )
  8. )
  9. )
回复

使用道具 举报

34

主题

123

帖子

90

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
175
发表于 2022-7-6 11:19:48 | 显示全部楼层
嗨,艾伦,
I使用此代码
像这样
我得到了这个奇妙的结果
我需要区分重复奇数次的项目(实际上,重复一次、三次和五次的元素就足够了)。
 
使用命令“prc”的结果-我打印到文本文件
 
“1”重复3次
“2”重复3次
“3”重复3次
“4”重复1
“5”重复1
 
6排除且未打印。
 
谢谢:滚动:
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-6 17:14 , Processed in 0.609811 second(s), 72 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表