Python 程序打印指定范围内的所有负数


有时任务是从给定范围内选择负数。在这篇 Python 文章中,首先将范围作为输入,然后指定此范围内的整数。然后,使用四种不同方法的四个不同示例从这些数字中仅选择负数。在示例 1 中,负数被挑选出来并分离到另一个列表中。在示例 2 中,所有非负元素都被移除。在示例 3 中,排序列表被分割到零,并且只保留负数。在示例 4 中,使用过滤器选择负数。

示例 1 - 选择范围内所有负数并将它们分离到另一个列表中进行打印

步骤 1 - 指定范围内的最小和最大数字作为输入。最小数应为负数,最大数应为正数。创建一个包含给定范围内的所有整数的列表。

步骤 2 - 首先将所有负数分离到一个单独的列表中。打印这些数字。

步骤 3 - 运行程序并检查结果。

Python 文件包含以下内容

Open Compiler
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]

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

示例 2:移除所有非负元素并打印剩余的负数

算法

步骤 1 - 首先指定范围内的最小和最大数字。最小数应为负数,最大数应为正数。创建一个包含给定范围内的所有整数的列表。

步骤 2 - 首先创建一个排序列表的副本,然后从中删除所有大于零的数字。

步骤 3 - 从主列表中打印所有剩余的负数。

步骤 4 - 运行程序并检查结果。

Python 文件包含以下内容

Open Compiler
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 文件包含以下内容

Open Compiler
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 文件包含以下内容

Open Compiler
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 中,使用过滤器选择负数。

更新于:2023年7月28日

浏览量:375

开启你的职业生涯

完成课程获得认证

开始学习
广告