Python程序打印列表中的正数
本文的任务是从整数列表中打印正数。在这篇Python文章中,我们将通过4个不同的例子,使用不同的方法来完成这个任务,然后打印这些正数。在示例1中,正数被挑选出来并放入另一个列表中。在示例2中,所有非正数元素都被移除。在示例3中,排序后的列表被分割到零,只保留正数。在示例4中,使用filter筛选出正数。
示例1 - 选择列表中所有正数,并将其分离到另一个列表中进行打印。
算法
步骤1 − 指定范围内的最小值和最大值。最小值应为负数,最大值应为正数。创建一个包含给定范围内的整数列表。
步骤2 − 首先将所有正数分离到一个单独的列表中。打印这些数字。
步骤3 − 运行程序,然后检查结果。
Python文件包含以下内容
lowNum=-100 highNum=100 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 Positive Elements in the List :") print(listPos)
查看结果 - 示例1
要在cmd窗口中查看结果,请运行Python文件。
In the given range from -100 to 100 : The Main List : [-100, -99, -98, -97, -96, -95, -94, -93, -92, -91, -90, -89, -88, -87, -86, -85, -84, -83, -82, -81, -80, -79, -78, -77, -76, -75, -74, -73, -72, -71, -70, -69, -68, -67, -66, -65, -64, -63, -62, -61, -60, -59, -58, -57, -56, -55, -54, -53, -52, -51, -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, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100] 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, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
图1:在命令窗口中显示结果。
示例2:移除所有非正数元素并打印剩余的正数。
算法
步骤1 − 指定范围内的最小值和最大值。最小值应为负数,最大值应为正数。创建一个包含给定范围内随机选择的100个整数的列表。
步骤2 − 首先对该列表进行排序,创建一个单独的排序列表,然后从中移除所有小于零的数字。
步骤3 − 打印排序列表中剩余的所有正数。
步骤4 − 运行程序,然后检查结果。
Python文件包含以下内容
lowNum=-1000 highNum=1000 mainlist=[] listPos=[] #Making the 100 random elements list with integers starting from lowNum and upto HighNum import random #Generate 100 random numbers between lowNum and highNum randomlist = random.sample(range(lowNum, highNum), 100) print("In the given range from ", lowNum, " to", highNum, " :") print("\nThe 100 element Random List :") print(randomlist) print("\nThe 100 element from Random List - Sorted:") randomListSorted= sorted(randomlist) print(randomListSorted) #dividing the main list into negatives and positives for item in randomlist: if (item <= 0): randomListSorted.remove(item) print("\nThe Positive Elements in the List :") print(randomListSorted)
查看结果 - 示例2
打开cmd窗口并运行python文件以查看结果。
In the given range from -1000 to 1000 : The 100 element Random List : [169, -420, 938, 508, 19, -232, -83, -118, 85, 639, -316, 940, 500, -708, 201, -301, 350, -350, -893, 690, 884, 412, 179, -768, -670, 737, -102, -760, -788, -968, -41, 591, -392, 551, 470, -912, -328, -697, -47, -229, -881, -69, 269, -771, 429, 686, -33, -844, 667, 962, 397, 110, -489, -550, -877, -136, -979, 955, -831, 151, -999, -584, -855, -667, -173, -883, -116, 521, 455, -237, 882, 82, 706, -500, 574, -992, 194, 778, 156, 974, 13, -401, -434, 891, 354, 863, 6, 417, 271, 599, 394, 457, -396, 552, 234, -904, 894, 221, 50, 627] The 100 element from Random List - Sorted: [-999, -992, -979, -968, -912, -904, -893, -883, -881, -877, -855, -844, -831, -788, -771, -768, -760, -708, -697, -670, -667, -584, -550, -500, -489, -434, -420, -401, -396, -392, -350, -328, -316, -301, -237, -232, -229, -173, -136, -118, -116, -102, -83, -69, -47, -41, -33, 6, 13, 19, 50, 82, 85, 110, 151, 156, 169, 179, 194, 201, 221, 234, 269, 271, 350, 354, 394, 397, 412, 417, 429, 455, 457, 470, 500, 508, 521, 551, 552, 574, 591, 599, 627, 639, 667, 686, 690, 706, 737, 778, 863, 882, 884, 891, 894, 938, 940, 955, 962, 974] The Positive Elements in the List : [6, 13, 19, 50, 82, 85, 110, 151, 156, 169, 179, 194, 201, 221, 234, 269, 271, 350, 354, 394, 397, 412, 417, 429, 455, 457, 470, 500, 508, 521, 551, 552, 574, 591, 599, 627, 639, 667, 686, 690, 706, 737, 778, 863, 882, 884, 891, 894, 938, 940, 955, 962, 974]
图2:显示正数作为最终结果。
示例3 - 将排序后的列表分割到零,并保留正数进行打印。
算法
步骤1 − 指定范围内的最小值和最大值。最小值应为负数,最大值应为正数。创建一个包含给定范围内随机选择的100个整数的列表。
步骤2 − 首先对该列表进行排序,创建一个单独的排序列表,然后将此列表分割到零。
步骤3 − 显示排序列表中剩余的所有正数。
步骤4 − 执行程序,然后检查结果。
Python文件包含以下内容
lowNum=-1000 highNum=1000 mainlist=[] listPos=[] #Making the 100 random elements list with integers starting from lowNum and upto HighNum import random #Generate 100 random numbers between lowNum and highNum randomlist = random.sample(range(lowNum, highNum), 100) print("In the given range from ", lowNum, " to", highNum, " :") print("\nThe 100 element Random List :") print(randomlist) randomListSorted= sorted(randomlist) print(randomListSorted) listPos[:] = [item for item in randomListSorted if item >= 0] print("\nThe Positive Elements in the List :") print(listPos)
查看结果 - 示例3
要在cmd窗口中查看结果,请运行Python文件。
In the given range from -1000 to 1000 : The 100 element Random List : [-263, 161, -346, -18, 187, 42, -385, 403, 609, -281, -787, -290, -819, -512, -953, -585, 850, -160, -380, -308, 959, 706, -434, -627, -31, -417, 358, -323, -333, -625, -468, 634, 283, -137, 692, -546, -747, -44, -749, -671, -8, 8, 628, 366, 710, -131, -438, -471, 347, 312, 491, -545, 299, -315, -41, -952, 843, 389, 180, -85, 738, 78, 569, -653, 978, 902, -875, -608, 782, -681, 848, -393, -339, -275, 514, 594, 368, -480, 435, 904, -693, 26, 543, -648, 477, 485, 880, -156, -966, 812, -933, -972, -96, -495, 503, 335, 276, -220, -149, -132] The Positive Elements in the List : [8, 26, 42, 78, 161, 180, 187, 276, 283, 299, 312, 335, 347, 358, 366, 368, 389, 403, 435, 477, 485, 491, 503, 514, 543, 569, 594, 609, 628, 634, 692, 706, 710, 738, 782, 812, 843, 848, 850, 880, 902, 904, 959, 978]
图3:在命令窗口中显示结果。
示例4:使用filter选择正数并过滤掉非正数。
算法
步骤1 − 设置范围内的最小值和最大值。最小值应为负数,最大值应为正数。创建一个包含给定范围内随机选择的100个整数的列表。
步骤2 − 使用带有lambda函数的filter。
步骤3 − 将正数过滤到一个单独的列表中。打印排序列表中所有过滤后的正数。
步骤4 − 运行程序,然后检查结果。
Python文件包含以下内容
lowNum=-1000 highNum=1000 mainlist=[] listPos=[] #Making the 100 random elements list with integers starting from lowNum and upto HighNum import random #Generate 100 random numbers between lowNum and highNum randomlist = random.sample(range(lowNum, highNum), 100) print("In the given range from ", lowNum, " to", highNum, " :") print("\nThe 100 element Random List :") print(randomlist) listPos = list(filter(lambda item: item >= 0, randomlist)) print("\nThe Positive Elements in the List :") print(listPos)
查看结果 - 示例4
打开cmd窗口并运行python文件以查看结果。
In the given range from -1000 to 1000 : The 100 element Random List : [556, -122, 228, -157, -340, 636, 408, 143, 957, -835, -688, -163, -786, -525, 518, -767, 877, 637, -60, -733, -379, 4, 348, -378, 671, -980, 119, -350, -167, -124, 593, 564, -641, 413, -3, -152, -916, 600, -132, 426, -553, -768, 561, 282, 952, -252, -209, 375, -674, 254, 11, 567, -679, -683, 28, 961, 211, 797, 848, -220, 724, -270, 471, -15, 835, -593, -840, 407, -543, 113, -23, -979, -541, -828, -534, -384, 730, -529, -506, -721, -905, -331, -429, -518, -831, 605, 298, 690, -436, 862, -140, 693, -719, -368, 161, 937, 79, -817, 204, -227] The Positive Elements in the List : [556, 228, 636, 408, 143, 957, 518, 877, 637, 4, 348, 671, 119, 593, 564, 413, 600, 426, 561, 282, 952, 375, 254, 11, 567, 28, 961, 211, 797, 848, 724, 471, 835, 407, 113, 730, 605, 298, 690, 862, 693, 161, 937, 79, 204]
图4:显示正数作为最终结果。
在这篇Python文章中,我们通过四个不同的例子,展示了如何从给定的列表中查找正数并打印它们的方法。首先,在示例1中,正数被分离到另一个列表中。在示例2中,所有非正数元素都被移除。在示例3中,排序后的列表被分割,并且只保留了正数。在示例4中,使用了filter来选择正数。