Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

remove only 1st occurance of item from list

48 REPLIES 48
Reply
Message 1 of 49
Anonymous
511 Views, 48 Replies

remove only 1st occurance of item from list

(setq AA (list 1 1 2 3 4 5 5 6 7))
returns (1 1 2 3 4 5 5 6 7)
(vl-remove 5 AA)
returns (1 1 2 3 4 6 7)

How do I remove only the first occurance of item from list?
48 REPLIES 48
Message 21 of 49
Anonymous
in reply to: Anonymous

and strangely 1 line of code works for all lists PROVIDED the list is not
supposed to have nils...

Jamie

"Marc'Antonio Alessi" wrote in message
news:6262082@discussion.autodesk.com...
{code}
; Marc'Antonio Alessi - http://xoomer.virgilio.it/alessi
; Function: ALE_List_ReplaceFirst - 22/01/2005
;
; Version 1.01
;
; 24/01/2005 - added new local (EndLst) to correct return
; value if OldItm is not member of In_Lst
;
; Description:
; returns a copy of the list with a new item substituted
; in place of the first old item in the list
; If NewItm = nil OldItm is removed
;
; Arguments:
; NewItm = An atom or list
; OldItm = An atom or list
; In_Lst = A list
; InRLst = Original list reversed
;
; Return Values:
; A list
; the original list if OldItm is not member of the list
;
; Examples:
; (setq alist '(0 1 2 3 4 3 5 3 6 3 3 7))
;
; (ALE_List_ReplaceFirst "NEW" 3 alist (reverse alist))
; Returns: (0 1 2 "NEW" 4 3 5 3 6 3 3 7)
;
; (ALE_List_ReplaceFirst '(9 . Z) 3 alist (reverse alist))
; Returns: (0 1 2 (9 . Z) 4 3 5 3 6 3 3 7)
;
; (ALE_List_ReplaceFirst nil 3 alist (reverse alist))
; Returns: (0 1 2 4 3 5 3 6 3 3 7)
;
(defun ALE_List_ReplaceFirst (NewItm OldItm In_Lst InRLst / NthPos EndLst)
(if (setq EndLst (member OldItm In_Lst))
(progn
(setq NthPos (- (length InRLst) (length EndLst)))
(while
(/=
NthPos
(length (setq InRLst (cdr (member OldItm InRLst))))
)
)
(append (reverse InRLst) (if NewItm (list NewItm)) (cdr EndLst))
)
In_Lst
)
)

Comando: (setq AA (list 1 1 2 3 4 5 5 6 5 7 5))
(1 1 2 3 4 5 5 6 5 7 5)

Comando: (ALE_List_ReplaceFirst nil 5 AA (reverse AA))
(1 1 2 3 4 5 6 5 7 5)

{code}

--
Marc'Antonio Alessi
(vl-string-translate "1234567890" "ie@mst.lan" "499825513610716")
http://xoomer.alice.it/alessi
> 2D Parametric for 2000-2010 <
(strcat "I like " (substr (ver) 8 4) "!")
GPS: N45° 50' 16" E12° 22' 44"
--
"Alan Henderson" <2009 at alan henderson dot org> ha scritto nel messaggio
news:6261300@discussion.autodesk.com...
(setq AA (list 1 1 2 3 4 5 5 6 7))
returns (1 1 2 3 4 5 5 6 7)
(vl-remove 5 AA)
returns (1 1 2 3 4 6 7)

How do I remove only the first occurance of item from list?
Message 22 of 49
Anonymous
in reply to: Anonymous

k

Jamie

wrote in message news:6261991@discussion.autodesk.com...
Jamie,

Believe my interpitation of the OP's question was short sighted but i gave
it a shot. And sorry for the bad lisp practice.
I did see your other post with advice about (append) and (cons) and will
keep that in mind. Here is some advice for you
chill out and try not to be so snorky in your posts.

hth
Southie
Message 23 of 49
Anonymous
in reply to: Anonymous

Sorry for the delay...
What I'm looking for is a way to remove the first of any duplicated item
from a list.
Note the list is always sorted, but doesn't have to be.
I am actually not using numbers, these are entity names, but it shouldn't
matter what the item type is.

Example -
(setq Llist (list 1 1 2 3 4 4 5 6 7 7 8 9))
(Remove_Item Llist 4) would return (list 1 1 2 3 4 5 6 7 7 8 9)
(Remove_Item Llist 1) would return (list 1 2 3 4 4 5 6 7 7 8 9)

Here is my brute force method -
(defun Remove_Item (NN List_Old / List_New KK KL KN)
(setq KN (vl-position NN List_Old))
(setq KK 0 KL (length List_Old))
;--get SLCtTaps before 1st occurance-----------
(while (< KK KN)
(setq List_New (append List_New (list (nth KK List_Old))))
(setq KK (1+ KK))
)
;--get SLCtTaps after 1st occurance------------
(setq KK (1+ KN))
(while (< KK KL)
(setq List_New (append List_New (list (nth KK List_Old))))
(setq KK (1+ KK))
)
;--return rebuilt list-------------------------
List_New
)

"Alan Henderson" <2009 at alan henderson dot org> wrote in message
news:6261300@discussion.autodesk.com...
(setq AA (list 1 1 2 3 4 5 5 6 7))
returns (1 1 2 3 4 5 5 6 7)
(vl-remove 5 AA)
returns (1 1 2 3 4 6 7)

How do I remove only the first occurance of item from list?
Message 24 of 49
Anonymous
in reply to: Anonymous

Hi,

What about triplets, or rather what should be done if an element is more
than doubled ? Should be my understanding that you want to have left only
one instance of an item, so then we can say that what we want is to remove
everything that's more than a single instance of a given item ?

--
Humans are born with a wide horizon.
As time goes by, the horizon narrows and
narrows, until it becomes a point of view.


"Alan Henderson" <2009 at alan henderson dot org> a écrit dans le message de
news: 6262285@discussion.autodesk.com...
Sorry for the delay...
What I'm looking for is a way to remove the first of any duplicated item
from a list.
Note the list is always sorted, but doesn't have to be.
I am actually not using numbers, these are entity names, but it shouldn't
matter what the item type is.

Example -
(setq Llist (list 1 1 2 3 4 4 5 6 7 7 8 9))
(Remove_Item Llist 4) would return (list 1 1 2 3 4 5 6 7 7 8 9)
(Remove_Item Llist 1) would return (list 1 2 3 4 4 5 6 7 7 8 9)

Here is my brute force method -
(defun Remove_Item (NN List_Old / List_New KK KL KN)
(setq KN (vl-position NN List_Old))
(setq KK 0 KL (length List_Old))
;--get SLCtTaps before 1st occurance-----------
(while (< KK KN)
(setq List_New (append List_New (list (nth KK List_Old))))
(setq KK (1+ KK))
)
;--get SLCtTaps after 1st occurance------------
(setq KK (1+ KN))
(while (< KK KL)
(setq List_New (append List_New (list (nth KK List_Old))))
(setq KK (1+ KK))
)
;--return rebuilt list-------------------------
List_New
)

"Alan Henderson" <2009 at alan henderson dot org> wrote in message
news:6261300@discussion.autodesk.com...
(setq AA (list 1 1 2 3 4 5 5 6 7))
returns (1 1 2 3 4 5 5 6 7)
(vl-remove 5 AA)
returns (1 1 2 3 4 6 7)

How do I remove only the first occurance of item from list?
Message 25 of 49
balisteor
in reply to: Anonymous

ok, so
As well as some buddy's question, Do you want to have to specify witch item to remove or do you just want to give the program a list and have it remove all but one duplicated items?

Yes, I do see that's the idea in your program I'm just wondering if that would be better for you or not. 😄
Message 26 of 49
Anonymous
in reply to: Anonymous

Okay here is the complete scenario.
I am writing a program to draw circuit lines between blocks.
At some of the blocks (junction boxes), I allow the user to specify how many
lines come out of the block.
If the user says 2, then I store 1 occurrence of the block in a list (the
other line is a continuation of the original line).
If the user says 3, then I store 2 occurrences of the block in the list.
After the user is done with the original line, the user then starts the
line(s) from any of the junction boxes (where I then remove one of the
occurrences of that specific block from the list).
I have to keep track of each junction box to make sure the user creates the
appropriate number of lines from each box.

I hope this is clearer than mud.

"Some Buddy" wrote in message
news:6262282@discussion.autodesk.com...
Hi,

What about triplets, or rather what should be done if an element is more
than doubled ? Should be my understanding that you want to have left only
one instance of an item, so then we can say that what we want is to remove
everything that's more than a single instance of a given item ?

--
Humans are born with a wide horizon.
As time goes by, the horizon narrows and
narrows, until it becomes a point of view.


"Alan Henderson" <2009 at alan henderson dot org> a écrit dans le message de
news: 6262285@discussion.autodesk.com...
Sorry for the delay...
What I'm looking for is a way to remove the first of any duplicated item
from a list.
Note the list is always sorted, but doesn't have to be.
I am actually not using numbers, these are entity names, but it shouldn't
matter what the item type is.

Example -
(setq Llist (list 1 1 2 3 4 4 5 6 7 7 8 9))
(Remove_Item Llist 4) would return (list 1 1 2 3 4 5 6 7 7 8 9)
(Remove_Item Llist 1) would return (list 1 2 3 4 4 5 6 7 7 8 9)

Here is my brute force method -
(defun Remove_Item (NN List_Old / List_New KK KL KN)
(setq KN (vl-position NN List_Old))
(setq KK 0 KL (length List_Old))
;--get SLCtTaps before 1st occurance-----------
(while (< KK KN)
(setq List_New (append List_New (list (nth KK List_Old))))
(setq KK (1+ KK))
)
;--get SLCtTaps after 1st occurance------------
(setq KK (1+ KN))
(while (< KK KL)
(setq List_New (append List_New (list (nth KK List_Old))))
(setq KK (1+ KK))
)
;--return rebuilt list-------------------------
List_New
)

"Alan Henderson" <2009 at alan henderson dot org> wrote in message
news:6261300@discussion.autodesk.com...
(setq AA (list 1 1 2 3 4 5 5 6 7))
returns (1 1 2 3 4 5 5 6 7)
(vl-remove 5 AA)
returns (1 1 2 3 4 6 7)

How do I remove only the first occurance of item from list?
Message 27 of 49
Kent1Cooper
in reply to: Anonymous

Don't several of the suggested routines [stevor's, two of mine, The Dark Princess's, Martti's, Marc-Antonio's if what you substitute is nil] already do that? Most of them put the two arguments in the reverse of the order you have them [item first, list second], but that's easily overcome.

--
Kent Cooper


Alan Henderson wrote...
....
Example -
(setq Llist (list 1 1 2 3 4 4 5 6 7 7 8 9))
(Remove_Item Llist 4) would return (list 1 1 2 3 4 5 6 7 7 8 9)
(Remove_Item Llist 1) would return (list 1 2 3 4 4 5 6 7 7 8 9)
....
Kent Cooper, AIA
Message 28 of 49
Anonymous
in reply to: Anonymous

(defun reduce_list (l1 / l2 l3 arg)
(setq l2 l1 l3 nil)
(while (> (length l2) 0)
(if (member (car l2) (cdr l2))
(setq l3 (cons (car l2) l3))
)
(setq l2 (vl-remove (car l2) l2))
)
(foreach arg l3
(setq l1 (vl-remove nil (mapcar '(lambda ( x ) (if (/= x arg) x (setq
arg nil))) l1)))
)
l1
)


testing:

Command: (reduce_list (list 1 2 3 2 2 4 3 3 5))
(1 2 2 4 3 3 5)

Command: (reduce_list (list 1 1 1 2 2 2 3 3 3 4 5 6 6 6 6 6 ))
(1 1 2 2 3 3 4 5 6 6 6 6)

Command: (reduce_list nil)
nil


is this what you want?

Jamie


"Alan Henderson" <2009 at alan henderson dot org> wrote in message
news:6262285@discussion.autodesk.com...
Sorry for the delay...
What I'm looking for is a way to remove the first of any duplicated item
from a list.
Note the list is always sorted, but doesn't have to be.
I am actually not using numbers, these are entity names, but it shouldn't
matter what the item type is.

Example -
(setq Llist (list 1 1 2 3 4 4 5 6 7 7 8 9))
(Remove_Item Llist 4) would return (list 1 1 2 3 4 5 6 7 7 8 9)
(Remove_Item Llist 1) would return (list 1 2 3 4 4 5 6 7 7 8 9)

Here is my brute force method -
(defun Remove_Item (NN List_Old / List_New KK KL KN)
(setq KN (vl-position NN List_Old))
(setq KK 0 KL (length List_Old))
;--get SLCtTaps before 1st occurance-----------
(while (< KK KN)
(setq List_New (append List_New (list (nth KK List_Old))))
(setq KK (1+ KK))
)
;--get SLCtTaps after 1st occurance------------
(setq KK (1+ KN))
(while (< KK KL)
(setq List_New (append List_New (list (nth KK List_Old))))
(setq KK (1+ KK))
)
;--return rebuilt list-------------------------
List_New
)

