Author Topic: get position of item in a list  (Read 2255 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
get position of item in a list
« on: April 08, 2014, 09:31:00 AM »
hello all
its been a while and if one of you kind souls could give me some refresher assistance i would appreciate it.

im needing a way to find the position or positions of an item or items in a list

i can get the position of the first item in a list, but if there are more than 1 of the same item, well thats where im drawing a blank.

any guidance is appreciated

ronjonp

  • Needs a day job
  • Posts: 7531

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

andrew_nao

  • Guest
Re: get position of item in a list
« Reply #2 on: April 08, 2014, 09:46:10 AM »
thanks ron,

i dont know what im doing wrong but they arent working for me   :ugly:


Code: [Select]
(defun g ( k l / a )
  (if k
    (if (setq a (assoc (car k) l))
      (subst (cons (car k) (g (cdr k) (cdr a))) a l)
      (cons  (cons (car k) (g (cdr k) nil)) l)
    )
    (if l (list (1+ (car l))) '(1))
  )
)

(g "key" "list")

(g "orifice" mainlst)

consp error

ronjonp

  • Needs a day job
  • Posts: 7531
Re: get position of item in a list
« Reply #3 on: April 08, 2014, 09:55:00 AM »
Here's a simple example ... see if it works for you.


Code: [Select]
(defun _foo (key l / n out)
  (setq n -1)
  (foreach item l
    (setq n (1+ n))
    (if (equal item key)
      (setq out (cons n out))
    )
  )
  (reverse out)
)
;; (_foo 1 '(1 2 3 1 4 51 1 1 1 1))

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

andrew_nao

  • Guest
Re: get position of item in a list
« Reply #4 on: April 08, 2014, 10:08:48 AM »
thanks again Ron,
 that works.

what was i doing wrong with the first code?

ronjonp

  • Needs a day job
  • Posts: 7531
Re: get position of item in a list
« Reply #5 on: April 08, 2014, 10:15:48 AM »
thanks again Ron,
 that works.

what was i doing wrong with the first code?


My guess is your list is not associative '((1 . 5) (2 . 7)(4 . 6))  etc...

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: get position of item in a list
« Reply #6 on: April 08, 2014, 12:47:09 PM »
This may be of interest too
(vl-position  symbol list)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12928
  • London, England
Re: get position of item in a list
« Reply #7 on: April 08, 2014, 02:55:28 PM »
i can get the position of the first item in a list, but if there are more than 1 of the same item, well thats where im drawing a blank.

If I've understood:

Code: [Select]
(defun LM:vl-positions1 ( x l / i )
    (setq i -1)
    (vl-remove nil (mapcar '(lambda ( y ) (setq i (1+ i)) (if (equal x y) i)) l))
)

Code: [Select]
(defun LM:vl-positions2 ( x l / foo )
    (defun foo ( x l n )
        (if (setq l (cdr (member x l)))
            (cons (- n (length l) 1) (foo x l n))
        )
    )
    (foo x l (length l))
)

Code: [Select]
(defun LM:vl-positions3 ( x l / n r )
    (setq n (length l))
    (while (setq l (cdr (member x l)))
        (setq r (cons (- n (length l) 1) r))
    )
    (reverse r)
)

Code: [Select]
(defun LM:vl-positions4 ( x l / foo )
    (defun foo ( x l n )
        (cond
            (   (null l) nil)
            (   (equal x (car l)) (cons n (foo x (cdr l) (1+ n))))
            (   (foo x (cdr l) (1+ n)))
        )
    )
    (foo x l 0)
)

Code: [Select]
_$ (LM:vl-positions4 3 '(0 1 2 3 4 3 4 3 2 1 0))
(3 5 7)

andrew_nao

  • Guest
Re: get position of item in a list
« Reply #8 on: April 08, 2014, 03:18:46 PM »
@Cab, thats how i find the position for one item in the list, but the brain fart for me was getting the position of all the items in a list that are the same.

@Lee, thanks for your input, however using your code returns nil.

my list is a list of strings.

divtiply

  • Guest
Re: get position of item in a list
« Reply #9 on: April 08, 2014, 03:22:23 PM »
my two cents:

Code - Auto/Visual Lisp: [Select]
  1. (defun positions (x lst / n out)
  2.   (setq n 0)
  3.   (foreach y lst
  4.     (and (equal x y)
  5.          (setq out (cons n out)))
  6.     (setq n (1+ n)))
  7.   (reverse out))
« Last Edit: April 08, 2014, 03:29:23 PM by divtiply »

ronjonp

  • Needs a day job
  • Posts: 7531
Re: get position of item in a list
« Reply #10 on: April 08, 2014, 03:23:49 PM »
Pretty funny Lee .. my first version was almost identical  :)


Code: [Select]
(defun LM:vl-positions1 ( x l / i )
    (setq i -1)
    (vl-remove nil (mapcar '(lambda ( y ) (setq i (1+ i)) (if (equal x y) i)) l))
)


(defun _foo (key l / i)
  (setq i -1)
  (vl-remove 'nil
        (mapcar (function (lambda (x)
             (setq i (1+ i))
             (if (equal key x)
               i
             )
                )
           )
           l
        )
  )
)
;; (_foo 1 '(1 3 4 5 6 7 1 1 3 1))

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12928
  • London, England
Re: get position of item in a list
« Reply #11 on: April 08, 2014, 04:09:59 PM »
Pretty funny Lee .. my first version was almost identical  :)

Great minds and all that  :wink:

@Lee, thanks for your input, however using your code returns nil.

Please post your usage.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1454
  • Marco
Re: get position of item in a list
« Reply #12 on: April 09, 2014, 12:51:54 PM »
Other versions:
Code: [Select]
;
; Function: ALE_PositionS - 2014/04/09
(defun ALE_PositionS (i l / c r)
  (setq c -1)
  (vl-every
    (function
      (lambda (x)
        (setq c (1+ c))
        (if (equal i x) (setq r (cons c r)) T)
      )
    )
    l
  )
  (reverse r)
)
; Function: ALE_PositionS2 - 01/09/2005
;
; Version 1.00
;
; Description:
;   returns the positions of a item in a list (nth based)
;
; Arguments:
;   TstItm = Any AutoLISP symbol
;   In_Lst = A true list
;
; Return Values:
;   List or nil if TstItm is not member of the list
;
; Examples:
;   (setq alist '(0 1 2 3 4 3 5 3 6 3 3 7))
;
;   (ALE_PositionS 3 alist)
;   Returns: (3 5 7 9 10)
;
;
(defun ALE_PositionS2 (TstItm In_Lst / LstLen OutLst)
  (setq LstLen (1- (length In_Lst)))
  (while (vl-position TstItm In_Lst)
    (setq
      In_Lst (cdr (member TstItm In_Lst))
      OutLst (cons (- LstLen (length In_Lst)) OutLst)
    )
  )
  (reverse OutLst)
)