Python 程序查找子列表的和


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

使用的方法

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

  • 使用 For 循环(暴力代码)

  • 使用累积和方法

  • 使用 sum() 函数

  • 使用 math.fsum() 函数

使用 For 循环(暴力代码)

算法(步骤)

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

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

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

  • 初始化一个变量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。

  • 如果上述 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

结论

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

更新于: 2023年1月31日

3K+ 浏览量

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.