Python 程序将正数和负数放入单独的列表中

编写一个 Python 程序,使用 For 循环、While 循环和函数将正数和负数放入单独的列表中,并附带实际示例。

Python 程序使用 For 循环将正数和负数放入单独的列表中

在此 python 程序中,我们使用 For 循环来迭代给定列表中的每个元素。在 Python 循环内部,我们使用 If 语句检查列表项是正数还是负数。根据结果,我们将该项附加到正数列表或负数列表中。

# Python Program to Put Positive and Negative Numbers in Separate List

NumList = []
Positive = []
Negative = []

Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

for j in range(Number):
    if(NumList[j] >= 0):
        Positive.append(NumList[j])
    else:
        Negative.append(NumList[j])

print("Element in Positive List is : ", Positive)
print("Element in Negative List is : ", Negative)
Python Program to Put Positive and Negative Numbers in Separate List

在此 python 程序中,用户输入的 列表项 = [12, -34, 55, -87, 67]

For 循环 – 第一次迭代:for 0 in range(0, 5)。条件为 True。因此,它进入 If 语句
if(NumList[0] >= 0) => if(12 >= 0) – 条件为 True
Positive.append(NumList[0]) => Positive = [12]

第二次迭代:for 1 in range(0, 5) – 条件为 True
if(NumList[1] >= 0) => if(-34 >= 0) – 条件为 False。因此,它进入 Else 块。
Negative.append(NumList[1]) => Negative = [-34]

第三次迭代:for 2 in range(0, 5) – 条件为 True
if(NumList[2] >= 0) => if(55 >= 0) – 条件为 True
Positive.append(55) => Positive = [12, 55]

第四次迭代:for 3 in range(0, 5) – 条件为 True
if(-87 >= 0) – 条件为 False,它进入 Else 块。
Negative.append(-87) => Negative = [-34, -87]

第五次迭代:for 4 in range(0, 5) – 条件为 True
if(67 >= 0) – 条件为 True
Positive.append(67) => Positive = [12, 55, 67]

第六次迭代:for 5 in range(5) – 条件为 False。因此,它退出 Python For 循环

Python 程序使用 While 循环将正数和负数放入单独的列表中

这个将正数放入 Positive List 并将负数放入 Negative List 的 Python 程序与上面的相同。我们只是用 While 循环 替换了 For 循环

# Python Program to Put Positive and Negative Numbers in Separate List

NumList = []
Positive = []
Negative = []
j = 0

Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

while(j < Number):
    if(NumList[j] >= 0):
        Positive.append(NumList[j])
    else:
        Negative.append(NumList[j])
    j = j + 1

print("Element in Positive List is : ", Positive)
print("Element in Negative List is : ", Negative)

正数和负数在单独的列表中使用 while 循环的输出

Please enter the Total Number of List Elements : 6
Please enter the Value of 1 Element : 2
Please enter the Value of 2 Element : -3
Please enter the Value of 3 Element : -5
Please enter the Value of 4 Element : 9
Please enter the Value of 5 Element : -8
Please enter the Value of 6 Element : 7
Element in Positive List is :  [2, 9, 7]
Element in Negative List is :  [-3, -5, -8]

Python 程序使用函数将正数和负数放入单独的列表中

这个 Python 分离正负列表数字的例子与第一个例子相同。但是,我们使用 函数 分离了逻辑。请记住,您也可以编写一个函数来代替为正数和负数编写单独的函数。

# Python Program to Put Positive and Negative Numbers in Separate List
def positive_numbers(NumList):
    Positive = []
    for j in range(Number):
        if(NumList[j] >= 0):
            Positive.append(NumList[j])
    print("Element in Positive List is : ", Positive)

def negative_numbers(NumList):
    Negative = []
    for j in range(Number):
        if(NumList[j] < 0):
            Negative.append(NumList[j])
    print("Element in Negative List is : ", Negative)
    
NumList = []
Positive = []
Negative = []
j = 0
Number = int(input("Please enter the Total Number of List Elements : "))
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    NumList.append(value)

positive_numbers(NumList)
negative_numbers(NumList)

正数和负数在单独列表中的输出

Please enter the Total Number of List Elements : 6
Please enter the Value of 1 Element : 12
Please enter the Value of 2 Element : -23
Please enter the Value of 3 Element : -44
Please enter the Value of 4 Element : 67
Please enter the Value of 5 Element : -98
Please enter the Value of 6 Element : -3
Element in Positive List is :  [12, 67]
Element in Negative List is :  [-23, -44, -98, -3]