Author Topic: How to get nth item from list in list  (Read 3822 times)

0 Members and 1 Guest are viewing this topic.

vincent.r

  • Newt
  • Posts: 101
How to get nth item from list in list
« on: April 06, 2018, 02:05:17 AM »
Hi,

How to get and remove nth item from list in list ?

For example
Here is a list -
(("abcd" "bcda" "cdab" "dabc") ("wxyz" "xyzw" "yzwx" "zwxy") ("john" "ohnj" "hnjo" "njoh") ("john" "ohnj" "hnjo" "njoh") .........)

want to get 4th item i.e. ("john" "ohnj" "hnjo" "njoh")

and remove the same from list


Can anybody help ?

Thanks

ribarm

  • Gator
  • Posts: 3282
  • Marko Ribar, architect
Re: How to get nth item from list in list
« Reply #1 on: April 06, 2018, 02:56:07 AM »
Code: [Select]
(defun removenth ( l n / k )
  (setq k -1)
  (vl-remove nil (mapcar '(lambda ( x ) (if (/= (setq k (1+ k)) n) x)) l))
)

(defun itm ( l n )
  (nth n l)
)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: How to get nth item from list in list
« Reply #2 on: April 06, 2018, 04:43:43 AM »
I have these (sorry for the condensed formatting) :

Code - Auto/Visual Lisp: [Select]
  1. ; Few Matrix List Subfunctions:
  2.  
  3.  
  4. ; Returns list of (row column) - finds the position of a item
  5. ; _$ (_MatrixPosition "F" '(("A" "B" "C")("D" "E" "F")("G" "H" "I"))) -> (1 2)
  6. ; _$ (_MatrixPosition "R" '(("A" "B" "C")("D" "E" "F")("G" "H" "I"))) -> nil
  7. (defun _MatrixPosition ( itm aL / r c )
  8.   (setq r -1) (vl-some (function (lambda (x) (if (progn (setq r (1+ r)) (setq c (vl-position itm x))) (list r c)))) aL)
  9. )
  10.  
  11.  
  12. ; _$ (_MatrixNth '(1 2) '(("A" "B" "C")("D" "E" "F")("G" "H" "I"))) -> "F"
  13. ; _$ (_MatrixNth '(1 7) '(("A" "B" "C")("D" "E" "F")("G" "H" "I"))) -> nil
  14. (defun _MatrixNth ( NthL aL / r )
  15.   (if (and (vl-every 'vl-consp (list NthL aL)) (setq r (nth (car NthL) aL))) ; get row
  16.     (nth (cadr NthL) r) ; item
  17.   ); if
  18. ); defun _MatrixNth
  19.  
  20.  
  21. ; _$ (_MatrixSubstNth '(1 2) "K" '(("A" "B" "C")("D" "E" "F")("G" "H" "I"))) -> (("A" "B" "C") ("D" "E" "K") ("G" "H" "I"))
  22. ; _$ (_MatrixSubstNth '(1 8) "K" '(("A" "B" "C")("D" "E" "F")("G" "H" "I"))) -> (("A" "B" "C") ("D" "E" "F") ("G" "H" "I"))
  23. (defun _MatrixSubstNth ( NthL NewItm aL / r c )
  24.   (setq r -1) (mapcar (function (lambda (row) (setq r (1+ r)) (setq c -1) (mapcar (function (lambda (itm) (setq c (1+ c)) (if (equal (list r c) NthL 1e-1) NewItm itm))) row))) aL)
  25. ); defun _MatrixSubstNth
  26.  
  27.  
  28. ; _$ (_MatrixRemoveNth '(1 2) '(("A" "B" "C")("D" "E" "F")("G" "H" "I"))) -> (("A" "B" "C") ("D" "E") ("G" "H" "I"))
  29. ; _$ (_MatrixRemoveNth '(1 8) '(("A" "B" "C")("D" "E" "F")("G" "H" "I"))) -> (("A" "B" "C") ("D" "E" "F") ("G" "H" "I"))
  30. (defun _MatrixRemoveNth ( NthL aL / r c )
  31.   (setq r -1)
  32.   (mapcar
  33.     (function
  34.       (lambda (row) (setq r (1+ r)) (setq c -1)
  35.         (apply (function append) (mapcar (function (lambda (itm) (setq c (1+ c)) (if (not (equal (list r c) NthL 1e-1)) (list itm)))) row))
  36.       )
  37.     )
  38.     aL
  39.   )
  40. ); defun _MatrixRemoveNth
  41.  
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: How to get nth item from list in list
« Reply #3 on: April 06, 2018, 04:49:43 AM »
Code: [Select]
(defun removenth ( l n / k )
  (setq k -1)
  (vl-remove nil (mapcar '(lambda ( x ) (if (/= (setq k (1+ k)) n) x)) l))
)
what if there are some nil's in the source list?

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to get nth item from list in list
« Reply #4 on: April 06, 2018, 05:13:15 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun gc:removeAt (ind lst)
  2.   (if (or (zerop ind) (null lst))
  3.     (cdr lst)
  4.     (cons (car lst) (gc:removeAt (1- ind) (cdr lst)))
  5.   )
  6. )
Speaking English as a French Frog

ribarm

  • Gator
  • Posts: 3282
  • Marko Ribar, architect
Re: How to get nth item from list in list
« Reply #5 on: April 06, 2018, 05:59:29 AM »
Code: [Select]
(defun removenth ( l n / k )
  (setq k -1)
  (vl-remove nil (mapcar '(lambda ( x ) (if (/= (setq k (1+ k)) n) x)) l))
)
what if there are some nil's in the source list?

Code: [Select]
(defun removenth ( l n / k r )
  (setq k -1)
  (foreach e l
    (if (/= (setq k (1+ k)) n)
      (setq r (cons e r))
    )
  )
  (reverse r)
)
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

NICK_VNV

  • Newt
  • Posts: 63
Sorry for my English...

ziele_o2k

  • Newt
  • Posts: 49
Re: How to get nth item from list in list
« Reply #7 on: April 06, 2018, 06:33:57 AM »
source: CADPL-Pack-v1
Code: [Select]
; =========================================================================================== ;
; Usuwa element z listy / Removes the item from the list                                      ;
;  Pos [INT]  - pozycja elementu / element position                                           ;
;  Lst [LIST] - lista wejsciowa / input list                                                  ;
; ------------------------------------------------------------------------------------------- ;
; (cd:LST_RemoveItem 3 (list 0 1 2 3 4 5))                                                    ;
; =========================================================================================== ;
(defun cd:LST_RemoveItem (Pos Lst)
  (vl-remove-if
    (function
      (lambda (%)
        (= -1 (setq Pos (1- Pos)))
      )
    )
    Lst
  )
)

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: How to get nth item from list in list
« Reply #8 on: April 06, 2018, 09:15:35 AM »
 :uglystupid2: Old version vanilla Lisp:
Code: [Select]
; Function: ALE_List_RemoveNth
;
; Version 2.02 - 15/02/2008 > old name: ALE_RemoveNth
; Version 1.02 - 16/06/2007
; Version 1.00 - 2001
;
; Description:
;   returns a copy of the list without the nth item
;
; Arguments:
;   NthPos = Integer - nth like
;   In_Lst = A list
;   InRLst = Original list reversed
;
; Examples:
;
;   (setq alist '((0 . "A") (1 . "B") nil (3 . "D") (4 . "E") nil))
;   => ((0 . "A") (1 . "B") nil (3 . "D") (4 . "E") nil)
;
;   (ALE_List_RemoveNth 0 alist (reverse alist))
;   => ((1 . "B") nil (3 . "D") (4 . "E") nil)
;
;   (ALE_List_RemoveNth 5 alist (reverse alist))
;   => ((0 . "A") (1 . "B") nil (3 . "D") (4 . "E"))
;
;   (ALE_List_RemoveNth 6 alist (reverse alist))
;   => ((0 . "A") (1 . "B") nil (3 . "D") (4 . "E") nil)
;
(defun ALE_List_RemoveNth (NthPos In_Lst InRLst / LstLng OldItm)
  (cond
    ( (null In_Lst)                                nil )
    ( (zerop NthPos)                      (cdr In_Lst) )
    ( (<= (setq LstLng (length In_Lst)) NthPos) In_Lst )
    ( (zerop (setq LstLng (- LstLng (1+ NthPos))))
      (reverse (cdr InRLst))
    )
    ( (setq OldItm (nth NthPos In_Lst))
      (while (/= NthPos (length (setq InRLst (cdr (member OldItm InRLst))))))
      (while (/= LstLng (length (setq In_Lst (cdr (member OldItm In_Lst))))))
      (append (reverse InRLst) In_Lst)
    )
  )
)

vincent.r

  • Newt
  • Posts: 101
Re: How to get nth item from list in list
« Reply #9 on: April 06, 2018, 02:44:19 PM »
Thank you very much. You all are great.   :-)

One more little help.

This list (("abcd" "bcda" "cdab" "dabc") ("wxyz" "xyzw" "yzwx" "zwxy") ("john" "ohnj" "hnjo" "njoh") ("john" "ohnj" "hnjo" "njoh") .........)


want to remove sublist. result will be ("abcd" "bcda" "cdab" "dabc" "wxyz" "xyzw" "yzwx" "zwxy" "john" "ohnj" "hnjo" "njoh" "john" "ohnj" "hnjo" "njoh" .........)


MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to get nth item from list in list
« Reply #10 on: April 06, 2018, 02:53:12 PM »
Specifically:

(apply 'append this_list)

Generically this should work:

Code: [Select]
(defun _Atoms ( x )
    (cond
        ((null x) x)
        ((atom x) (list x))
        ((append (_Atoms (car x)) (_Atoms (cdr x))))
    )
)

(_Atoms this_list)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: How to get nth item from list in list
« Reply #11 on: April 06, 2018, 03:07:31 PM »
Interesting recursion, Michael !
Maybe OP wanted to retain the nils :

Code: [Select]
(defun mApplyAppend ( L )
  (while (vl-some 'vl-consp L)
    (setq L
      (apply (function append)
        (mapcar
          (function
            (lambda (x)
              (cond
                ( (vl-consp x) x)
                ( (list x) )
              )
            )
          )
          L
        )
      )
    )
  )
); defun mApplyAppend

Code: [Select]
_$ (_Atoms '(1 2 3 nil (4 5) (6 ((7 nil 8))) (((9)))))
(1 2 3 4 5 6 7 8 9)

_$ (mApplyAppend '(1 2 3 nil (4 5) (6 ((7 nil 8))) (((9)))))
(1 2 3 nil 4 5 6 7 nil 8 9)

But now at seeing the result, I remembered that Lee have LM:Flatten subfoo.   :roll:
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to get nth item from list in list
« Reply #12 on: April 06, 2018, 03:50:01 PM »
I believe the core algorythm is attributable to Doug Broad. As for retaining nils my experience is "generally not", but if so i pen something specifically for that context. 
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to get nth item from list in list
« Reply #13 on: April 06, 2018, 04:00:53 PM »
As for retaining nils my experience is "generally not", but if so i pen something specifically for that context.

Not uber efficient but I don't care enough to flesh it out more than this --

Code: [Select]
(defun _Atoms ( x )
    (if (or (null x) (atom x))
        (list x)
        (append (_Atoms (car x)) (_Atoms (cdr x)))
    )           
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to get nth item from list in list
« Reply #14 on: April 06, 2018, 04:21:18 PM »
For a general data distribution reversing the order of the null and atom test will realize better performance.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: How to get nth item from list in list
« Reply #15 on: April 06, 2018, 04:35:40 PM »
Code: [Select]
(defun _Atoms ( x / tmp )
  (if (atom x)
    (list x)
    (append (_Atoms (car x)) (if (setq tmp (cdr x)) (_Atoms tmp)))
  )
)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to get nth item from list in list
« Reply #16 on: April 06, 2018, 04:42:27 PM »
Nice if it works (Im on my phone).
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: How to get nth item from list in list
« Reply #17 on: April 06, 2018, 04:50:38 PM »
Nice if it works (Im on my phone).

Yup, it seems to..
BTW didn't ment to raise a problem about it, rather a possible requirement.  :tongue2:
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: How to get nth item from list in list
« Reply #18 on: April 06, 2018, 04:59:33 PM »
Worry not Grrr. Ive been burnt out for months; posting tends to be sporadic / minimal but Im grateful and enjoy the illumination no less that always springs from forum play. Thanks and cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: How to get nth item from list in list
« Reply #19 on: April 06, 2018, 05:07:02 PM »
Worry not Grrr. Ive been burnt out for months; posting tends to be sporadic / minimal but Im grateful and enjoy the illumination no less that always springs from forum play. Thanks and cheers.

Glad to hear that, Michael !
We all have our days - but one thing is sure: everyone gains knowledge/solutions by only participating in this forum. :)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg