elegant way to count items

后端 未结 9 1961
感情败类
感情败类 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:50

    I dunno that this is the most elegant, but it seems reasonable:

    (defun add-for-cheeso (data)
      (let (result)
        (dolist (elt data result)
          (let ((sofar (assoc (car elt) result)))
            (if sofar
                (setcdr sofar (1+ (cdr sofar)))
              (push (cons (car elt) 1) result))))))
    

提交回复
热议问题