Python 查找子列表的和

Python 查找子列表的和

在本文中,我们将学习一个python程序来查找子列表的和。

使用的方法

以下是完成此任务的各种方法:

  • 使用for循环(Brute Code)

  • 使用累积和方法

  • 使用sum()函数

  • 使用math.fsum()函数

使用for循环(Brute Code)

步骤

以下是执行所需任务的算法/步骤:

  • 创建一个变量以存储输入列表。

  • 创建两个单独的变量来存储起始和结束索引。

  • 初始化一个变量 resultSum0 ,用于存储子列表的结果和。

  • 使用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。
  • 如果上述条件为真,则打印输入列表中给定终止索引处的元素。
  • 否则,打印给定终止索引和起始索引前一个元素之间的差值。

示例

以下程序使用累积求和方法返回子列表的求和,即从给定的起始和终止索引处的元素的求和。

# 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

结论

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程