Python程式列印指定範圍內的所有正數
有時任務是從給定範圍內選擇所有正數。在本篇 Python 文章中,首先將範圍作為輸入,然後選擇此範圍內的負整數和正整數。在本篇 Python 文章中,然後使用四種不同示例中的不同方法從這些數字中僅選擇正數。在示例 1 中,選擇正數並將它們分離到另一個列表中。在示例 2 中,刪除所有非正元素。在示例 3 中,將排序列表拆分為零,並僅保留正數。在示例 4 中,使用過濾器選擇正數。
示例 1 - 選擇指定範圍內的所有正數並將這些正數分離到另一個列表中進行列印
演算法
步驟 1 − 指定範圍內的最小和最大數字作為輸入。最小數字應為負數,最大數字應為正數。建立給定範圍內的整數列表
步驟 2 − 首先將所有正數分離到一個單獨的列表中。列印這些數字。
步驟 3 − 執行程式,然後檢查結果。
Python 檔案包含以下內容
lowNum=-50 highNum=60 mainlist=[] listPos=[] #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 and positives for item in mainlist: if (item > 0): listPos.append(item) print("\nThe Negative Elements in the Range :") print(listPos)
檢視結果 - 示例 1
要檢視結果,請在 cmd 視窗中執行 Python 檔案。
In the given range from -50 to 60 : The Main List : [-50, -49, -48, -47, -46, -45, -44, -43, -42, -41, -40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60] The Positive Elements in the Range : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]
示例 2:刪除所有非正元素並列印剩餘的正數
演算法
步驟 1 − 首先輸入範圍內的最小和最大數字。最小數字應為負數,最大數字應為正數。建立給定範圍內的所有整數列表
步驟 2 − 首先將其複製為排序列表,然後從此列表中刪除所有小於或等於零的數字。
步驟 3 − 列印主列表中剩餘的所有正數。
步驟 4 − 執行程式,然後檢查結果。
Python 檔案包含以下內容
lowNum=-40 highNum=70 mainlist=[] mainlistcopy=[] #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) mainlistcopy=sorted(mainlist) for item in mainlistcopy: if (item <= 0): mainlist.remove(item) print("\nThe Positive Elements in the List :") print(mainlist)
檢視結果 - 示例 2
開啟 cmd 視窗並執行 python 檔案以檢視結果。
Enter a negative number for the start of a Range (example -100): -40 Enter a positive number for the end of a Range (example 100): 70 In the given range from -40 to 70 : The Main List : [-40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70] The Positive Elements in the List : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70]
示例 3 - 將排序列表拆分為零並保留正數以進行列印
演算法
步驟 1− 指定範圍內的最小和最大數字作為輸入。最小數字應為負數,最大數字應為正數。建立輸入範圍內的所有整數列表
步驟 2− 首先複製此列表,然後將此列表拆分為零。
步驟 3− 列印列表副本中剩餘的所有正數。
步驟 4− 執行程式,然後檢查結果。
Python 檔案包含以下內容
lowNum=-20 highNum=40 mainlist=[] listPos=[] #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) listPos=mainlist listPos[:] = [item for item in listPos if item > 0] print("\nThe Positive Elements in the List :") print(listPos)
檢視結果 - 示例 3
要檢視結果,請在 cmd 視窗中執行 Python 檔案。
Enter a negative number for the start of a Range (example -100): -20 Enter a positive number for the end of a Range (example 100): 40 In the given range from -20 to 40 : The Main List : [-20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40] The Positive Elements in the List : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40]
圖 3:顯示命令視窗中的結果。
示例 4:使用過濾器選擇正數並過濾掉非正數
演算法
步驟 1 − 提供範圍內的最小和最大數字的輸入。最小數字應為負數,最大數字應為正數。建立此輸入範圍內的所有整數列表
步驟 2 − 使用帶有 lambda 函式的過濾器。
步驟 3 − 在新列表中過濾出正數。列印新列表中所有已過濾的正數。
步驟 4 − 執行程式,然後檢查結果。
Python 檔案包含以下內容
lowNum=-50 highNum=50 mainlist=[] listPos=[] #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) listPos = list(filter(lambda item: item > 0, mainlist)) print("\nThe Positive Elements in the Range :") print(listPos)
檢視結果 - 示例 4
開啟 cmd 視窗並執行 python 檔案以檢視結果。
Enter a negative number for the start of a Range (example -100): -50 Enter a positive number for the end of a Range (example 100): 50 In the given range from -50 to 50 : The Main List : [-50, -49, -48, -47, -46, -45, -44, -43, -42, -41, -40, -39, -38, -37, -36, -35, -34, -33, -32, -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50] The Positive Elements in the Range : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]]
在本篇 Python 文章中,透過四個不同的示例,介紹瞭如何從給定範圍內查詢正數並列印這些正數的方法。首先,在示例 1 中,將正數分離到另一個列表中。在示例 2 中,刪除所有負元素。在示例 3 中,將列表拆分並僅保留正數。在示例 4 中,使用過濾器和 lambda 選擇正數。