简体   繁体   中英

Sum all numbers in a given range of a given list Python

How can I write a function to get the sum of the items in the given list between the indices a and b. For example give aList=[6,3,4,2,5] and a=1 , b=3 , the function should return 9. Here is my code:

def sumRange(L,a,b):
    sum= []
    L = [6,3,4,2,5]
    for i in range(a,b+1,1):
    sum +=L[i]
    return sum

You can achieve this with list slicing:

sum(your_list[a:b + 1])

Here, your_list[a:b+1] is a slice - a part of your list starting from the index a and ending with the index b , including the values at both indexes (this is why you need b + 1 ).

您可以简单地在python中使用索引切片和sum函数。

return sum(L[a:b])

It seems like you want do roll your own solution. You can do it like this (based on the code you had in your question):

def sumRange(L,a,b):                                                                                                                                                                                                
    sum = 0                                                                                                                                                                                                         
    for i in range(a,b+1,1):                                                                                                                                                                                        
        sum += L[i]                                                                                                                                                                                                  
    return sum                                                                                                                                                                                                      

L = [6,3,4,2,5]                                                                                                                                                                                                     
a = 1                                                                                                                                                                                                               
b = 3                                                                                                                                                                                                               

result = sumRange(L,a,b)                                                                                                                                                                                            

print "The result is", result

This program prints

The result is 9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM