Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Element counting in a list

  1. #1
    Member
    Join Date
    2002-10
    Posts
    8
    Login to Give a bone
    0

    Question Element counting in a list

    I have a list (setq list ("A" "A" "B" "B" "B" "C" etc..))
    I need to have a way to return a count of the elements, maybe a dotted pair ("A" . 2) ("B" . 3) ("C" . 1). I have figured out how to make the first dotted pair i.e. ("A" . 1), but I dunno how to change the 1 to a 2 or any other succesive number.

    There might be another way to get these counts, but I can't seem to get anywhere in getting it.

    Thanks, Rabbit

  2. #2
    AUGI Addict
    Join Date
    2005-08
    Posts
    1,043
    Login to Give a bone
    0

    Default Re: Element counting in a list

    Here is a freebie.

    Code:
    (defun ListCount ( lst / tlst rlst )
        
        (foreach i lst
            (setq rlst
                (if (setq tlst (assoc i rlst))
                    (subst (cons i (1+ (cdr tlst))) tlst rlst)
                    (cons (cons i 1) rlst)
                )
            )
        )
    )
    Command: (listcount '("A" "A" "B" "B" "B" "C"))
    (("C" . 1) ("B" . 3) ("A" . 2))

    Welcome to AUGI!

  3. #3
    Member
    Join Date
    2002-10
    Posts
    8
    Login to Give a bone
    0

    Question Re: Element counting in a list

    Welp, I had gotten to that point before, although yours is waaaaay more graceful. My problem is if I want to increase the count by 1 "A", ("A" . 2) to ("A" . 3) . How would I go about just changing the number? SUBST?

    Or, would I have to get all the info from the returned list and then add 1 to the number I need and then use your code to recreate the list.

    Thanks again, Rabbit

  4. #4
    AUGI Addict
    Join Date
    2005-08
    Posts
    1,043
    Login to Give a bone
    0

    Default Re: Element counting in a list

    The code I posted will do what you want. Try to understand what is happening.

  5. #5
    Active Member
    Join Date
    2006-08
    Location
    Brisbane : GMT+10
    Posts
    87
    Login to Give a bone
    0

    Default Re: Element counting in a list

    Tims code does exactly what you asked for.

    If you want to add to the list manually, try something like this perhaps ...
    Code:
    
    (defun kdub-IncrementConsValue (ConsList Key Value / v)
      (if (setq v (assoc Key ConsList))
        (subst (cons Key (+ Value (cdr v))) v ConsList)
        (cons (cons Key Value) ConsList)  
      ) 
    )

    Code:
    
    (setq Counted_List '(("C" . 1) ("B" . 3) ("A" . 2)))
    
    (setq New_List (kdub-IncrementConsValue Counted_List "A" 5))
    
    (kdub-IncrementConsValue New_List "NEW" 55)
    returns


    (("C" . 1) ("B" . 3) ("A" . 2))
    (("C" . 1) ("B" . 3) ("A" . 7))
    (("NEW" . 55) ("C" . 1) ("B" . 3) ("A" . 7))

  6. #6
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: Element counting in a list

    Hope you don't mind... Here's a minor adaptation on Buzzard's code:

    Code:
    (defun ListCount  (lst / tlst rlst)
      (vl-load-com)
      (foreach i  lst
        (setq rlst
               (if (setq tlst (assoc i rlst))
                 (subst (cons i (1+ (cdr tlst))) tlst rlst)
                 (cons (cons i 1) rlst)
                 )
              )
        )
      (vl-sort
        rlst
        '(lambda (x y)
           (< (car x) (car y))))  
    )
    Results:

    Code:
    _$ (setq lst (list  "A" "A" "B" "B" "B" "C"))
    ("A" "A" "B" "B" "B" "C")
    _$ (ListCount lst)
    (("A" . 2) ("B" . 3) ("C" . 1))
    _$
    Last edited by RenderMan; 2010-11-01 at 03:02 PM. Reason: Added (vl-load-com)
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  7. #7
    Member
    Join Date
    2002-10
    Posts
    8
    Login to Give a bone
    0

    Default Re: Element counting in a list

    Your right, Tim's code does so it. But for the life of me, I couldn't get it to work as you have shown. I think I'm at the point to where I've got to toatly clear my mind of everything I've been trying and focus on the solutions you guys gave me so that I can understand how it all works.

    I appreciate all the help everyone has given me.

    Rabbit

    Quote Originally Posted by Kerry Brown View Post
    Tims code does exactly what you asked for.

    If you want to add to the list manually, try something like this perhaps ...
    Code:
    
    (defun kdub-IncrementConsValue (ConsList Key Value / v)
      (if (setq v (assoc Key ConsList))
        (subst (cons Key (+ Value (cdr v))) v ConsList)
        (cons (cons Key Value) ConsList)  
      ) 
    )

    Code:
    
    (setq Counted_List '(("C" . 1) ("B" . 3) ("A" . 2)))
    
    (setq New_List (kdub-IncrementConsValue Counted_List "A" 5))
    
    (kdub-IncrementConsValue New_List "NEW" 55)
    returns


    (("C" . 1) ("B" . 3) ("A" . 2))
    (("C" . 1) ("B" . 3) ("A" . 7))
    (("NEW" . 55) ("C" . 1) ("B" . 3) ("A" . 7))

  8. #8
    Member
    Join Date
    2015-12
    Location
    Illinois
    Posts
    38
    Login to Give a bone
    0

    Default Re: Element counting in a list

    I think what Rabbitm_69 is looking for is supplying a list like:

    ("A" "A" "B" "B" "B" "C")

    And receiving a list like:

    ( ("A" . 1) ("A" . 2) ("B" . 1) ("B" . 2) ("B" . 3) ("C" .1))

  9. #9
    Administrator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,719
    Login to Give a bone
    0

    Default Re: Element counting in a list

    Quote Originally Posted by rmoore View Post
    I think what Rabbitm_69 is looking for is supplying a list like:

    ("A" "A" "B" "B" "B" "C")

    And receiving a list like:

    ( ("A" . 1) ("A" . 2) ("B" . 1) ("B" . 2) ("B" . 3) ("C" .1))
    ... What purpose does that result serve?
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3660, Core i9-12900K 5.2GHz, 64GB DDR5 RAM, PCIe 4.0 M.2 SSD (RAID 0), 16GB NVIDIA RTX A4000

  10. #10
    AUGI Addict
    Join Date
    2015-12
    Posts
    2,095
    Login to Give a bone
    0

    Default Re: Element counting in a list

    Ordinal count of each unque item the list i.e. third "A", first "C", and so on?

Page 1 of 2 12 LastLast

Similar Threads

  1. 2013: Creating a list of element IDs and IFC GUIDs
    By kyelek356536 in forum Revit - API
    Replies: 1
    Last Post: 2013-02-13, 05:53 PM
  2. Replies: 0
    Last Post: 2012-02-24, 02:33 PM
  3. Replies: 1
    Last Post: 2010-07-15, 08:43 PM
  4. Structural Element may not be supported -- verify support for element
    By anthony.hoare in forum Revit Structure - General
    Replies: 0
    Last Post: 2007-06-27, 01:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •