gpt4 book ai didi

algorithm - (emacs) 口齿不清 : search anything in a ((nested) list)

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:44:46 26 4
gpt4 key购买 nike

我需要找到一个特定的值,该值可以埋入一个深度嵌套的列表中,而且永远不会在同一个地方。甚至深度相同;这是列表的一种形式:

(setq my-list '(((partnum . 1) (type (TEXT . plain)) (body (charset UTF-8))
(disposition nil) (transfer-encoding QUOTED-PRINTABLE))
((partnum . 2) (type (TEXT . html)) (body (charset UTF-8))
(disposition nil) (transfer-encoding QUOTED-PRINTABLE))))

现在我需要检索“字符集”的值;有的话第一个。在这个配置中,很容易:

(car (cdr (cadr (third (car my-list)))))
=> UTF-8

但这是我确切知道“ body ”细胞在哪里的时候。

我尝试像这样递归地使用 mapcar :

(defun search-rec (list)
(mapcar
(lambda (x)
(if (listp x)
(search-rec x)
(message "OY %s" x)))
list))

但每次,当递归命中第一个 cons 单元的第一个原子时,我都会收到错误 (wrong-type-argument listp 1)。我想我的问题真的是它是什么:

如何在列表中搜索?

编辑 现在列表看起来像这样,“字符集”仍然在(正文)中(告诉过你那是唯一不变的东西)并且不再找到 :(

(setq my-list '(((partnum . 1)
(1.1 (type (TEXT . plain)) (body (charset UTF-8))
(disposition nil) (transfer-encoding 7BIT))
(1.2 (type (TEXT . html)) (body (charset UTF-8))
(disposition nil) (transfer-encoding 7BIT))
(type . alternative) (body (boundary e89a8fb2067eba300404c63c5f7f))
(disposition nil) (transfer-encoding nil))
((partnum . 1.1) (type (TEXT . plain)) (body (charset UTF-8))
(disposition nil) (transfer-encoding 7BIT))
((partnum . 1.2) (type (TEXT . html)) (body (charset UTF-8))
(disposition nil) (transfer-encoding 7BIT))
((partnum . 2) (type (IMAGE . x-xpixmap)) (body (name ladybug.xpm))
(disposition nil) (transfer-encoding BASE64))))

这里编辑一些更多的 IRL 示例:

    (setq my-list haystack-list)
(setq my-needle (tree-assoc 'charset my-list))
(message "
-------------\n
- my-list: %s\n
- my-needle: %s\n
-------------\n" my-list my-needle)

产生:


  • my-list: ((TEXT plain (charset UTF-8) nil nil 7BIT 260 18 nil nil nil) (TEXT html (charset UTF-8) nil nil QUOTED-PRINTABLE 738 17 nil nil nil) 替代(边界 e89a8fb1f8061a6be404c70a24a0)无无)

  • 我的针:无


另一方面:

(tree-assoc 'charset '((TEXT plain (charset UTF-8) nil nil 7BIT 260 18 nil nil nil)
(TEXT html (charset UTF-8) nil nil QUOTED-PRINTABLE 738 17 nil nil nil)
alternative (boundary e89a8fb1f8061a6be404c70a24a0) nil nil))
=>(charset UTF-8)

真的,我不知道这里发生了什么:有人可能会争辩“这个干草堆列表是什么,它从哪里来?”但它相关吗?我正在处理这个 haystack-list 的副本(我的列表),那么是什么给出了这些不同的结果?列表的引用?伙计们,我真的迷路了

注意(此行为(在直接评估中有效,但在 defun/let 生产情况下无效)出现在所有给出的解决方案中)

编辑:我最终提取了找到的第一个列表,然后从该列表中提取(而不是搜索)元素。我证明更快;当然,此时您可以说“我的元素总是在找到的第一个列表中);感谢大家,我从这一切中学到了很多东西。

最佳答案

看起来你想要 Association Lists 的树模拟.通过遵循 assoc 函数的约定,它检索包含给定键作为其头部的列表元素,这里是一个适用于树的 assoc 版本:

(defun tree-assoc (key tree)
(when (consp tree)
(destructuring-bind (x . y) tree
(if (eql x key) tree
(or (tree-assoc key x) (tree-assoc key y))))))

示例:

(let ((my-list '(((partnum . 1)
(1.1 (type (TEXT . plain)) (body (charset UTF-8))
(disposition nil) (transfer-encoding 7BIT))
(1.2 (type (TEXT . html)) (body (charset UTF-8))
(disposition nil) (transfer-encoding 7BIT))
(type . alternative) (body (boundary e89a8fb2067eba300404c63c5f7f))
(disposition nil) (transfer-encoding nil))
((partnum . 1.1) (type (TEXT . plain)) (body (charset UTF-8))
(disposition nil) (transfer-encoding 7BIT))
((partnum . 1.2) (type (TEXT . html)) (body (charset UTF-8))
(disposition nil) (transfer-encoding 7BIT))
((partnum . 2) (type (IMAGE . x-xpixmap)) (body (name ladybug.xpm))
(disposition nil) (transfer-encoding BASE64)))))
(tree-assoc 'charset my-list))

=> (charset UTF-8)

关于algorithm - (emacs) 口齿不清 : search anything in a ((nested) list),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11912027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com