当前位置:首页 > 编程技术 > 正文内容

用于查找子列表总和的 Python 程序

yc8881年前 (2023-02-22)编程技术470

用于查找子列表总和的 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

结论

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


本站发布的内容若侵犯到您的权益,请邮件联系站长删除,我们将及时处理!


从您进入本站开始,已表示您已同意接受本站【免责声明】中的一切条款!


本站大部分下载资源收集于网络,不保证其完整性以及安全性,请下载后自行研究。


本站资源仅供学习和交流使用,版权归原作者所有,请勿商业运营、违法使用和传播!请在下载后24小时之内自觉删除。


若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,使用者自行承担,概与本站无关。


本文链接:https://www.10zhan.com/biancheng/10589.html

标签: Python
分享给朋友:

“用于查找子列表总和的 Python 程序” 的相关文章

【说站】laravel实现自定义404页面并给页面传值

【说站】laravel实现自定义404页面并给页面传值

以 laravel5.8 为例,虽然有自带的404页面,但太简单,我们更希望能自定义404页面,将用户留在站点。实现的方式很简单,将自定义的视图文件命名为 404.blade.php,并放到 reso...

【说站】Thymeleaf报错Error resolving template “XXX”

【说站】Thymeleaf报错Error resolving template “XXX”

修改了一下开源项目的目录结构访问突然报错Error resolving template “XXX”可能原因有如下三种:第一种可能:原因:在使用springboot的过程中,如果使用thymeleaf...

【说站】Centos8.0如何配置静态IP详解及永久关闭防火墙

【说站】Centos8.0如何配置静态IP详解及永久关闭防火墙

这篇文章主要介绍了详解Centos8 配置静态IP的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来学习一下!1. 查看自己的网关地址点击虚...

【说站】利用Webhook实现Java项目自动化部署

【说站】利用Webhook实现Java项目自动化部署

用webhook就能实现Java项目自动部署,其实原理很简单。费话不多说,直接往下看教程。1. 创建gitee仓库并初始化2. 在linux安装git3. 在宝塔的软件的商店里下载Webhook4....

【说站】电脑安装MySQL时出现starting the server失败原因及解决方案

【说站】电脑安装MySQL时出现starting the server失败原因及解决方案

今天在安装MySQL时出现starting the server失败,经过查询分析得出以下结论,记录一下操作步骤。原因分析:如果电脑是第一次安装MySQL,一般不会出现这样的报错。如下图所示。star...

【说站】vagrant实现linux虚拟机的安装并配置网络

【说站】vagrant实现linux虚拟机的安装并配置网络

一、VirtualBox的下载和安装1、下载VirtualBox官网下载:https://www.virtualbox.org/wiki/Downloads我的电脑是Windows的,所以下载Wind...