Python 程序查找列表中元素的总和

编写一个 Python 程序,使用内置方法、for 循环、while 循环、函数以及实际示例来查找列表元素的总和。

此程序允许用户输入列表的长度。接下来,我们使用 For 循环将数字添加到列表中。sum 函数返回 列表 中所有元素的总和

NumList = []

Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

total = sum(NumList)

print("\n The Sum of All Element in this List is : ", total)
Python Program to find Sum of Elements in a List

使用 math fsum 方法

import math

numberList = [10, 25, 40, 55, 60, 89, 98]

tot = math.fsum(numberList)

print(tot)
377.0

使用 numpy sum 函数

import numpy as np

numberList = [10, 25, 40, 55, 60, 89, 98]

tot = np.sum(numberList)

print(tot)
377

使用 pandas Series

import pandas as pd

numberList = [11, 250, -140, 555, 60, 89, 98]

tot = pd.Series(numberList).sum()

print(tot)
923

使用 statistics fsum 方法

import statistics

numberList = [11, 250, -340, 955, 690, 829, 98]

tot = statistics.fsum(numberList)

print(tot)
2493.0

Python 程序在不使用 sum() 方法的情况下查找列表中元素的总和

在此 Python 程序中,我们使用 For 循环 遍历此 NumList 中的每个元素。在循环中,我们将这些元素添加到 total 变量中。

NumList = []
total = 0

Number = int(input("Please enter the Length : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

for j in range(Number):
    total = total + NumList[j]

print("\n The Sum of All Element in this List is : ", total)
Please enter the Length : 5
Please enter the Value of 1 Element : 10
Please enter the Value of 2 Element : 20
Please enter the Value of 3 Element : 30
Please enter the Value of 4 Element : 40
Please enter the Value of 5 Element : 55

 The Sum of All Element in this List is :  155

使用 for 循环将列表的所有元素相加的程序

numberList = [20, 40, 50, 60, 80, 90]

tot = 0

for num in numberList:
    tot += num

print(tot)
340

使用 While 循环计算列表项的总和

此返回列表项总和的程序与上面相同。我们只是用 While 循环 替换了 For 循环。

NumList = [10, 20, -30, -40, 50, 100]
total = 0
j = 0

while(j < len(NumList)):
    total = total + NumList[j]
    j = j + 1
print(total)

使用 while 循环计算列表项总和的输出

110

Python 程序使用函数查找列表中元素的总和

此查找列表项总和的程序与第一个示例相同。但是,我们使用 函数Python 程序逻辑分开了。

def sum_of_list(NumList):
    total = 0
    for j in range(len(NumList)):
        total = total + NumList[j]
    return total


NumList = [19, 11, 32, 86, 567, 32, 9]

total = sum_of_list(NumList)
print(total)

使用函数计算列表项总和的输出

756

下面展示了编写上述代码的另一种更好的方法。

def sum_of_list(numList):
    total = 0
    for n in numList:
        total += n
    return total

numList = [19, 11, 32, 86, 567, 32, 9]

tot = sum_of_list(numList)
print(tot)
756

使用列表推导式

numberList = [22, 49, 222, -150, 160, 820, 97]

tot = sum([n for n in numberList])

print(tot)
1220

使用 lambda 和 reduce 函数

from functools import reduce

numberList = [22, 49, -50, 160, 820, 97]

tot = reduce(lambda x, y: x + y, numberList)

print(tot)
1098

使用递归查找列表中元素总和的程序

此示例代码在函数中使用 if else 语句,并递归调用该方法来计算列表总和。

def sumOfList(numList):
    if len(numList)  == 1:
        return numList[0]
    else:
        return numList[0] + sumOfList(numList[1:])


numList = [129, 211, 302, -86, 567, 32, 9]
total = sumOfList(numList)
print(total)
1164