用于查找子列表总和的 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() 是数学模块中的特殊函数之一。然后可以使用 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

结论

在本文中,我们学习了如何使用四种不同的方法查找子列表的总和,即给定的开始和结束索引之间的总和。我们还学习了如何使用切片来获取列表的一部分。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值