elegant way to count items

后端 未结 9 1997
感情败类
感情败类 2020-12-10 15:29

I have a list shaped like this:

  \'((\"Alpha\" .  1538)
    (\"Beta\"  .  8036)
    (\"Gamma\" .  8990)
    (\"Beta\"  .  10052)
    (\"Alpha\" .  12837)
           


        
9条回答
  •  臣服心动
    2020-12-10 15:49

    (require 'cl)
    (defun count-uniq (list)
      (let ((k 1) (list (sort (mapcar #'car list) #'string<)))
        (loop for (i . j) on list
              when (string= i (car j)) do (incf k)
              else collect (cons i k) and do (setf k 1))))
    

提交回复
热议问题