Author Topic: Count Items in a list  (Read 8087 times)

0 Members and 1 Guest are viewing this topic.

Q1241274614

  • Guest
Count Items in a list
« on: January 23, 2013, 09:47:25 AM »

Can you help me to have a look how fast the statistical number of the same string
thank!
("JB1X22X40X100X20X8"
"JB1X26X50X120X40X10"
"JB1X22X40X100X20X8"
"JB1X30X60X150X40X16"
"JB2X23X50X100X20X8"
"JB2X23X40X100X20X8"
"JB2X30X60X150X40X16"
"JB1X26X50X120X40X10"
"JB2X26X50X120X40X10"
"JB2X30X60X150X40X16"
"JB1X30X60X150X40X16"
"JB2X23X50X100X20X8"
"JB1X26X50X120X40X10"
"JB2X30X60X150X40X16"
"JB2X26X50X120X40X10"
"JB1X30X60X150X40X16"
"JB2X23X40X100X20X8"
"JB2X26X50X120X40X10"
"JB1X30X60X150X40X16"
"JB2X30X60X150X40X16"
"JB3X40X60X155X40X16"
 )

("JB1X22X40X100X20X8" 2)
("JB1X26X50X120X40X10" 3 )
("JB1X30X60X150X40X16" 4)
("JB2X23X40X100X20X8" 2)
("JB2X23X50X100X20X8" 2)
("JB2X26X50X120X40X10" 3)
("JB2X30X60X150X40X16" 4)
("JB3X40X60X155X40X16" 1)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Count Items in a list
« Reply #1 on: January 23, 2013, 10:03:11 AM »
Maybe this:
Code: [Select]
(defun count (lst / i cnt nlst)
  (while (setq i (car lst))
    (setq lst (cdr lst)
          cnt (length lst))
    (setq lst (vl-remove i lst)
          nlst (cons (list i (1+ (- cnt (length lst)))) nlst))
  )
  nlst
)
I've reached the age where the happy hour is a nap. (°Ώ°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Q1241274614

  • Guest
Re: Count Items in a list
« Reply #2 on: January 23, 2013, 10:15:31 AM »
thank

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Count Items in a list
« Reply #3 on: January 23, 2013, 11:04:09 AM »
IMHO I think for this new question is better to start a new thread...
Very similar CAB version:
Code: [Select]
(defun count2 (In_Lst / OutLst TmpLst)
 (while In_Lst
  (setq
    TmpLst (car In_Lst)
    OutLst
    (cons
      (cons
        TmpLst
        (-
          (length In_Lst)
          (length (setq In_Lst (vl-remove TmpLst In_Lst)))
        )
      )
      OutLst
    )
  )
 )
)

; Michael Puckett - Foreach "old style"
(defun TallyList ( lst / result pair )
   (reverse
      (foreach x lst
         (setq result
            (if (setq pair (assoc x result))
               (subst (cons x (1+ (cdr pair))) pair result)
               (cons (cons x 1) result)
            )
         )
      )
   )
)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Count Items in a list
« Reply #4 on: January 23, 2013, 11:45:22 AM »
Another:

Code: [Select]
(defun _count ( l / c x )
    (if (setq x (car l))
        (progn
            (setq c (length l)
                  l (vl-remove x (cdr l))
            )
            (cons (cons x (- c (length l))) (_count l))
        )
    )
)

pBe

  • Bull Frog
  • Posts: 402
Re: Count Items in a list
« Reply #5 on: January 23, 2013, 12:43:34 PM »
Talk about "old style"  ;D

Code: [Select]
(defun countOLDer (lst / counted)
  (setq lst (acad_strlsort lst))
     (while (setq a (car lst))
                (setq n 1)
                (while (eq a (car (setq lst (cdr lst))))
                                (setq n ( + n 1))
                          )
             (setq counted (cons (list a n) counted)))
  )

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Count Items in a list
« Reply #6 on: January 23, 2013, 12:48:12 PM »
Code: [Select]
(defun countOLDer (lst / counted)
< ... >

Nice variation pBe  :-)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Count Items in a list
« Reply #7 on: January 23, 2013, 01:02:37 PM »
Another:
Code: [Select]
(defun _count ( l / c x ) ...

Recursion is often shorter and beautiful but sometimes can be slower and can bomb on very very very log list:

(setq aList '("JB1X22X40X100X20X8"
"JB1X26X50X120X40X10""JB1X22X40X100X20X8""JB1X30X60X150X40X16""JB2X23X50X100X20X8"
"JB2X23X40X100X20X8""JB2X30X60X150X40X16""JB1X26X50X120X40X10""JB2X26X50X120X40X10"
"JB2X30X60X150X40X16""JB1X30X60X150X40X16""JB2X23X50X100X20X8""JB1X26X50X120X40X10"
"JB2X30X60X150X40X16""JB2X26X50X120X40X10""JB1X30X60X150X40X16""JB2X23X40X100X20X8"
"JB2X26X50X120X40X10""JB1X30X60X150X40X16""JB2X30X60X150X40X16""JB3X40X60X155X40X16"
) )
(progn
  (setq alist (append alist alist alist alist))
  (setq alist (append alist alist alist alist))
  (setq alist (append alist alist alist alist))
  (setq alist (append alist alist alist alist))
  (setq alist (append alist alist alist alist))
  (setq alist (append alist alist alist alist))
  (setq alist (append alist alist alist alist))
  (setq alist (append alist alist alist alist))
  (setq alist (append alist alist alist alist))
  (princ)
)
;(length alist) 5505024

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Count Items in a list
« Reply #8 on: January 23, 2013, 01:04:22 PM »
Another:
Code: [Select]
(defun _count ( l / c x ) ...
Recursion is often shorter and beautiful but sometimes can be slower and can bomb on very very very log list:

Of course  :-)

