Python程序:求列表中负数、正偶数和正奇数的和
有时任务需要将负数和正数分开,或者将奇数和偶数从整数列表中分离出来。在这篇Python文章中,我们将完成这两项任务。首先,将所有负数分离到另一个列表中。然后,将正奇数和正偶数分别分离到不同的列表中。为了计算总和,我们将从这些负数列表、正奇数列表和正偶数列表中取数,分别计算它们的和。在两个不同的示例中,我们将创建一个主要的整数列表。在示例1中,负数和正数在一个负数到正数的范围内指定。在示例2中,我们将从给定的负数到正数的范围内随机选择20个数,创建一个包含20个数字的随机列表。
示例1 - 求在指定范围内所有整数的列表中负数、正偶数和正奇数的和。
算法
步骤1 - 指定范围内的最小数和最大数。最小数应为负数,最大数应为正数。创建一个包含给定范围内的整数的列表。
步骤2 - 首先将所有负数分离到一个列表中。将这些数字相加。
步骤3 - 然后将所有正数分离到一个列表中。现在将这些正数进一步分离到奇数列表和偶数列表中。
步骤4 - 分别将正奇数列表中的数字和正偶数列表中的数字相加。打印所有列表及其计算出的和。
步骤5 - 运行程序,然后检查结果。
Python文件包含以下内容
lowNum=-10
highNum=10
mainlist=[]
listPositiveOdd=[]
listPositiveEven=[]
listNeg=[]
sumNeg=0
sumPositiveOdd=0
sumPositiveEven=0
#Making the main list with integers starting from lowNum and upto HighNum
for item in range(lowNum, highNum+1):
mainlist.append(item)
print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe Main List :")
print(mainlist)
#dividing the main list into negatives, positive odds and positive even
# and calculating their sums separately
for item in mainlist:
if (item > 0):
if (item%2 == 0):
listPositiveEven.append(item)
sumPositiveEven += item
else:
listPositiveOdd.append(item)
sumPositiveOdd += item
elif (item < 0):
listNeg.append(item)
sumNeg += item
print("\nThe Negative Elements in the List :")
print(listNeg)
print("\nThe Sum of all Negative Elements in the List :", sumNeg)
print("\nThe Positive Even Elements in the List :")
print(listPositiveEven)
print("\nThe Sum of all Positive Even Elements in the List :", sumPositiveEven)
print("\nThe Positive Odd Elements in the List :")
print(listPositiveOdd)
print("\nThe Sum of all Positive Odd Elements in the List :", sumPositiveOdd)
查看结果 - 示例1
要查看结果,请在cmd窗口中运行Python文件。
In the given range from -10 to 10 : The Main List : [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] The Negative Elements in the List : [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1] The Sum of all Negative Elements in the List : -55 The Positive Even Elements in the List : [2, 4, 6, 8, 10] The Sum of all Positive Even Elements in the List : 30 The Positive Odd Elements in the List : [1, 3, 5, 7, 9] The Sum of all Positive Odd Elements in the List : 25
图1:在命令窗口中显示结果。
示例2:求在给定范围内具有随机数的列表中负数、正偶数和正奇数的和。
算法
步骤1 - 指定范围内的最小数和最大数。最小数应为负数,最大数应为正数。创建一个包含从给定范围中随机选择的20个整数的列表。
步骤2 - 首先从这个随机列表中分离出所有负整数。将这些数字相加。
步骤3 - 然后从这个随机列表中分离出所有正整数。现在将这些正整数进一步分离到奇数列表和偶数列表中。
步骤4 - 分别将正奇数列表中的所有整数和正偶数列表中的所有整数相加。打印所有列表及其计算出的和。
步骤5 - 执行程序以打印所需的结果。
Python文件包含以下内容
lowNum=-100
highNum=200
mainlist=[]
listPositiveOdd=[]
listPositiveEven=[]
listNeg=[]
sumNeg=0
sumPositiveOdd=0
sumPositiveEven=0
#Making the 20 random elements list with integers starting from lowNum and upto #HighNum
import random
#Generate 20 random numbers between lowNum and highNum
randomlist = random.sample(range(lowNum, highNum), 20)
print("In the given range from ", lowNum, " to", highNum, " :")
print("\nThe 20 element Random List :")
print(randomlist)
#dividing the main list into negatives, positive odds and positive even
# and calculating their sums separately
for item in randomlist:
if (item > 0):
if (item%2 == 1):
listPositiveOdd.append(item)
sumPositiveOdd += item
else:
listPositiveEven.append(item)
sumPositiveEven += item
elif (item < 0):
listNeg.append(item)
sumNeg += item
print("\nThe Negative Elements in the List :")
print(listNeg)
print("\nThe Sum of all Negative Elements in the List :", sumNeg)
print("\nThe Positive Even Elements in the List :")
print(listPositiveEven)
print("\nThe Sum of all Positive Even Elements in the List :", sumPositiveEven)
print("\nThe Positive Odd Elements in the List :")
print(listPositiveOdd)
print("\nThe Sum of all Positive Odd Elements in the List :", sumPositiveOdd)
查看结果 - 示例2
打开cmd窗口并运行python文件以查看结果。
In the given range from -100 to 200 : The 20 element Random List : [117, -56, 28, 198, 36, 151, 155, 197, 56, -84, 133, 131, 97, 99, 4, 43, 80, 39, 47, 69] The Negative Elements in the List : [-56, -84] The Sum of all Negative Elements in the List : -140 The Positive Even Elements in the List : [28, 198, 36, 56, 4, 80] The Sum of all Positive Even Elements in the List : 402 The Positive Odd Elements in the List : [117, 151, 155, 197, 133, 131, 97, 99, 43, 39, 47, 69] The Sum of all Positive Odd Elements in the List : 1278
图2:显示列表的和和元素。
在这篇Python文章中,我们通过两个不同的示例说明了如何在给定列表中查找所有负数、所有正奇数和所有正偶数的和的方法。首先,在示例1中,我们创建了一个包含给定范围内的所有整数的主列表;然后,在示例2中,我们只取给定范围内的一些随机选择的元素进行计算。
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP