Python 程式查詢子列表的和
在本文中,我們將學習一個 Python 程式來查詢子列表的和。
使用的方法
以下是完成此任務的各種方法:
使用 For 迴圈(暴力程式碼)
使用累積和方法
使用 sum() 函式
使用 math.fsum() 函式
使用 For 迴圈(暴力程式碼)
演算法(步驟)
以下是執行所需任務的演算法/步驟:
建立一個變數來儲存輸入列表。
建立兩個單獨的變數來儲存起始和結束索引。
初始化一個變數resultSum為0,用於儲存子列表的結果和。
使用for 迴圈遍歷從給定起始索引到結束索引的範圍。
將迭代器索引處對應的值新增到上面定義的resultSum變數(從給定起始和結束索引的元素之和)
列印子列表的結果和(從起始到結束索引)。
示例
以下程式返回子列表的和,即使用 for 迴圈從給定起始和結束索引的元素之和:
# input list inputList = [3, 5, 10, 5, 2, 3, 1, 20] print("The Given List is:", inputList) # starting index start_index = 1 # ending index end_index = 5 # initializing a variable to 0 for storing the resultant sublist sum resultSum = 0 # traversing in the range from the given start index to the end index for k in range(start_index, end_index+1): # adding corresponding value at the iterator index to the resultSum variable resultSum += inputList[k] # Printing the resultant sum of sublist(from start to end index) print("The resultant sum of sublist is:", resultSum)
輸出
執行上述程式將生成以下輸出:
The Given List is: [3, 5, 10, 5, 2, 3, 1, 20] The resultant sum of sublist is: 25
使用累積和方法
使用累積和方法將前一個元素值新增到當前索引值。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用for 迴圈迴圈到輸入列表的長度,使用len() 函式(返回物件中的專案數)。
如果當前索引為 0,則在前面的索引處將沒有元素,因此使用 continue 語句繼續迭代。
否則將前一個元素的值新增到當前元素(累積和)。
使用if 條件語句檢查給定起始索引是否為 0。
如果上述 if 條件為真,則列印輸入列表中給定結束索引處的元素。
否則列印給定結束索引處的元素與起始索引前一個元素的差值。
示例
以下程式返回子列表的和,即使用累積和方法從給定起始和結束索引的元素之和:
# input list inputList = [3, 5, 10, 5, 2, 3, 1, 20] print("The Given List is:", inputList) # starting index start_index = 1 # ending index end_index = 5 # looping till the length of the input list for k in range(len(inputList)): # If it the index is 0 i.e first element then continue(do nothing) if(k == 0): continue else: # Else add the previous element value to the current element inputList[k] = inputList[k]+inputList[k-1] print("The resultant sum of sublist is:") # checking whether the given starting index is 0 if(start_index == 0): # printing the element at the given end index of the list print(inputList[end_index]) else: # Else printing the difference of elements at the given end index # and previous of start index print(inputList[end_index]-inputList[start_index-1])
輸出
The Given List is: [3, 5, 10, 5, 2, 3, 1, 20] The resultant sum of sublist is: 25
使用 sum() 函式
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用切片獲取從起始索引到結束索引的列表元素。
使用 sum() 函式(返回任何可迭代物件中所有專案的總和)列印子列表的和,即從給定起始索引到結束索引的元素之和。
示例
以下程式返回子列表的和,即使用 sum() 函式從給定起始和結束索引的元素之和:
# input list inputList = [3, 5, 10, 5, 2, 3, 1, 20] print("The Given List is:", inputList) start_index = 1 end_index = 5 print("The resultant sum of sublist is:") # Getting the list elements between start and end indices resultList = inputList[start_index:end_index+1] # Printing the sum of the sublist i.e printing above resultList sum print(sum(resultList))
輸出
The Given List is: [3, 5, 10, 5, 2, 3, 1, 20] The resultant sum of sublist is: 25
使用 math.fsum() 函式
fsum() 是 math 模組中的一個特殊函式之一。然後可以使用fsum()函式計算子列表的和。
Python 中的math.fsum()函式返回任何可迭代物件(如元組、陣列、列表等)中所有專案的總和。
示例
以下程式返回子列表的和,即使用 math.fsum() 函式從給定起始和結束索引的元素之和:
# importing math module import math # input list inputList = [3, 5, 10, 5, 2, 3, 1, 20] # starting index start_index = 1 # ending index end_index = 5 print("The Given List is: [3, 5, 10, 5, 2, 3, 1, 20]") print("The resultant sum of sublist is:") # Getting the list elements between start and end indices resultList = inputList[start_index:end_index+1] # Printing the sum of the sublist i.e printing above resultList sum print(math.fsum(resultList))
輸出
The Given List is: [3, 5, 10, 5, 2, 3, 1, 20] The resultant sum of sublist is: 25.0
結論
在本文中,我們學習瞭如何使用四種不同的方法查詢子列表的和,即給定起始和結束索引之間的和。我們還學習瞭如何使用切片獲取列表的一部分。