"Alan Henderson" <2009 at alan henderson dot org> wrote in message
news:6261300@discussion.autodesk.com...
(setq AA (list 1 1 2 3 4 5 5 6 7))
returns (1 1 2 3 4 5 5 6 7)
(vl-remove 5 AA)
returns (1 1 2 3 4 6 7)

How do I remove only the first occurance of item from list?
Message 29 of 49
Anonymous
in reply to: Anonymous

> and strangely 1 line of code works for all lists PROVIDED the list is not
> supposed to have nils...

Sorry I do not understand:

Comando: (setq AA (list 1 1 nil nil 2 3 4 5 5 6 5 7 5))
(1 1 nil nil 2 3 4 5 5 6 5 7 5)

Comando: (ALE_List_ReplaceFirst nil 5 AA (reverse AA))
(1 1 nil nil 2 3 4 5 6 5 7 5)

Comando: (ALE_List_ReplaceFirst nil 1 AA (reverse AA))
(1 nil nil 2 3 4 5 5 6 5 7 5)

Comando: (ALE_List_ReplaceFirst nil nil AA (reverse AA))
(1 1 nil 2 3 4 5 5 6 5 7 5)

Comando: (setq Llist (list 1 1 2 3 4 4 5 6 7 7 8 9))
(1 1 2 3 4 4 5 6 7 7 8 9)

Comando: (ALE_List_ReplaceFirst nil 4 Llist (reverse Llist))
(1 1 2 3 4 5 6 7 7 8 9)

Comando: (ALE_List_ReplaceFirst nil 1 Llist (reverse Llist))
(1 2 3 4 4 5 6 7 7 8 9)

What do you mean?


Marco


"The Dark Princess" ha scritto nel messaggio news:6262194@discussion.autodesk.com...
and strangely 1 line of code works for all lists PROVIDED the list is not
supposed to have nils...

Jamie

