Notifications
Clear all

[Closed] Select the nth-last-element in an array

Hi
I like to know what is the shorter and quicker way of doing this in MS and Python
I have an array and i want to select the 4-last-element of the array here it is my slow try

mysel =selection as array
  sel_length = mysel.count
  last_4_Ar = #()
  st_nm = sel_length - 3
  for  i in st_nm to mysel.count do (	ii = mysel[i] ;append last_4_Ar ii )
  select last_4_Ar   

Definitely there will be shorter and quicker way, i would be grateful if someone leave Python answer as well

Thanks in advance

3 Replies

Basically it’s good.

To make it faster try using collect and make the mysel.count as a variable like:

myselCount = mysel.Count
last_4_Ar = for i = st_nm to myselCount collect mysel[i]

You can try this:
arr = selection as array
for i = (arr.count – 3) to arr.count do selectmore arr[i]

 MZ1
end = selection.count
start = end - 3 
objs = for i = start to end collect selection[i]
select objs