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

0 Members and 1 Guest are viewing this topic.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Count Items in a list
« Reply #15 on: January 24, 2013, 10:36:36 AM »
Very deep but slower  :-D

If I were aiming for raw performance, I would not be using recursion...  :roll:
It is a very new approach  :kewl:
 :-D

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Count Items in a list
« Reply #16 on: January 24, 2013, 10:38:50 AM »
It is a very new approach  :kewl:
 :-D

I guess you're new here  :lol:

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Count Items in a list
« Reply #17 on: January 24, 2013, 10:42:39 AM »
It is a very new approach  :kewl:
 :-D

I guess you're new here  :lol:

What was the first clue?
Perhaps the post count?

regardless, recursion does work and is elegant and useful in some cases ... but you knew that already ;-)
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


Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Count Items in a list
« Reply #19 on: January 24, 2013, 10:57:37 AM »
Quote
regardless, recursion does work and is elegant and useful in some cases ... but you knew that already ;-)
Yes about recursion.

My 2 cents: append IS very slow:
(append
  nl
  (list item
      (- (length l) (length (setq l (vl-remove item l))))
  )
)
My apologies for my english.  :-)

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: Count Items in a list
« Reply #20 on: January 24, 2013, 02:08:17 PM »
I think you can shave a little more off your function Lee (forgive me if I'm wrong; I have a hard time with lisp now).
Quick once through look.

Code - Auto/Visual Lisp: [Select]
  1. (defun _count ( l / c x )
  2.   (cond
  3.     ((setq x (car l)
  4.            l (vl-remove x (cdr l)))
  5.      (cons (cons x (- (length l) (length l))) (_count l))
  6.      )
  7.     )
  8.   )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Count Items in a list
« Reply #21 on: January 24, 2013, 02:13:58 PM »
I think you can shave a little more off your function Lee (forgive me if I'm wrong; I have a hard time with lisp now).
Quick once through look.

Code - Auto/Visual Lisp: [Select]
  1. (defun _count ( l / c x )
  2.   (cond
  3.     ((setq x (car l)
  4.            l (vl-remove x (cdr l)))
  5.      (cons (cons x (- (length l) (length l))) (_count l))
  6.      )
  7.     )
  8.   )

Not quite, since (- (length l) (length l)) will always return zero, and you will also miss the last item of the list; but thank you for your interest Se7en, I appreciate your suggestion.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: Count Items in a list
« Reply #22 on: January 24, 2013, 02:16:30 PM »
The [much faster] iterative version might be:

Code - Auto/Visual Lisp: [Select]
  1. (defun _count ( l / c l r x )
  2.     (while l
  3.         (setq x (car l)
  4.               c (length l)
  5.               l (vl-remove x (cdr l))
  6.               r (cons (cons x (- c (length l))) r)
  7.         )
  8.     )
  9.     (reverse r)
  10. )

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: Count Items in a list
« Reply #23 on: January 24, 2013, 02:36:28 PM »
Not quite ...

*nod* Well, it was worth a shot (I'm surprised I got that far ;] ).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Count Items in a list
« Reply #24 on: January 24, 2013, 03:16:46 PM »
Maybe:

Code - Auto/Visual Lisp: [Select]
  1. (defun item_cnt (data / cl q)
  2.   (foreach a data
  3.     (setq cl (if (setq q (cadr (assoc a cl)))
  4.                  (subst (list a (1+ q)) (assoc a cl) cl)
  5.                  (cons (list a 1) cl))))
  6.  cl)
  7.  

-David
« Last Edit: January 25, 2013, 07:23:54 AM by David Bethel »
R12 Dos - A2K

Q1241274614

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


(defun c:test( / )
(setq aa
(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")
 )   
 )
(setq b (item_sort aa))
(princ b)
(princ)
  )
(defun item_sort ( l / c l r x e  aa ll)
              (while l  (setq  x (caar l)
                e (cons (cadar l) e)
                l (cdr l)   
          )   
         (foreach y l
               (if  (= (car y) x)
               (setq e (cons (cadr y) e))
               (setq ll (cons y ll))       
                )
           )
          (setq aa (cons (list x e) aa) l ll  e nil  ll nil)
      )
  aa
  )

Maybe I didn't express clearly.I hope this result.Can optimize a (item_sort)?

pBe

  • Bull Frog
  • Posts: 402
Re: Count Items in a list
« Reply #26 on: January 24, 2013, 10:03:04 PM »
Really now Q1241274614, you change your requirement from this:

(list "JB1X22X40X100X20X8" a1);<--- note second element not a string, hence my Q about being a variable
(list"JB1X22X40X100X20X8"a3)
Result
("JB1X22X40X100X20X8" 2 (a1 a3));<--- note the second element as quantity of found match

Now to this:
(list "JB1X22X40X100X20X8" "a1");<--- both elements as string
(list "JB1X22X40X100X20X8" "a13")
Result
("JB1X22X40X100X20X8" ("a3" "a1"));<-- quantity, gone, not there, not included.

Please make up you mind Q1241274614  ;D
Code: [Select]
(defun countvar  (l / a b c d )
      (while (setq a (Car l))
            (while (setq b (assoc (Car a) l))
                  (setq c   (cons (cadr b) c)
                        l (vl-remove b l)))
            (setq d (cons (list (Car a)  c) d)
                  c nil))
      d
      )

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Count Items in a list
« Reply #27 on: January 25, 2013, 03:27:54 PM »
The [much faster] iterative version might be:

Code - Auto/Visual Lisp: [Select]
  1. (defun _count ( l / c l r x ) ...
  2.  
Yes it is very similar but is faster on long list:
Code: [Select]
Benchmark.lsp | © 2005 Michael Puckett | All Rights Reserved
Elapsed milliseconds / relative speed for 32768 iteration(s):

    (COUNT2 ALIST)......1966 / 1.03 <fastest>
    (_COUNT3 ALIST).....2028 / 1 <slowest>

Elapsed milliseconds / relative speed for 1 iteration(s):

    (_COUNT3 ALIST).....2996 / 1.26 <fastest>
    (COUNT2 ALIST)......3776 / 1 <slowest>

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Count Items in a list
« Reply #28 on: December 04, 2016, 01:56:11 PM »
Necro-thread resurrection ...

Had to write a "find all positions of x in list" type function today and realized it could be used to "count items in a list".

Not saying it's the fastest (haven't benched it) but it's a different spin which is always good for algorithm fodder ...

Code: [Select]
(defun _Positions ( x lst / p )
    ;;  find all the n positions of x in lst
    ;;  (_Positions 1 '(0 0 1 0 0 1)) >> (2 5)
    (if (setq p (vl-position x lst))
        (   (lambda ( lst result )
                (while (setq p (vl-position x lst))
                    (setq
                        result (cons (+ 1 p (car result)) result)
                        lst    (cdr (member x lst))
                    )
                )
                (reverse result)
            )
            (cdr (member x lst))
            (list p)
        )
    )   
)

And:

Code: [Select]
(defun _Tally ( x lst )
    ;;  count all the occurances of x in lst
    ;;  (_Tally 1 '(0 0 1 0 0 1)) >> 2
    (length (_Positions x lst))
)

For what it's worth, cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Count Items in a list
« Reply #29 on: December 05, 2016, 08:47:04 AM »
New test:
Code: [Select]
(progn
  (setq aList nil Countr 0)
  (repeat 10000 (setq aList (cons (setq Countr (1+ Countr)) aList)));100000
  (repeat 4 (setq Alist (append Alist Alist))) ;5
  (princ "\nLength   : ") (princ (length aList))
  (princ "    Positions: ") (princ (length (ALE_PositionS 1   Alist))) (princ "\n ") (princ)
)
(Benchmark '(
(MP_Positions 1   Alist)
(SK_index 1   Alist)
(MP_Positions 1   Alist)
(SK_index 1   Alist)
)   
Code: [Select]
Length   : 160000    Positions: 16
Elapsed milliseconds / relative speed for 512 iteration(s):
    (SK_INDEX 1 ALIST).........1531 / 1.01 <fastest>
    (MP_POSITIONS 1 ALIST).....1546 / 1
    (SK_INDEX 1 ALIST).........1547 / 1
    (MP_POSITIONS 1 ALIST).....1547 / 1 <slowest>

Length   : 160000    Positions: 16
Elapsed milliseconds / relative speed for 512 iteration(s):
    (MP_POSITIONS 1 ALIST).....1547 / 1 <fastest>
    (SK_INDEX 1 ALIST).........1547 / 1
    (MP_POSITIONS 1 ALIST).....1547 / 1
    (SK_INDEX 1 ALIST).........1547 / 1 <slowest>
Code: [Select]
; Stefan
(defun SK_index (n l / i j r)
  (setq j -1)
  (while  (setq i (vl-position n l))
    (setq r (cons (setq j (+ 1 i j)) r))
    (setq l (cdr (member n l)))
  )
  (reverse r)
)