我有一个元素列表。每个元素的结构如下:
('symbol "string" int-score)
示例列表:
(list (list 'object1 "wabadu" 0.5)
(list 'object2 "xezulu" 0.6)
(list 'object1 "yebasi" 0.5)
(list 'object1 "tesora" 0.2))
我想检索特定符号的最大值。当我搜索符号时object2
,应该返回:
('object2 "xezulu" 0.6)
如果使用搜索object1
,我应该回来:
(('object1 "wabadu" 0.5) ('object1 "yebasi" 0.5))
我想收集特定对象的所有最高元素。我能做的是:假设上面的列表是下面使用的列表,并且我正在搜索object1
。我可以检索特定对象的所有元素:
(loop for element in list
when (equal 'object1 (first element))
collect element)
我还可以检索列表中的最高元素:
(loop for element in list
when (equal 'object1 (first element))
maximize (third element))
但是,这只会返回一个元素。我想要的是所有最大要素。我曾尝试使用collect
和进行一些组合maximize
,但是我对语法的了解很少。有没有办法在“简单”功能中收集所有最高元素?
您可以loop
通过一次遍历列表来选择具有正确的第一个元素的所有子列表并确定最大值(可以into
用来loop
累积多个值)来做到这一点,然后loop
在finally
子句中进行第二遍选择,现在仅选择得分最高的人:
(loop for triple in *l*
for (key nil score) = triple
when (eq key 'object1)
collect triple into selection
and maximize score into max-score
finally (return (loop for triple in selection
when (eql (third triple) max-score)
collect triple)))
编辑:或者,代替第二个循环,该delete
函数可以在这里非常简洁地使用:
(loop for triple in *l*
for (key name score) = triple
when (eq key 'object1)
collect triple into selection
and maximize score into max-score
finally (return (delete max-score selection
:test #'/=
:key #'third)))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句