[英]Sorting a list from max to min by of index number in LISP?
我正在嘗試編寫一個接受列表的函數,將其從高到低排序,然后給出列表中元素從高到低排序的原始位置。 另外,我希望返回的列表中不要重復。
所以..
(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))
))
我不知道要添加什么才能使其正常工作。 有人有什么想法嗎?
(也稱為Schwartzian變換 )
(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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.