Python程序:统计数组中正数和负数

创新互联python教程:

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、网站空间、营销软件、网站建设、金东网站维护、网站推广。

编写一个 Python 程序来计算数组中的正数和负数,用于循环范围。if 条件(if(posnegarar[I]> = 0))检查数组项是否大于或等于零。如果为真,我们在正数组计数上加一;否则,将负数组计数加 1。

# Count Positive and Negative in Array

import numpy as np

posNegaArr = np.array([1, -19, -22, 16, -76, 0, 22, 50, -122, 70])
print("posNegaArr Array = ", posNegaArr)

posArrCount = 0
negArrCount = 0

for i in range(len(posNegaArr)):
    if (posNegaArr[i] >= 0):
        posArrCount = posArrCount + 1
    else:
        negArrCount = negArrCount + 1

print("The Count of Positive Numbers in posNegaArr Array = ", posArrCount)
print("The Count of Negative Numbers in posNegaArr Array  = ", negArrCount)

计算数组中正数和负数的 Python 程序

在这个 Python 示例中,我们使用 for 循环来迭代实际值,而不是索引位置。在第二个 for 循环中,numpy greater_equal 函数检查特定 numpy 数组项是否大于或等于零并返回 True。

# Count Positive and Negative in Array

import numpy as np

posNegaArr = np.array([4, 25, -9, 8, -6, -7, 0, 11, -17, 6, -44, 89])
posArrCount = posArrCount1 = 0
negaArrCount = negaArrCount1 = 0

for i in posNegaArr:
    if (i >= 0):
        posArrCount = posArrCount + 1
    else:
        negaArrCount = negaArrCount + 1

print("The Count of Positive Numbers in evennegaArr Array = ", posArrCount)
print("The Count of Negative Numbers in evennegaArr Array  = ", negaArrCount)

print("\n=== Using greater_equal function===")
for i in posNegaArr:
    if (np.greater_equal(i, 0) == True):
        posArrCount1 = posArrCount1 + 1
    else:
        negaArrCount1 = negaArrCount1 + 1

print("The Count of Positive Numbers in evennegaArr Array = ", posArrCount1)
print("The Count of Negative Numbers in evennegaArr Array  = ", negaArrCount1)

计算 Python Numpy 数组中的正数和负数,用于循环输出

The Count of Positive Numbers in evennegaArr Array =  7
The Count of Negative Numbers in evennegaArr Array  =  5

=== Using greater_equal function===
The Count of Positive Numbers in evennegaArr Array =  7
The Count of Negative Numbers in evennegaArr Array  =  5

Python 程序使用 While 循环计算 Numpy 数组中的正数和负数。

# Count Positive and Negative in Array

import numpy as np

posNegArr = np.array([418, -22, -33, 0, -89, -56, 11, -99])
i = 0

posArrCount = 0
negArrCount = 0

while (i < len(posNegArr)):
    if (np.greater_equal(posNegArr[i], 0) == True):
        posArrCount = posArrCount + 1
    else:
        negArrCount = negArrCount + 1
    i = i + 1

print("The Count of Positive Numbers in posNegArr Array = ", posArrCount)
print("The Count of Negative Numbers in posNegArr Array = ", negArrCount)

使用 while 循环输出 计算正负 Python Numpy 数组项目

The Count of Positive Numbers in posNegArr Array =  3
The Count of Negative Numbers in posNegArr Array =  5

在这个 Python numpy 数组的例子中,我们创建了一个函数(countevenodnumbers(posNegArr)),返回正数和负数的计数。

# Count Positive and Negative in Array

import numpy as np

def CountEvenOddNumbers(posNegArr):
    posArrCount = 0
    negArrCount = 0
    for i in posNegArr:
        if (np.greater_equal(i, 0) == True):
            posArrCount = posArrCount + 1
        else:
            negArrCount = negArrCount + 1

    return posArrCount, negArrCount

posNegArr = np.array([6, -9, -11, 8, 0, -14, -16, -9, 1])
positive, negative = CountEvenOddNumbers(posNegArr)

print("The Count of Positive Numbers in posNegArr Array = ", positive)
print("The Count of Negative Numbers in posNegArr Array = ", negative)
The Count of Positive Numbers in posNegArr Array =  4
The Count of Negative Numbers in posNegArr Array =  5

网页题目:Python程序:统计数组中正数和负数
标题路径:http://www.shufengxianlan.com/qtweb/news41/307491.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联