当前分类:300例题>>正文

Python示例:将正数和负数放在单独的列表中

来源:互联网   更新时间:2023年6月22日  

Python 示例

用 For 循环、While 循环和函数编写一个 Python示例,将正数和负数放在单独的列表中,并给出一个实例。

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 程序中,用户输入了列表项= [12,-34,55,-87,67]

对于循环–第一次迭代:对于范围(0,5)中的 0。条件为真。因此,它进入 If 语句 If(NumList[0]>= 0)=>If(12>= 0)–条件为真 正.追加(NumList[0]) = >正= [12]

第二次迭代:对于范围(0,5)中的 1–条件为真 如果(NumList[1] > = 0) = >如果(-34>= 0)–条件为假。所以,它进入了 Else 块。 Negative . append(NumList[1])=>Negative =[-34]

第三次迭代:对于范围(0,5)中的 2–条件为真 如果(NumList[2] > = 0) = >如果(55>= 0)–条件为真 正。追加(55) = >正=【12,55】

第四次迭代:对于范围(0,5)中的 3-条件为真 如果(-87>= 0)–条件为假,则进入否则块。 阴性.追加(-87) = >阴性= [-34,-87]

第五次迭代:对于范围(0,5)中的 4-条件为真 如果(67>= 0)-条件为真 正.追加(67) = >正=【12,55,67】

第六次迭代:对于范围(5)中的 5–条件为假。所以它退出 Python For Loop

Python示例使用 While 循环将正数和负数放在单独的列表中

这个 Python示例将正数放在正数列表中,将负数放在负数列表中,与上面的相同。我们刚刚将 For Loop 替换为 While loop 。

# 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]
本文固定链接:https://6yhj.com/leku-p-4398.html  版权所有,转载请保留本地址!
[猜你喜欢]

标签: VScode