Elisp:如何使用字符串键从关联列表中删除元素 [英] Elisp: How to delete an element from an association list with string key
问题描述
现在这样做很好:
(setq al '((a . "1") (b . "2")))
(assq-delete-all 'a al)
在我的应用程序中使用字符串作为键:
But I'm using strings as keys in my app:
(setq al '(("a" . "foo") ("b" . "bar")))
这样做无效:
(assq-delete-all "a" al)
我认为这是因为字符串对象实例是不同的(?)
I think that's because the string object instance is different (?)
那么我应该如何从关联中删除一个字符串键的元素清单?或者我应该放弃使用符号作为键,并在需要时将其转换为字符串?
So how should I delete an element with a string key from an association list? Or should I give up and use symbols as keys instead, and convert them to strings when needed?
推荐答案
如果你知道可以您只能在列表中单个匹配的条目,也可以使用以下格式:
If you know there can only be a single matching entry in your list, you can also use the following form:
(setq al (delq (assoc <string> al) al)
请注意, setq
(您的示例代码中缺少)对列表上的删除操作非常重要,否则当已删除的元素恰好是列表中的第一个时,操作将失败。
Notice that the setq
(which was missing from your sample code) is very important for `delete' operations on lists, otherwise the operation fails when the deleted element happens to be the first on the list.
这篇关于Elisp:如何使用字符串键从关联列表中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!