Python - Sum with Start -


>>> sum([1,2,3]) 6 

failed attempt declare start position:

>>> sum([1,2,3],1) 7 

docs sum(iterable[, start])

can supply me example of using sum declaring start position please.

that start means sum starting value, not position on array:

sum():

sums start , items of iterable left right , returns total.

if want accomplish can use list slicing:

>>>sum([1,2,3][1:]) 5 

this slicing won't work iterator, in case can use enumerate() in generator expression. this:

>>> i=iter([1,2,3]) >>> sum(v i,v in enumerate(i) if >= 1) 5 

or better yet pointed out @lvc on comments, use itertools.islice() function slice iterator:

>>>import itertools >>> i=iter([1,2,3]) >>> sum(itertools.islice(i,1,none)) 5 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -