Replacing an item in a list with another

Discussion in 'AutoCAD' started by Danv., Dec 13, 2004.

  1. Danv.

    CAB2k Guest

    Bill
    I did make a mistake in the call to my code. Sorry.
    Here are the corrected times.

    Elapsed time: 0.041000 seconds

    Command:
    Command: timer
    Elapsed time: 0.030000 seconds

    Command:
    Command: timer
    Elapsed time: 0.030000 seconds

    Command:
    Command: timer
    Elapsed time: 0.030000 seconds

    Command:
    Command: timer
    Elapsed time: 0.030000 seconds

    Command:
    Command: timer
    Elapsed time: 0.030000 seconds

    Command:
    Command: timer
    Elapsed time: 0.030000 seconds

    Command:
    Command: timer
    Elapsed time: 0.030000 seconds

    Command:
    Command: timer
    Elapsed time: 0.030000 seconds
     
    CAB2k, Dec 15, 2004
    #21
  2. Danv.

    BillZ Guest

    Maybe mine just looks cooler then. :)

    Try timing this version (which I was going to post if it was what Dan wanted.

    ;;12/15/04 Bill Zondlo
    ;;Program to merge like items from one assoc list to another.
    ;;l1, l2 = assoc lists of matched length and items to swap.
    ;;Aitm = assoc item number to replace. i.e.
    ;; lst1 = ((1 . "") (2 . "K") (1 . "") (5 . "ff") (1 . ""))
    ;; lst2 = ((1 . "A") (2 . "Not This") (1 . "Door") (5 . "Or This") (1 . "all internal doors"))
    ;;(swaplists lst1 lst2 1) will return ((1 . "A") (2 . "K") (1 . "Door") (5 . "ff") (1 . "all internal doors"))
    ;;
    (defun SwapLists (l1 l2 Aitm)
    (apply
    'append
    (mapcar
    '(lambda (a b)(if (= (car a) Aitm)(subst b a (list a))(list a))
    )
    l1 l2)
    )
    ) ;end defun

    Bill
     
    BillZ, Dec 15, 2004
    #22
  3. Danv.

    Doug Broad Guest

    Bill here is your code example:
    lst new))


    CAB2k,
    Where did Bill post that example?
    I can't find it in earlier in this thread.

    The only thing I can find that he posted was:
    Which doesn't make any sense whatsoever in solving the problem.
     
    Doug Broad, Dec 15, 2004
    #23
  4. Danv.

    CAB2k Guest

    Here you go.

    Command: timer
    Elapsed time: 0.041000 seconds

    Command:
    Command: timer
    Elapsed time: 0.050000 seconds

    Command:
    Command: timer
    Elapsed time: 0.040000 seconds

    Command:
    Command: timer
    Elapsed time: 0.050000 seconds

    Command:
    Command: timer
    Elapsed time: 0.050000 seconds

    Command:
    Command: timer
    Elapsed time: 0.051000 seconds

    Command:
    Command: timer
    Elapsed time: 0.041000 seconds

    Command:
    Command: timer
    Elapsed time: 0.051000 seconds
     
    CAB2k, Dec 15, 2004
    #24
  5. Danv.

    BillZ Guest

    Well you got me.

    I guess the mapcar thing evaluates each item from each list where the foreach only does one.
    I think the real difference is the append at the end that takes the time. Ant the fact that I "list" each item. I'll have to work with it some more.
    Still looks cooler. :)

    Thanks

    Bill
     
    BillZ, Dec 15, 2004
    #25
  6. Danv.

    Doug Broad Guest

    Bill,
    That would require both lists to have a 1 for 1 mapping as to
    number of elements. It also seems far from efficient. Instead
    consider this

    (defun SwapLists (l1 l2 Aitm)
    (mapcar
    '(lambda (a b)(if (= (car a) Aitm) b a)
    )
    l1 l2)
    ) ;end defun

    Which should do the same as yours but without all the clutter.
    Does yours do something else that I can't see?
     
    Doug Broad, Dec 15, 2004
    #26
  7. Danv.

    Doug Broad Guest

    Wow. That is wild. The post I see ends with
    Very strange. Anne is something going on?
     
    Doug Broad, Dec 15, 2004
    #27
  8. Danv.

    CAB2k Guest

    2 or 3 post after that one.
    Quote
    Reply From: BillZ
    Date: Dec/15/04 - 06:56 (EST)

    Re: Replacing an item in a list with another
    Danv,
    Sorry i missed your reply.
    If the answer is yes then how would I do it?<<<

    It would depend on the structure of the 2 lists.

    Are they the same length?
    Are the assoc 1's in the same order and the same quantity in each list.

    A sample of each list would be most helpful.

    If the lists are the same length and the assoc 1's are located in the same location in each list then:

    Command: (setq lt1 '((1 . "")(2 . "K")(1 . "")(5 . "ff")(1 . "")))
    ((1 . "") (2 . "K") (1 . "") (5 . "ff") (1 . ""))

    Command: (setq lt2 '((1 . "A")(2 . "Not This")(1 . "Door")(5 . "OR This")(1 .
    "all internal doors")))
    ((1 . "A") (2 . "Not This") (1 . "Door") (5 . "OR This") (1 . "all internal
    doors"))

    Command: (setq lt3 (apply 'append (mapcar '(lambda (a b)(if (= (car a) 1)(subst
    b a (list a))(list a))) lt1 lt2)))
    ((1 . "A") (2 . "K") (1 . "Door") (5 . "ff") (1 . "all internal doors"))

    Bill
     
    CAB2k, Dec 15, 2004
    #28
  9. Danv.

    CAB2k Guest

    Definitely looks cooler. :)

    I love the brevity of it too. I can take the mapcar lambda apart now but
    still have trouble building them.
     
    CAB2k, Dec 15, 2004
    #29
  10. Danv.

    BillZ Guest

    Doug,
    Maybe you need to reload that post as I had edited it once.


    Bill
     
    BillZ, Dec 15, 2004
    #30
  11. Danv.

    Doug Broad Guest

    Hi Bill,

    I unsubscribed from the newsgroup and redownloaded all the
    posts before I mentioned that I couldn't see your second code
    solution. I don't think it exists any longer, unless it is as an
    attachment that is only accessible on the web side. Strange.

    In any case, Mapcar is a good approach if the lists can be
    mapped. ;-)

    Regards,
    Doug
    Using OE
     
    Doug Broad, Dec 15, 2004
    #31
  12. Danv.

    BillZ Guest

    Doug,
    Once again your expertise has once again triumphed.

    I would have gotten it eventually. :)

    Thanks

    Bill
     
    BillZ, Dec 15, 2004
    #32
  13. Danv.

    BillZ Guest

    I had to run this now 5000 times just to get a reading.

    Elapsed time: 0.015000 seconds

    Woo Woooo

    Bill
     
    BillZ, Dec 15, 2004
    #33
  14. Danv.

    BillZ Guest

    Oh yeah,

    And thanks for the time.

    I hope it keeps me out of trouble from now on. :)

    Bill
     
    BillZ, Dec 15, 2004
    #34
  15. Danv.

    BillZ Guest

    Oh yeah,

    And thanks for the timer.

    I hope it keeps me out of trouble from now on. :)

    Bill
     
    BillZ, Dec 15, 2004
    #35
  16. Danv.

    Danv. Guest

    Thanks Bill and CAB2k for your efforts. I will take a look at both codes.
     
    Danv., Dec 17, 2004
    #36
  17. Danv.

    BillZ Guest

    You're welcome,

    Just in case you get lost in the thread, it's Doug Broad that gleaned the best one.

    (defun SwapLists (l1 l2 Aitm)
    (mapcar
    '(lambda (a b)(if (= (car a) Aitm) b a)
    )
    l1 l2)
    ) ;end defun


    Bill
     
    BillZ, Dec 17, 2004
    #37
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.