Author Topic: How to replace the duplicate items in the list  (Read 2001 times)

0 Members and 3 Guests are viewing this topic.

xiaxiang

  • Guest
How to replace the duplicate items in the list
« on: January 15, 2015, 01:48:22 AM »
Inheritted from this topic
How to find  the duplicate items in the list
http://www.theswamp.org/index.php?topic=48646
**********************************************************************************
Just grouping duplicate items is the trick what has been done by you masters.
Then I want to replace them According to the regulation I'll talk about.
1.
Says the original list as follow:
Code: [Select]
(foo '(1.0 1.1 1.19 1.2 1.2 1.21 1.22 1.23 1.25 1.3 )0.05)
;0.05 is fuzz

then we get the duplicate grouped list as follow:
Code: [Select]
((1.19 1.2 1.2 1.21 1.22 1.23 1.25))

and
Code: [Select]
if (1.19 1.2 1.2 1.21 1.22 1.23 1.25)
;;1.2 is the one which Appeared two times at least and Most times then others.
then replace the list
-->(1.2 1.2 1.2 1.2 1.2 1.2 1.2)

So the integral  function as follow:
Code: [Select]
(foo '(1.0 1.1 1.19 1.2 1.2 1.21 1.22 1.23 1.25 1.3 )0.05)
;0.05 is fuzz
-->(1.0 1.1 1.2 1.2 1.2 1.2 1.2 1.2 1.2 1.3 )

2.
Another original list as follow:
Code: [Select]
(foo '(1.0 1.1 1.19 1.2 1.21 1.22 1.23 1.25 1.3 )-->0.05)
;0.05 is fuzz

then we get the duplicate grouped list as follow:
Code: [Select]
((1.19 1.2 1.21 1.22 1.23 1.25))

And
Code: [Select]
if (1.19 1.2 1.21 1.22 1.23 1.25)
;;Nothing Appear more then two times.
then replace the list
-->(1.2167 1.2167 1.2167 1.2167 1.2167 1.2167)
;;1.2167 is the average value of the list.

So the integral  function as follow:
Code: [Select]
(foo '(1.0 1.1 1.19 1.2 1.21 1.22 1.23 1.25 1.3 )0.05)
;0.05 is fuzz
-->(1.0 1.1 1.2167 1.2167 1.2167 1.2167 1.2167 1.2167 1.3 )

The reason for doing that is I want to Calibrate the Coordinates of the Irregular table.
Maybe my method is useful.Every suggestion is welcome.
I think that will be a real Challenge.
Thanks!   :-D
« Last Edit: January 19, 2015, 10:32:29 PM by xiaxiang »

xiaxiang

  • Guest
Re: {Challenge}How to replace the duplicate items in the list
« Reply #1 on: January 19, 2015, 10:15:29 PM »
Do the trick?
Code - Auto/Visual Lisp: [Select]
  1. (defun Replace_Duplist(lst  fuzz / DuplicatesEq CountItems replace tmplst a b lst1)
  2.    ;;Find and group the duolicate items(with nth) in the given list
  3.    ;;Original by irneb
  4.    (defun DuplicatesEq  (LST fuzz / L n item group1 group2 result1 result2)  
  5.         (setq L (vl-sort LST '<=) n -1)  
  6.         (while (setq item (car L))    
  7.         (setq L (cdr L) n (1+ n))  
  8.         (while (and L (equal item (car L) fuzz))  
  9.                 (setq   group1 (cons item group1)
  10.                         group2 (cons n group2)
  11.                         n (1+ n)
  12.                         item  (car L)          
  13.                         L     (cdr L)
  14.                 )
  15.         )
  16.         (if group1    
  17.         (setq result1 (cons (reverse (cons item group1)) result1)
  18.                  result2 (cons (reverse (cons n group2)) result2)
  19.                 group1  nil
  20.                 group2  nil
  21.         )
  22.         )
  23.         )  
  24.         (list(reverse result1)(reverse result2))
  25.      )
  26.   ;; Count Items  -  Lee Mac
  27.   ;; Returns a list of dotted pairs detailing the number of
  28.   ;; occurrences of each item in a supplied list.
  29.   (defun CountItems (l / c l r x)
  30.         (while l
  31.           (setq x (car l)
  32.                 c (length l)
  33.                 l (vl-remove-if (function (lambda (e)(equal e x 1e-5))) (cdr l))
  34.                 r (cons (cons x (- c (length l))) r)
  35.           )
  36.         )
  37.         (reverse r)
  38.   )
  39.   ;;Replace_nth  -  Lee Mac
  40.   (defun replace ( alist position newitem / i )
  41.         (setq i -1)
  42.         (mapcar '(lambda ( x ) (if (= position (setq i (1+ i))) newitem x)) alist)
  43.   )
  44.   ;*****************************************************************
  45.    (setq tmplst(DuplicatesEq lst fuzz)
  46.         a(car tmplst)
  47.         b(cadr tmplst)
  48.         lst1 (vl-sort lst '<=)
  49.    )
  50.   ;(princ tmplst)
  51.  
  52.   (if a
  53.           (progn
  54.                   (mapcar
  55.                         '(lambda (x y / tmplst1 tmplst2 v)
  56.                                 (setq tmplst1
  57.                                         (vl-remove-if
  58.                                                 (function (lambda (e)(= (cdr e) 1)))
  59.                                                 (CountItems x)
  60.                                         )
  61.                                 )
  62.                            (setq  tmplst2
  63.                                         (vl-sort-i  tmplst1
  64.                                                 (function (lambda (e1 e2)(> (cdr e1) (cdr e2))))
  65.                                         )
  66.                                 )
  67.                                 (setq v
  68.                                         (if     tmplst1
  69.                                                 (car (nth (nth 0 tmplst2) tmplst1))
  70.                                                 (/(apply '+ x)(length x))
  71.                                         )
  72.                                 )
  73.                                 (mapcar '(lambda(i) (setq lst1(replace lst1 i v)))y)
  74.                         )
  75.                         a b
  76.                   )
  77.                   lst1
  78.           )
  79.           lst
  80.   )
  81. )[/pre]

Code: [Select]
(Replace_Duplist '(1.0 1.1 1.19 1.2 1.2 1.21 1.22 1.23 1.25 1.3 2.0 2.1 2.19 2.2 2.2 2.21 2.22 2.23 2.25 2.31)0.05)
-->(1.0 1.1 1.2 1.2 1.2 1.2 1.2 1.2 1.2 1.3 2.0 2.1 2.2 2.2 2.2 2.2 2.2 2.2 2.2 2.31)

Code: [Select]
(Replace_Duplist '(1.0 1.1 1.19 1.2  1.21 1.22 1.23 1.25 1.3 2.0 2.1 2.19 2.2  2.21 2.22 2.23 2.25 2.31)0.05)
-->(1.0 1.1 1.21667 1.21667 1.21667 1.21667 1.21667 1.21667 1.3 2.0 2.1 2.21667 2.21667 2.21667 2.21667 2.21667 2.21667 2.31)
« Last Edit: January 19, 2015, 10:29:28 PM by xiaxiang »