sorting list of point

sorting list of point

xuantientutungXUK6E
Advocate Advocate
1,107 Views
5 Replies
Message 1 of 6

sorting list of point

xuantientutungXUK6E
Advocate
Advocate

hello masters,

i have two list of point, for example A=( (1 0 1) (2 0 1) (4 5 1) ( 6 2 1))

                                                                  B=( (1 0 1) (1 0 1) (3 0 1) (4 5 1)  (4 5 1))

the task is find the difference points between two list A and B,

(Each point in list A only needs to be repeated at B once to be subtracted)

which result is a new list C=((1 0 1) (2 0 1) (3 0 1) (4 5 1) ( 6 2 1))

please guide me.

thank for your help.

0 Likes
Accepted solutions (2)
1,108 Views
5 Replies
Replies (5)
Message 2 of 6

Sea-Haven
Mentor
Mentor

For me the best way may be add the two lists together, then sort the big list based on the 3 items, you just compare two items and if = same then go to next making a result list.

 

I have what you want but its not in one bit of code. This is a good starting point

 

(defun compare-elements (a b sortspec)
( (lambda (x y test)
(if (and (equal x y ) (cdr sortspec))
(compare-elements a b (cdr sortspec))
(apply test (list x y))
)
)
(nth (cdar sortspec) a)
(nth (cdar sortspec) b)
(caar sortspec)
)
)

(defun complex-sort (alst sortspec)
(vl-sort alst
'(lambda (a b)
(compare-elements a b sortspec)
)
)
)

(setq lst (complex-sort lst '((< . 0) (< . 1) (< . 2))))

(setq lst3 '())
(setq x 0)
(repeat (- (length lst) 1)
(if (= (nth x lst)(nth (+ x 1) lst))
(princ "OK")
(setq lst3 (cons  (nth x lst) lst3))
)
Message 3 of 6

Sea-Haven
Mentor
Mentor

Please ignore post above this is probably what you want

 

; make 1 list in order of two lists
; Alan H OCT 2019

(defun compare-elements (a b sortspec)
( (lambda (x y test)
(if (and (equal x y ) (cdr sortspec))
(compare-elements a b (cdr sortspec))
(apply test (list x y))
)
)
(nth (cdar sortspec) a)
(nth (cdar sortspec) b)
(caar sortspec)
)
)
(defun complex-sort (alst sortspec)
(vl-sort alst
'(lambda (a b)
(compare-elements a b sortspec)
)
)
)

(setq a (list (list 1 0 1)(list 2 0 1)(list 4 5 1)(list 6 2 1)))
(setq b (list (list 1 0 1)(list 1 0 1)(list 3 0 1)(list 4 5 1)(list 4 5 1)))

(setq lst (append a b))
(setq lst (complex-sort lst '((< . 0) (< . 1) (< . 2))))

(setq lst3 '())
(setq x 0)
(repeat (- (length lst) 1)
(if (equal(nth x lst)(nth (+ x 1) lst))
(princ "match")
(setq lst3 (cons (nth x lst) lst3))
)
(setq x (+ x 1))
)
(setq lst3 (cons (nth  (- (length lst) 1) lst) lst3))
Message 4 of 6

Kent1Cooper
Consultant
Consultant
Accepted solution

If, as in your example, the A list would not have repeats as the B list can, and if the result doesn't need to be in any particular order, try this:

(defun C:whatever ()
  (setq
    listA '((1 0 1) (2 0 1) (4 5 1) ( 6 2 1))
    listB '((1 0 1) (1 0 1) (3 0 1) (4 5 1) (4 5 1))
    listC listA
  ); setq
  (foreach item listB
    (if (not (member item listA))
      (setq listC (cons item listC))
    ); if
  ); foreach
  listC
)

That can be sorted [presumably by X coordinate if your example is the desired order, but could be by other criteria].  If list A might also have duplicates, that can also be accounted for.

 

Kent Cooper, AIA
Message 5 of 6

ronjonp
Mentor
Mentor
Accepted solution

Give this a try too:

;;http://www.lee-mac.com/uniqueduplicate.html
;; Unique  -  Lee Mac
;; Returns a list with duplicate elements removed.
(defun LM:Unique ( l )
    (if l (cons (car l) (LM:Unique (vl-remove (car l) (cdr l)))))
)
(setq l1 '((1 0 1) (2 0 1) (4 5 1) (6 2 1)))
(setq l2 '((1 0 1) (1 0 1) (3 0 1) (4 5 1) (4 5 1)))
( LM:Unique (append l1 l2) )
Message 6 of 6

Sea-Haven
Mentor
Mentor

Should have known Lee as usual has a brilliant answer.