"Marc'Antonio Alessi" wrote in message
news:6262082@discussion.autodesk.com...
{code}
; Marc'Antonio Alessi - http://xoomer.virgilio.it/alessi
; Function: ALE_List_ReplaceFirst - 22/01/2005
;
; Version 1.01
;
; 24/01/2005 - added new local (EndLst) to correct return
; value if OldItm is not member of In_Lst
;
; Description:
; returns a copy of the list with a new item substituted
; in place of the first old item in the list
; If NewItm = nil OldItm is removed
;
; Arguments:
; NewItm = An atom or list
; OldItm = An atom or list
; In_Lst = A list
; InRLst = Original list reversed
;
; Return Values:
; A list
; the original list if OldItm is not member of the list
;
; Examples:
; (setq alist '(0 1 2 3 4 3 5 3 6 3 3 7))
;
; (ALE_List_ReplaceFirst "NEW" 3 alist (reverse alist))
; Returns: (0 1 2 "NEW" 4 3 5 3 6 3 3 7)
;
; (ALE_List_ReplaceFirst '(9 . Z) 3 alist (reverse alist))
; Returns: (0 1 2 (9 . Z) 4 3 5 3 6 3 3 7)
;
; (ALE_List_ReplaceFirst nil 3 alist (reverse alist))
; Returns: (0 1 2 4 3 5 3 6 3 3 7)
;
(defun ALE_List_ReplaceFirst (NewItm OldItm In_Lst InRLst / NthPos EndLst)
(if (setq EndLst (member OldItm In_Lst))
(progn
(setq NthPos (- (length InRLst) (length EndLst)))
(while
(/=
NthPos
(length (setq InRLst (cdr (member OldItm InRLst))))
)
)
(append (reverse InRLst) (if NewItm (list NewItm)) (cdr EndLst))
)
In_Lst
)
)

Comando: (setq AA (list 1 1 2 3 4 5 5 6 5 7 5))
(1 1 2 3 4 5 5 6 5 7 5)

Comando: (ALE_List_ReplaceFirst nil 5 AA (reverse AA))
(1 1 2 3 4 5 6 5 7 5)

{code}

--
Marc'Antonio Alessi
(vl-string-translate "1234567890" "ie@mst.lan" "499825513610716")
http://xoomer.alice.it/alessi
> 2D Parametric for 2000-2010 <
(strcat "I like " (substr (ver) 8 4) "!")
GPS: N45° 50' 16" E12° 22' 44"
--
"Alan Henderson" <2009 at alan henderson dot org> ha scritto nel messaggio
news:6261300@discussion.autodesk.com...
(setq AA (list 1 1 2 3 4 5 5 6 7))
returns (1 1 2 3 4 5 5 6 7)
(vl-remove 5 AA)
returns (1 1 2 3 4 6 7)

How do I remove only the first occurance of item from list?
Message 30 of 49
balisteor
in reply to: Anonymous

Hi Alan,
I have tried to make it so you can understand what is going on.
See if this simple routine is what your looking for, like you mentioned the order does not matter so order is ignored.

{code}
(defun remove-one ( item lst / newlist foundone )
(setq newlist (list))
(foreach n lst
(if (and
(equal item n)
(not foundone)
);a
(setq foundone T);<--skips the first "item" found.
(setq newlist (cons n newlist));<--add any and all other items.
);i
);fe
);d
{code}
Message 31 of 49
Anonymous
in reply to: Anonymous

Thanks to all who replied.
I was just hoping to find a simplier method, similar to vl-remove.

"Alan Henderson" <2009 at alan henderson dot org> wrote in message
news:6261300@discussion.autodesk.com...
(setq AA (list 1 1 2 3 4 5 5 6 7))
returns (1 1 2 3 4 5 5 6 7)
(vl-remove 5 AA)
returns (1 1 2 3 4 6 7)

How do I remove only the first occurance of item from list?
Message 32 of 49
Anonymous
in reply to: Anonymous

this isn't bad

(defun reduce_list (l1 / l2 l3 arg)
(setq l2 l1 l3 nil)
(while (> (length l2) 0)
(if (member (car l2) (cdr l2))
(setq l3 (cons (car l2) l3))
)
(setq l2 (vl-remove (car l2) l2))
)
(foreach arg l3
(setq l1 (vl-remove nil (mapcar '(lambda ( x ) (if (/= x arg) x (setq
arg nil))) l1)))
)
l1
)


this is easy too


(defun reduce_list (l1 rems /)
(foreach arg (if (atom rems)(list rems) rems)
(setq l1 (vl-remove nil (mapcar '(lambda ( x ) (if (/= x arg) x (setq
arg nil))) l1)))
)
l1
)

usage

Command: (reduce_list '( 1 1 2 3 3 1 5 6 1 5 3 7) 1)
(1 2 3 3 1 5 6 1 5 3 7)

or

Command: (reduce_list (list 1 1 2 3 3 1 5 6 1 5 3 7) ( list 1 3 5))
(1 2 3 1 6 1 5 3 7)


Jamie


ps if you gave more examples of the list sent and how it should be after
we'd all have a better idea.



"Alan Henderson" <2009 at alan henderson dot org> wrote in message
news:6262623@discussion.autodesk.com...
Thanks to all who replied.
I was just hoping to find a simplier method, similar to vl-remove.

"Alan Henderson" <2009 at alan henderson dot org> wrote in message
news:6261300@discussion.autodesk.com...
(setq AA (list 1 1 2 3 4 5 5 6 7))
returns (1 1 2 3 4 5 5 6 7)
(vl-remove 5 AA)
returns (1 1 2 3 4 6 7)

How do I remove only the first occurance of item from list?
Message 33 of 49
Anonymous
in reply to: Anonymous

Jamie,
In each of your examples, you are actually processing the list twice.
vl-remove-if requires processing only once. In the one immediately below,
you are actually making multiple copies of the list before working on it.

Compare perforance with this: (about 3x faster)
(defun removedups (lst)
(cond
((null lst) nil)
((member (car lst) (cdr lst)) (removedups (cdr lst)))
(t (cons (car lst) (removedups (cdr lst))))))

or, if vl-sort works to remove duplicates with the particular data type, it
if far faster (23x faster)

"The Dark Princess" wrote in message
news:6262625@discussion.autodesk.com...
this isn't bad

(defun reduce_list (l1 / l2 l3 arg)
(setq l2 l1 l3 nil)
(while (> (length l2) 0)
(if (member (car l2) (cdr l2))
(setq l3 (cons (car l2) l3))
)
(setq l2 (vl-remove (car l2) l2))
)
(foreach arg l3
(setq l1 (vl-remove nil (mapcar '(lambda ( x ) (if (/= x arg) x (setq
arg nil))) l1)))
)
l1
)


this is easy too


(defun reduce_list (l1 rems /)
(foreach arg (if (atom rems)(list rems) rems)
(setq l1 (vl-remove nil (mapcar '(lambda ( x ) (if (/= x arg) x (setq
arg nil))) l1)))
)
l1
)

usage

