2

I want the last 4 values of this equation.

Is there a better way to this in one step, like modifying the equation or another extraction technique other than using delete

a=5
d=2

n = np.cumsum(d ** np.arange(1, a+1))
print 'n=', n
n = np.delete(n,0)
print 'n extracted=', n

n= [ 2  6 14 30 62]
n extracted= [ 6 14 30 62]

1 Answer 1

7
print n[-4::] 

will print the last 4 elements. You can do all kinds of array slicing like this, the above was just an example. You can index the array from the front or the back.

Sign up to request clarification or add additional context in comments.

4 Comments

you should note that in this case the second : is optional
Then that would mean I can also do this n = n[1:] Which way is more efficient in terms of performance slicing or deleting.
@user3084006 Is this the actual use case or is this a simplified example? I mean, do you always just have to remove the first element out of a 5 element list? Or will you have to remove 50 elements from a 20000 element list (for example)?
@Cyber it is a simplified case. I am just trying to remove x elements out something like n=n[len(n)-x,:], n = np.delete(n,index), index=[x]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.