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中,我們只取給定範圍內的一些隨機選擇的元素進行計算。