Command: (reduce_list '( 1 1 2 3 3 1 5 6 1 5 3 7) 1)
(1 2 3 3 1 5 6 1 5 3 7)

or

Command: (reduce_list (list 1 1 2 3 3 1 5 6 1 5 3 7) ( list 1 3 5))
(1 2 3 1 6 1 5 3 7)


Jamie


ps if you gave more examples of the list sent and how it should be after
we'd all have a better idea.



"Alan Henderson" <2009 at alan henderson dot org> wrote in message
news:6262623@discussion.autodesk.com...
Thanks to all who replied.
I was just hoping to find a simplier method, similar to vl-remove.

"Alan Henderson" <2009 at alan henderson dot org> wrote in message
news:6261300@discussion.autodesk.com...
(setq AA (list 1 1 2 3 4 5 5 6 7))
returns (1 1 2 3 4 5 5 6 7)
(vl-remove 5 AA)
returns (1 1 2 3 4 6 7)

How do I remove only the first occurance of item from list?
Message 34 of 49
Anonymous
in reply to: Anonymous

good point. i wanted to do it using mapcar... I didn't want to use sort as
I'm guessing that the order is important/

this is pretty clean and fast

(defun reduce_list ( l1 args / l2 a1 )
(if (atom args) (setq args (list args)))
(foreach a1 l1
(if (member a1 args)
(setq args (vl-remove a1 args))
(setq l2 (cons a1 l2))
)
)
(reverse l2)
)

testing:


Command: (reduce_list '( 1 1 2 3 3 1 5 6 1 5 3 7) 1)*Cancel*

Command: (reduce_list '( 1 1 2 3 3 1 5 6 1 5 3 7) 1)
(1 2 3 3 1 5 6 1 5 3 7)

Command: (reduce_list (list 1 1 2 3 3 1 5 6 1 5 3 7) ( list 1 3 5))
(1 2 3 1 6 1 5 3 7)

Jamie

"Doug Broad" wrote in message
news:6262737@discussion.autodesk.com...
Jamie,
In each of your examples, you are actually processing the list twice.
vl-remove-if requires processing only once. In the one immediately below,
you are actually making multiple copies of the list before working on it.

Compare perforance with this: (about 3x faster)
(defun removedups (lst)
(cond
((null lst) nil)
((member (car lst) (cdr lst)) (removedups (cdr lst)))
(t (cons (car lst) (removedups (cdr lst))))))

or, if vl-sort works to remove duplicates with the particular data type, it
if far faster (23x faster)

"The Dark Princess" wrote in message
news:6262625@discussion.autodesk.com...
this isn't bad

(defun reduce_list (l1 / l2 l3 arg)
(setq l2 l1 l3 nil)
(while (> (length l2) 0)
(if (member (car l2) (cdr l2))
(setq l3 (cons (car l2) l3))
)
(setq l2 (vl-remove (car l2) l2))
)
(foreach arg l3
(setq l1 (vl-remove nil (mapcar '(lambda ( x ) (if (/= x arg) x (setq
arg nil))) l1)))
)
l1
)


this is easy too


(defun reduce_list (l1 rems /)
(foreach arg (if (atom rems)(list rems) rems)
(setq l1 (vl-remove nil (mapcar '(lambda ( x ) (if (/= x arg) x (setq
arg nil))) l1)))
)
l1
)

usage

Command: (reduce_list '( 1 1 2 3 3 1 5 6 1 5 3 7) 1)
(1 2 3 3 1 5 6 1 5 3 7)

or

Command: (reduce_list (list 1 1 2 3 3 1 5 6 1 5 3 7) ( list 1 3 5))
(1 2 3 1 6 1 5 3 7)


Jamie


ps if you gave more examples of the list sent and how it should be after
we'd all have a better idea.



"Alan Henderson" <2009 at alan henderson dot org> wrote in message
news:6262623@discussion.autodesk.com...
Thanks to all who replied.
I was just hoping to find a simplier method, similar to vl-remove.

"Alan Henderson" <2009 at alan henderson dot org> wrote in message
news:6261300@discussion.autodesk.com...
(setq AA (list 1 1 2 3 4 5 5 6 7))
returns (1 1 2 3 4 5 5 6 7)
(vl-remove 5 AA)
returns (1 1 2 3 4 6 7)

How do I remove only the first occurance of item from list?
Message 35 of 49
Anonymous
in reply to: Anonymous

Try this:

{code}
; Marc'Antonio Alessi - http://xoomer.virgilio.it/alessi
; Function: ALE_RemoveDupOnLst
;
; Version 1.00 - 2006
;
; Description:
; removes all the duplicated elements leaving originals,
; faster on lists with low number of duplicates
; For example:
; (setq alist (atoms-family 1))
; (setq alist (append alist alist alist alist))
;
; Arguments:
; In_Lst = A list
;
(defun ALE_RemoveDupOnLst (In_Lst / OutLst)
(reverse
(foreach ForElm In_Lst
(if (vl-position ForElm OutLst)
OutLst
(setq OutLst (cons ForElm OutLst))
)
)
)
)
;
; Marc'Antonio Alessi - http://xoomer.virgilio.it/alessi
; Function: ALE_RemoveDupOnLst2
;
; Version 1.00 - 2006
;
; Description:
; removes all the duplicated elements leaving originals,
; faster on lists with high number of duplicates
; For example:
; (setq alist nil) (setq alist2 '(1 2 3 4 4 5 5 5 6 7 8 9 9 9 9))
; (setq alist (repeat 500 (setq alist (append alist alist2))))
;
; Arguments:
; In_Lst = A list
;
(defun ALE_RemoveDupOnLst2 (In_Lst / OutLst CarElm)
(while In_Lst
(setq
In_Lst (vl-remove (setq CarElm (car In_Lst)) (cdr In_Lst))
OutLst (cons CarElm OutLst)
)
)
(reverse OutLst)
)
{code}

--
Marc'Antonio Alessi
(vl-string-translate "1234567890" "ie@mst.lan" "499825513610716")
http://xoomer.alice.it/alessi
> 2D Parametric for 2000-2010 <
(strcat "I like " (substr (ver) 8 4) "!")
GPS: N45° 50' 16" E12° 22' 44"
--
"The Dark Princess" ha scritto nel messaggio news:6263025@discussion.autodesk.com...
good point. i wanted to do it using mapcar... I didn't want to use sort as
I'm guessing that the order is important/

this is pretty clean and fast

(defun reduce_list ( l1 args / l2 a1 )
(if (atom args) (setq args (list args)))
(foreach a1 l1
(if (member a1 args)
(setq args (vl-remove a1 args))
(setq l2 (cons a1 l2))
)
)
(reverse l2)
)

testing:


Command: (reduce_list '( 1 1 2 3 3 1 5 6 1 5 3 7) 1)*Cancel*

Command: (reduce_list '( 1 1 2 3 3 1 5 6 1 5 3 7) 1)
(1 2 3 3 1 5 6 1 5 3 7)

Command: (reduce_list (list 1 1 2 3 3 1 5 6 1 5 3 7) ( list 1 3 5))
(1 2 3 1 6 1 5 3 7)

Jamie

"Doug Broad" wrote in message
news:6262737@discussion.autodesk.com...
Jamie,
In each of your examples, you are actually processing the list twice.
vl-remove-if requires processing only once. In the one immediately below,
you are actually making multiple copies of the list before working on it.

Compare perforance with this: (about 3x faster)
(defun removedups (lst)
(cond
((null lst) nil)
((member (car lst) (cdr lst)) (removedups (cdr lst)))
(t (cons (car lst) (removedups (cdr lst))))))

or, if vl-sort works to remove duplicates with the particular data type, it
if far faster (23x faster)

"The Dark Princess" wrote in message
news:6262625@discussion.autodesk.com...
this isn't bad

(defun reduce_list (l1 / l2 l3 arg)
(setq l2 l1 l3 nil)
(while (> (length l2) 0)
(if (member (car l2) (cdr l2))
(setq l3 (cons (car l2) l3))
)
(setq l2 (vl-remove (car l2) l2))
)
(foreach arg l3
(setq l1 (vl-remove nil (mapcar '(lambda ( x ) (if (/= x arg) x (setq
arg nil))) l1)))
)
l1
)


this is easy too


(defun reduce_list (l1 rems /)
(foreach arg (if (atom rems)(list rems) rems)
(setq l1 (vl-remove nil (mapcar '(lambda ( x ) (if (/= x arg) x (setq
arg nil))) l1)))
)
l1
)

usage

