kulfi Posted July 16, 2012 Posted July 16, 2012 Exactly this is how i need List1 = ((0 0) (1 0.1) (2 0.1) (3 0.1) (4 0.2) (5 0.2) (6 0.3) (7 0.3))First of all the list given above can vary every time secondly i want to separate the list for same cdr elements and store them to a variable Like i want like this List A = (0 0) List B = (1 0.1) (2 0.1)(3 0.1) List C = ............................etc Thanks OR if i get a list like this is also good list1 ((0 0)(1 .1)(4 .2)(6 .3)) Quote
Tharwat Posted July 16, 2012 Posted July 16, 2012 Here are many replies to your thread which is the same thread title and contents as well . http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/List-Conents/td-p/3532496 Quote
Lee Mac Posted July 16, 2012 Posted July 16, 2012 Consider the following function: [color=GREEN];; Group By Function - Lee Mac[/color] [color=GREEN];; Groups items considered equal by a given predicate function[/color] ([color=BLUE]defun[/color] LM:GroupByFunction ( lst fun [color=BLUE]/[/color] tmp1 tmp2 x1 ) ([color=BLUE]if[/color] ([color=BLUE]setq[/color] x1 ([color=BLUE]car[/color] lst)) ([color=BLUE]progn[/color] ([color=BLUE]foreach[/color] x2 ([color=BLUE]cdr[/color] lst) ([color=BLUE]if[/color] (fun x1 x2) ([color=BLUE]setq[/color] tmp1 ([color=BLUE]cons[/color] x2 tmp1)) ([color=BLUE]setq[/color] tmp2 ([color=BLUE]cons[/color] x2 tmp2)) ) ) ([color=BLUE]cons[/color] ([color=BLUE]cons[/color] x1 ([color=BLUE]reverse[/color] tmp1)) (LM:GroupByFunction ([color=BLUE]reverse[/color] tmp2) fun)) ) ) ) Call the above with an appropriate predicate function: (LM:GroupByFunction '( (0 0) (1 0.1) (2 0.1) (3 0.1) (4 0.2) (5 0.2) (6 0.3) (7 0.3) ) (lambda ( a b ) (= (cadr a) (cadr b))) ) Returns: ( ((0 0)) ((1 0.1) (2 0.1) (3 0.1)) ((4 0.2) (5 0.2)) ((6 0.3) (7 0.3)) ) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.