Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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]
share|improve this question
add comment

1 Answer

up vote 7 down vote accepted
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.

share|improve this answer
6  
you should note that in this case the second : is optional –  MattDMo 16 hours ago
 
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 16 hours ago
 
@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 16 hours ago
 
@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] –  user3084006 16 hours ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.