Command: (reduce_list '( 1 1 2 3 3 1 5 6 1 5 3 7) 1)
(1 2 3 3 1 5 6 1 5 3 7)

or

Command: (reduce_list (list 1 1 2 3 3 1 5 6 1 5 3 7) ( list 1 3 5))
(1 2 3 1 6 1 5 3 7)


Jamie


ps if you gave more examples of the list sent and how it should be after
we'd all have a better idea.



"Alan Henderson" <2009 at alan henderson dot org> wrote in message
news:6262623@discussion.autodesk.com...
Thanks to all who replied.
I was just hoping to find a simplier method, similar to vl-remove.

"Alan Henderson" <2009 at alan henderson dot org> wrote in message
news:6261300@discussion.autodesk.com...
(setq AA (list 1 1 2 3 4 5 5 6 7))
returns (1 1 2 3 4 5 5 6 7)
(vl-remove 5 AA)
returns (1 1 2 3 4 6 7)

How do I remove only the first occurance of item from list?
Message 36 of 49
t.willey
in reply to: Anonymous

I felt left out, so here is my contribution.

{code}
(defun Remove ( item lst / tempItem tempList flag )

(setq flag T)
(while
(and
(setq tempItem (car lst))
flag
)
(setq lst (cdr lst))
(if (equal item tempItem)
(setq flag nil)
(setq tempList (cons tempItem tempList))
)
)
(append (reverse tempList) lst)
)
{code}

Results:
{quote}
Command: (remove 2 '(2 3 1 1 5))
(3 1 1 5)

Command: (remove 1 '(2 3 1 1 5))
(2 3 1 5)

Command: (remove 6 '(2 3 1 1 5))
(2 3 1 1 5)

Command: (remove 5 '(2 3 1 1 5))
(2 3 1 1)

Command: (remove 3 '(2 3 1 1 5))
(2 1 1 5)
{quote}
Message 37 of 49
Anonymous
in reply to: Anonymous

Marc the first function has a nice style. do you have the results of some
tests with it?

Jamie
"Marc'Antonio Alessi" wrote in message
news:6263599@discussion.autodesk.com...
Try this:

{code}
; Marc'Antonio Alessi - http://xoomer.virgilio.it/alessi
; Function: ALE_RemoveDupOnLst
;
; Version 1.00 - 2006
;
; Description:
; removes all the duplicated elements leaving originals,
; faster on lists with low number of duplicates
; For example:
; (setq alist (atoms-family 1))
; (setq alist (append alist alist alist alist))
;
; Arguments:
; In_Lst = A list
;
(defun ALE_RemoveDupOnLst (In_Lst / OutLst)
(reverse
(foreach ForElm In_Lst
(if (vl-position ForElm OutLst)
OutLst
(setq OutLst (cons ForElm OutLst))
)
)
)
)
;
; Marc'Antonio Alessi - http://xoomer.virgilio.it/alessi
; Function: ALE_RemoveDupOnLst2
;
; Version 1.00 - 2006
;
; Description:
; removes all the duplicated elements leaving originals,
; faster on lists with high number of duplicates
; For example:
; (setq alist nil) (setq alist2 '(1 2 3 4 4 5 5 5 6 7 8 9 9 9 9))
; (setq alist (repeat 500 (setq alist (append alist alist2))))
;
; Arguments:
; In_Lst = A list
;
(defun ALE_RemoveDupOnLst2 (In_Lst / OutLst CarElm)
(while In_Lst
(setq
In_Lst (vl-remove (setq CarElm (car In_Lst)) (cdr In_Lst))
OutLst (cons CarElm OutLst)
)
)
(reverse OutLst)
)
{code}

--
Marc'Antonio Alessi
(vl-string-translate "1234567890" "ie@mst.lan" "499825513610716")
http://xoomer.alice.it/alessi
> 2D Parametric for 2000-2010 <
(strcat "I like " (substr (ver) 8 4) "!")
GPS: N45° 50' 16" E12° 22' 44"
--
"The Dark Princess" ha scritto nel messaggio
news:6263025@discussion.autodesk.com...
good point. i wanted to do it using mapcar... I didn't want to use sort as
I'm guessing that the order is important/

this is pretty clean and fast

(defun reduce_list ( l1 args / l2 a1 )
(if (atom args) (setq args (list args)))
(foreach a1 l1
(if (member a1 args)
(setq args (vl-remove a1 args))
(setq l2 (cons a1 l2))
)
)
(reverse l2)
)

testing:


Command: (reduce_list '( 1 1 2 3 3 1 5 6 1 5 3 7) 1)*Cancel*

Command: (reduce_list '( 1 1 2 3 3 1 5 6 1 5 3 7) 1)
(1 2 3 3 1 5 6 1 5 3 7)

Command: (reduce_list (list 1 1 2 3 3 1 5 6 1 5 3 7) ( list 1 3 5))
(1 2 3 1 6 1 5 3 7)

Jamie

"Doug Broad" wrote in message
news:6262737@discussion.autodesk.com...
Jamie,
In each of your examples, you are actually processing the list twice.
vl-remove-if requires processing only once. In the one immediately below,
you are actually making multiple copies of the list before working on it.

Compare perforance with this: (about 3x faster)
(defun removedups (lst)
(cond
((null lst) nil)
((member (car lst) (cdr lst)) (removedups (cdr lst)))
(t (cons (car lst) (removedups (cdr lst))))))

or, if vl-sort works to remove duplicates with the particular data type, it
if far faster (23x faster)

"The Dark Princess" wrote in message
news:6262625@discussion.autodesk.com...
this isn't bad

(defun reduce_list (l1 / l2 l3 arg)
(setq l2 l1 l3 nil)
(while (> (length l2) 0)
(if (member (car l2) (cdr l2))
(setq l3 (cons (car l2) l3))
)
(setq l2 (vl-remove (car l2) l2))
)
(foreach arg l3
(setq l1 (vl-remove nil (mapcar '(lambda ( x ) (if (/= x arg) x (setq
arg nil))) l1)))
)
l1
)


this is easy too


(defun reduce_list (l1 rems /)
(foreach arg (if (atom rems)(list rems) rems)
(setq l1 (vl-remove nil (mapcar '(lambda ( x ) (if (/= x arg) x (setq
arg nil))) l1)))
)
l1
)

usage

Command: (reduce_list '( 1 1 2 3 3 1 5 6 1 5 3 7) 1)
(1 2 3 3 1 5 6 1 5 3 7)

or

Command: (reduce_list (list 1 1 2 3 3 1 5 6 1 5 3 7) ( list 1 3 5))
(1 2 3 1 6 1 5 3 7)


Jamie


ps if you gave more examples of the list sent and how it should be after
we'd all have a better idea.



"Alan Henderson" <2009 at alan henderson dot org> wrote in message
news:6262623@discussion.autodesk.com...
Thanks to all who replied.
I was just hoping to find a simplier method, similar to vl-remove.

"Alan Henderson" <2009 at alan henderson dot org> wrote in message
news:6261300@discussion.autodesk.com...
(setq AA (list 1 1 2 3 4 5 5 6 7))
returns (1 1 2 3 4 5 5 6 7)
(vl-remove 5 AA)
returns (1 1 2 3 4 6 7)

How do I remove only the first occurance of item from list?
Message 38 of 49
Anonymous
in reply to: Anonymous

{code}
(progn
(gc)(gc)
(setq i 1 alist nil Ntimes 10000)
(repeat Ntimes (setq alist (cons i alist) i (1+ i)))
(setq alist (cons "X" alist))
(repeat Ntimes (setq alist (cons i alist) i (1+ i)))
(setq alist (cons "X" alist))
(repeat Ntimes (setq alist (cons i alist) i (1+ i)))
(Benchmark
'(
(ALE_RemoveDupOnLst alist)
(reduce_list alist "X")
)
)
(princ "\Length alist: ") (princ (length alist))
(princ "\n")(princ)
)

Elapsed milliseconds / relative speed for 32 iteration(s):

(REDUCE_LIST ALIST "X")..........1882 / 114.78
(ALE_REMOVEDUPONLST ALIST).....216011 / 1
Length alist: 30002



(progn
(gc)(gc)
(setq i 1 alist nil Ntimes 10000)
(repeat Ntimes (setq alist (cons i alist) i (1+ i)))
(setq alist (cons "X" alist))
(repeat Ntimes (setq alist (cons i alist) i (1+ i)))
(setq alist (cons "X" alist))
(repeat Ntimes (setq alist (cons i alist) i (1+ i)))
(setq alistR (reverse alist))
(Benchmark
'(
(ALE_List_ReplaceFirst nil "X" alist alistR)
(reduce_list alist "X")
)
)
(princ "\Length alist: ") (princ (length alist))
(princ "\n")(princ)
)

Elapsed milliseconds / relative speed for 64 iteration(s):

(ALE_LIST_REPLACEFIRST nil "X" ALIST...).....1091 / 3.41
(REDUCE_LIST ALIST "X")......................3725 / 1
Length alist: 30002


(progn
(gc)(gc)
(setq alist nil) (setq alist2 '(1 2 3 4 4 5 5 5 6 7 8 9 9 9 9))
(setq alist (repeat 500 (setq alist (append alist alist2))))
(Benchmark
'(
(ALE_RemoveDupOnLst alist)
(reduce_list alist 9)
(ALE_RemoveDupOnLst2 alist)
)
)
(princ "\Length alist: ") (princ (length alist))
(princ "\n")(princ)
)

Elapsed milliseconds / relative speed for 256 iteration(s):

(ALE_REMOVEDUPONLST2 ALIST).....2023 / 1.6
(ALE_REMOVEDUPONLST ALIST)......2103 / 1.54
(REDUCE_LIST ALIST 9)...........3245 / 1
Length alist: 7500


--
Marc'Antonio Alessi
(vl-string-translate "1234567890" "ie@mst.lan" "499825513610716")
http://xoomer.alice.it/alessi
> 2D Parametric for 2000-2010 <
(strcat "I like " (substr (ver) 8 4) "!")
GPS: N45° 50' 16" E12° 22' 44"
--
"The Dark Princess" ha scritto nel messaggio news:6263863@discussion.autodesk.com...
Marc the first function has a nice style. do you have the results of some
tests with it?
Message 39 of 49
Anonymous
in reply to: Anonymous

Very nice Marco.

"Marc'Antonio Alessi" wrote in message
news:6263939@discussion.autodesk.com...
{code}
(progn
(gc)(gc)
(setq i 1 alist nil Ntimes 10000)
(repeat Ntimes (setq alist (cons i alist) i (1+ i)))
(setq alist (cons "X" alist))
(repeat Ntimes (setq alist (cons i alist) i (1+ i)))
(setq alist (cons "X" alist))
(repeat Ntimes (setq alist (cons i alist) i (1+ i)))
(Benchmark
'(
(ALE_RemoveDupOnLst alist)
(reduce_list alist "X")
)
)
(princ "\Length alist: ") (princ (length alist))
(princ "\n")(princ)
)

Elapsed milliseconds / relative speed for 32 iteration(s):

(REDUCE_LIST ALIST "X")..........1882 / 114.78
(ALE_REMOVEDUPONLST ALIST).....216011 / 1
Length alist: 30002



(progn
(gc)(gc)
(setq i 1 alist nil Ntimes 10000)
(repeat Ntimes (setq alist (cons i alist) i (1+ i)))
(setq alist (cons "X" alist))
(repeat Ntimes (setq alist (cons i alist) i (1+ i)))
(setq alist (cons "X" alist))
(repeat Ntimes (setq alist (cons i alist) i (1+ i)))
(setq alistR (reverse alist))
(Benchmark
'(
(ALE_List_ReplaceFirst nil "X" alist alistR)
(reduce_list alist "X")
)
)
(princ "\Length alist: ") (princ (length alist))
(princ "\n")(princ)
)

