我正在尝试编写一个带有列表的函数,将其从高到低排序,然后给出从高到低排序的列表中元素的原始位置。此外,我希望在返回的列表中没有重复。
所以..
(LargestNumberIndex '( 1 23 101 5 6 5 )
应该返回
(2 1 4 3 5 0)
不
(2 1 4 3 3 0)
目前我的功能如下:
(defun LargestNumberIndex (listofnums Indexes)
;; make a copy of the list to sort
(setf listcopy (copy-list listofnums))
;; sort list from high to low
(setf sortlist (sort listcopy #'>))
;; compare the elements from both lists to determine the
;; position
(loop for i from 0 below Indexes collect
(position (nth i sortlist) listofnums :test #'equal))
))
我无法弄清楚要添加什么才能让它发挥作用。有没有人有任何想法?
答案 0 :(得分:2)
(defun largest-number-index (list)
(mapcar #'second
(stable-sort
(loop
for index from 0
for element in list
collect (list element index))
#'>
:key #'first)))
(largest-number-index '(1 23 101 5 6 5))
=> (2 1 4 3 5 0)