Q1241274614

  • Guest
Re: Count Items in a list
« Reply #9 on: January 23, 2013, 09:14:15 PM »
(list
(list "JB1X22X40X100X20X8"   a1)
(list"JB1X26X50X120X40X10"   a2)
(list"JB1X22X40X100X20X8"    a3)
(list"JB1X30X60X150X40X16"   a4)
(list"JB2X23X50X100X20X8"    a5)
(list"JB2X23X40X100X20X8"    a6)
(list"JB2X30X60X150X40X16"   a7)
(list"JB1X26X50X120X40X10"   a8)
(list"JB2X26X50X120X40X10"   a9)
(list"JB2X30X60X150X40X16"   a10)
(list"JB1X30X60X150X40X16"   a11)
(list"JB2X23X50X100X20X8"    a12)
(list"JB1X26X50X120X40X10"   a13)
(list"JB2X30X60X150X40X16"   a14)
(list"JB2X26X50X120X40X10"   a15)
(list"JB1X30X60X150X40X16"   a16)
(list"JB2X23X40X100X20X8"    a17)
(list"JB2X26X50X120X40X10"   a18)
(list"JB1X30X60X150X40X16"   a19)
(list"JB2X30X60X150X40X16"  a20)
(list"JB3X40X60X155X40X16"  a21)
 )

("JB1X22X40X100X20X8" 2   (a1 a3))
("JB1X26X50X120X40X10" 3  (a13 a2 a9))
("JB1X30X60X150X40X16" 4  (a?  a?  a?  a?) )
("JB2X23X40X100X20X8" 2   (a?  a?))
("JB2X23X50X100X20X8" 2   (a?  a?))
("JB2X26X50X120X40X10" 3  (a?  a?  a?))
("JB2X30X60X150X40X16" 4  (a?  a?  a?  a?))
("JB3X40X60X155X40X16" 1  (a21 ))


Statistical number at the same time the corresponding pixel is classified
« Last Edit: January 24, 2013, 02:51:14 AM by Q1241274614 »

pBe

  • Bull Frog
  • Posts: 402
Re: Count Items in a list
« Reply #10 on: January 23, 2013, 11:53:46 PM »
Code: [Select]
(defun countOLDer (lst / counted)
< ... >

Nice variation pBe  :)

Thank you Lee, its a dinosaur but somebody's  got to do it;  :)

Heres one for the OPS new list:
Code: [Select]
(defun countvar  (lst / a b c d n)
      (while (setq a (Car lst))
            (setq n 0)
            (while (setq b (assoc (Car a) lst))
                  (setq n   (1+ n )
                        c   (cons (cadr b) c)
                        lst (vl-remove b lst)))
            (setq d (cons (list (Car a) n c) d)
                  c nil))
      d
      )

BTW: are those variables?
(list "JB1X22X40X100X20X8"   a1);<--- that

Also i change the format to:
(setq lst '(( "JB1X22X40X100X20X8"   a1)("JB1X26X50X120X40X10"   a2)("JB1X22X40X100X20X8"    a3).......)
« Last Edit: January 24, 2013, 12:00:50 AM by pBe »

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Count Items in a list
« Reply #11 on: January 24, 2013, 08:34:31 AM »
One more to boil the brain  :lol:

Code - Auto/Visual Lisp: [Select]
  1. (defun f ( l / g h )
  2.     (defun g ( k l )
  3.         (cond
  4.             ((= k (caar l)) (cons (list k (1+ (cadar l))) (cdr l)))
  5.             (l (cons (car l) (g k (cdr l))))
  6.             ((list (list k 1)))
  7.         )
  8.     )
  9.     (defun h ( l r )
  10.         (if l (h (cdr l) (g (car l) r)) r)
  11.     )
  12.     (h l nil)
  13. )

Code - Auto/Visual Lisp: [Select]
  1. _$ (f '("a" "b" "c" "d" "b" "a"))
  2. (("a" 2) ("b" 2) ("c" 1) ("d" 1))

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: Count Items in a list
« Reply #12 on: January 24, 2013, 10:06:10 AM »
One more to boil the brain  :lol:

Very deep but slower  :-D

Benchmark.lsp | © 2005 Michael Puckett | All Rights Reserved
Elapsed milliseconds / relative speed for 16384 iteration(s):

    (_COUNT ALIST).....1856 / 3.61 <fastest>
    (F ALIST)..........6708 / 1 <slowest>

(length alist)=21

(_count '("a" "b" "c" "d" "b" "a"))
= > (("a" . 2) ("b" . 2) ("c" . 1) ("d" . 1)) >> ("a" > . < 2)  :pissed:


Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Count Items in a list
« Reply #13 on: January 24, 2013, 10:19:25 AM »
Very deep but slower  :-D

If I were aiming for raw performance, I would not be using recursion...  :roll:

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Count Items in a list
« Reply #14 on: January 24, 2013, 10:22:01 AM »
Ok, here is mine ... buyer beware ;-)


Code: [Select]
(defun _count (l / nl)
  (foreach item l
    (if (not(member item nl))
      (setq
nl (append
     nl
     (list item
   (- (length l) (length (setq l (vl-remove item l))))
     )
   )
      )
    )
  )
  nl
)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie