Author Topic: split list into lists  (Read 16213 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
split list into lists
« on: January 14, 2005, 11:40:10 AM »
I'm having a brain freeze problem today!
I want to parse a list into smaller lists excluding a certain item.  Example, given the list
Code: [Select]
(5 4 3 0 9 8 7 0 6 7 8) separate it into a list of smaller lists such as
Code: [Select]
((543)(987)(678))
I can't for the life of me figure this out! Maybe because it's Friday. :D
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
split list into lists
« Reply #1 on: January 14, 2005, 12:01:41 PM »
Always three items?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
split list into lists
« Reply #2 on: January 14, 2005, 12:05:09 PM »
Quote from: Se7en
Always three items?

Nope!! can be any number.
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
split list into lists
« Reply #3 on: January 14, 2005, 12:26:10 PM »
What?! Your killing me!

...Fine, give me a few more min.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

whdjr

  • Guest
split list into lists
« Reply #4 on: January 14, 2005, 12:35:40 PM »
Are you always going to use 0 as a divider?

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
split list into lists
« Reply #5 on: January 14, 2005, 12:36:37 PM »
Quote from: whdjr
Are you always going to use 0 as a divider?

yes, well it's always a constant.
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
split list into lists
« Reply #6 on: January 14, 2005, 12:44:41 PM »
Sorry i dont have more time to test this more. But this is a quick run thru. No pseudo code, no nothing. Just whiped it off the top of my head.  

(second run at it though ...*Ahem!?*)

Code: [Select]
;;; Parse list
;;; Split a list into parts delimited of a specific delim.
;;;
;;; Example:
;;; Command: (setq lst '(1 0 2 0 5 0 5 9 2 3 4 7 8 9 5 0 7 8 9))
;;; (1 0 2 0 5 0 5 9 2 3 4 7 8 9 5 0 7 8 9)
;;;
;;; Command: (parselst 3 0 lst)
;;; ((1 2 5) (5 5 9) (5 9 2) (3 4 7) (8 9 5) (7 8 9))
(defun parselst (nu del alst / pop# isdel nlst)
  (defun isdel (a b) (= a b))
  (defun pop# (alst / cntr nlst)
    (setq cntr 0 nlst '() )
    (while
      (< (length nlst) nu)
      (if (isdel (nth cntr alst) del)
        (setq cntr (1+ cntr)))      
      (setq nlst (cons (nth cntr alst) nlst))
      (setq cntr (1+ cntr))
      )
    (reverse nlst)
    )
  (while (>= (length alst) nu)
    (setq nlst (cons (pop# alst) nlst))
    (setq alst (cdddr alst))
   )
  (reverse nlst)
 )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
split list into lists
« Reply #7 on: January 14, 2005, 12:55:19 PM »
Whoa nelly!  Thats wrong!?

Hold on I'll fix it.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

SMadsen

  • Guest
split list into lists
« Reply #8 on: January 14, 2005, 01:24:39 PM »
Mark, it's just a parser with a delimiter you're making, right? e.g.

(func 8 '(5 4 3 0 9 8 9 0 5 6 8 )) -> ((5 4 3 0 9) (9 0 5 6))

Code: [Select]
(defun parselst (item lst / l ll)
  (while lst
    (cond ((not (eq (car lst) item))
           (setq l (cons (car lst) l)))
          ((setq ll (cons (reverse l) ll)
                 l  nil))
    )
    (setq lst (cdr lst))
  )
  (reverse (if l (cons (reverse l) ll) ll))
)

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
split list into lists
« Reply #9 on: January 14, 2005, 01:38:56 PM »
Quote from: SMadsen
Mark, it's just a parser with a delimiter you're making, right?

Yes that is correct. That's really close Stig, close enough to get me going anyway. thanks.

Output w/ my list
Code: [Select]

ll = (32 53 54 46 48 48 32 32 55 56 32 32 56 56 32)
(parselst 32 ll)
(nil (53 54 46 48 48) nil (55 56) nil (56 56))
TheSwamp.org  (serving the CAD community since 2003)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
split list into lists
« Reply #10 on: January 14, 2005, 02:11:06 PM »
More code:
Code: [Select]
;;-------------------------------------------------
;; Functions originated by Ken Alexander (03-05-03)
;; that are 10X faster than @cv_parse_list, yet limited
;; to only pairs or triplets.
;; Thanks, Ken!
;; (01-02-04) corrected to (while old ...)
;; Revised (01-16-04) to increase speed thanks to Tony T's
;; example of using (while (setq ...))
(defun @cv_double_up (old / new)
  (and
    old
    (while
      (setq new (cons (list (car old) (cadr old)) new)
            old (cddr old)
      )
    )
  )
  (reverse new)
)
;; Revised (01-16-04) to increase speed thanks to Tony T's
;; example of using (while (setq ...))
(defun @cv_triple_up (old / new)
  (and
    old
    (while
      (setq new (cons (list (car old) (cadr old) (caddr old)) new)
            old (cdddr old)
      )
    )
  )
  (reverse new)
)
;; (01-16-04) combination of @cv_double_up and @cv_triple_up
;; Not yet employed.
(defun @cv_grouplist (old n / new)
  ;; n must be 2 or 3
  (and
    old
    (cond
      ((= n 2)
       (while
         (setq new (cons (list (car old) (cadr old)) new)
               old (cddr old)
         )
       )
      )
      ((= n 3)
       (while
         (setq new (cons (list (car old) (cadr old) (caddr old)) new)
               old (cdddr old)
         )
       )
      )
    )
  )
  (reverse new)
)
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.

SMadsen

  • Guest
split list into lists
« Reply #11 on: January 14, 2005, 02:23:25 PM »
Quote from: Mark Thomas
Yes that is correct. That's really close Stig, close enough to get me going anyway. thanks.

Oh well, one typo more or less .. :)  It could've been avoided by using APPEND instead (and code made shorter) but I like CONS better

Code: [Select]
(defun parselst (item lst / l ll)
  (while lst
    (cond ((not (eq (car lst) item))
           (setq l (cons (car lst) l)))
          ((setq ll (if l (cons (reverse l) ll) ll)
                 l  nil))
    )
    (setq lst (cdr lst))
  )
  (reverse (if l (cons (reverse l) ll) ll))
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
split list into lists
« Reply #12 on: January 14, 2005, 03:14:26 PM »
Ooops, Guess I wasn't paying attention. :oops:
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.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
split list into lists
« Reply #13 on: January 14, 2005, 03:37:05 PM »
Thanks Stig that's works like a champ. I was trying to wrap the whole thing into a 'apply append mapcar' type setup.
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
split list into lists
« Reply #14 on: January 14, 2005, 04:12:51 PM »
Ok, im sorry. I got hung up here at work.  ...but im glad you got it working.

BTW, Thats all you wanted?! What was the 'list of three' thing?  (Im not sorry anymore.)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org