Python 程式列印指定範圍內的所有負數
有時任務是從給定範圍內選擇負數。在這篇 Python 文章中,首先將範圍作為輸入,然後指定此範圍內的整數。然後,使用四種不同方法的四個不同示例從這些數字中僅選擇負數。在示例 1 中,負數被挑選出來並分離到另一個列表中。在示例 2 中,所有非負元素都被移除。在示例 3 中,排序列表被分割到零,並且只保留負數。在示例 4 中,使用過濾器選擇負數。
示例 1 - 選擇範圍內所有負數並將它們分離到另一個列表中進行列印
步驟 1 - 指定範圍內的最小和最大數字作為輸入。最小數應為負數,最大數應為正數。建立一個包含給定範圍內的所有整數的列表。
步驟 2 - 首先將所有負數分離到一個單獨的列表中。列印這些數字。
步驟 3 - 執行程式並檢查結果。
Python 檔案包含以下內容
lowNum=-50 highNum=60 mainlist=[] listNeg=[] #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): listNeg.append(item) print("\nThe Negative Elements in the Range :") print(listNeg)
檢視結果 - 示例 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 Negative Elements in the Range : [-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]
示例 2:移除所有非負元素並列印剩餘的負數
演算法
步驟 1 - 首先指定範圍內的最小和最大數字。最小數應為負數,最大數應為正數。建立一個包含給定範圍內的所有整數的列表。
步驟 2 - 首先建立一個排序列表的副本,然後從中刪除所有大於零的數字。
步驟 3 - 從主列表中列印所有剩餘的負數。
步驟 4 - 執行程式並檢查結果。
Python 檔案包含以下內容
lowNum=-40 highNum=50 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 Negative Elements in the List :") print(mainlist)
檢視結果 - 示例 2
開啟 cmd 視窗並執行 Python 檔案以檢視結果。
In the given range from -40 to 50 : 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] The Negative Elements in the 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]
示例 3 - 將排序列表分割到零並保留負數進行列印。
演算法
步驟 1 - 指定範圍內的最小和最大數字。最小數應為負數,最大數應為正數。建立一個包含輸入範圍內的所有整數的列表。
步驟 2 - 首先複製此列表,然後分割此列表並選擇小於零的數字。
步驟 3 - 列印列表副本中所有剩餘的負數。
步驟 4 - 執行程式並檢查結果。
Python 檔案包含以下內容
lowNum=-40 highNum=60 mainlist=[] listNeg=[] #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) listNeg=mainlist listNeg[:] = [item for item in listNeg if item < 0] print("\nThe negative Elements in the List :") print(listNeg)
檢視結果 - 示例 3
要在 cmd 視窗中檢視結果,請執行 Python 檔案。
In the given range from -40 to 60 : 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] The negative Elements in the 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]
圖 3:顯示命令視窗中的結果。
示例 4:使用過濾器選擇負數並過濾掉非負數
演算法
步驟 1 - 指定範圍內的最小和最大數字的值。最小數應為負數,最大數應為正數。建立一個包含此輸入範圍內的所有整數的列表。
步驟 2 - 使用帶有 lambda 函式的過濾器。
步驟 3 - 在新列表中過濾出負數。列印新列表中所有已過濾的負數。
步驟 4 - 執行程式並檢查結果。
Python 檔案包含以下內容
lowNum=-30 highNum=20 mainlist=[] listNeg=[] #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) listNeg = list(filter(lambda item: item < 0, mainlist)) print("\nThe Negative Elements in the Range :") print(listNeg)
檢視結果
開啟 cmd 視窗並執行 Python 檔案以檢視結果。
In the given range from -30 to 20 : The Main List : [-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] The Negative Elements in the Range : [-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]
在這篇 Python 文章中,我們透過四個不同的示例,介紹瞭如何從給定範圍內查詢負數並列印它們的方法。首先,在示例 1 中,負數被分離到另一個列表中。在示例 2 中,所有非負元素都被移除。在示例 3 中,列表被分割,並且只保留負數。在示例 4 中,使用過濾器選擇負數。