Elapsed milliseconds / relative speed for 64 iteration(s):

(ALE_LIST_REPLACEFIRST nil "X" ALIST...).....1091 / 3.41
(REDUCE_LIST ALIST "X")......................3725 / 1
Length alist: 30002


(progn
(gc)(gc)
(setq alist nil) (setq alist2 '(1 2 3 4 4 5 5 5 6 7 8 9 9 9 9))
(setq alist (repeat 500 (setq alist (append alist alist2))))
(Benchmark
'(
(ALE_RemoveDupOnLst alist)
(reduce_list alist 9)
(ALE_RemoveDupOnLst2 alist)
)
)
(princ "\Length alist: ") (princ (length alist))
(princ "\n")(princ)
)

Elapsed milliseconds / relative speed for 256 iteration(s):

(ALE_REMOVEDUPONLST2 ALIST).....2023 / 1.6
(ALE_REMOVEDUPONLST ALIST)......2103 / 1.54
(REDUCE_LIST ALIST 9)...........3245 / 1
Length alist: 7500


--
Marc'Antonio Alessi
(vl-string-translate "1234567890" "ie@mst.lan" "499825513610716")
http://xoomer.alice.it/alessi
> 2D Parametric for 2000-2010 <
(strcat "I like " (substr (ver) 8 4) "!")
GPS: N45° 50' 16" E12° 22' 44"
--
"The Dark Princess" ha scritto nel messaggio
news:6263863@discussion.autodesk.com...
Marc the first function has a nice style. do you have the results of some
tests with it?
Message 40 of 49
Anonymous
in reply to: Anonymous

Hi, thanks.

"Doug Broad" ha scritto nel messaggio news:6264008@discussion.autodesk.com...
Very nice Marco.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost