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

原创文章,作者:W7O6Z,如若转载,请注明出处:https://www.506064.com/n/129195.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
W7O6ZW7O6Z
上一篇 2024-10-03 23:26
下一篇 2024-10-03 23:26

相关推荐

  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • Python程序文件的拓展

    Python是一门功能丰富、易于学习、可读性高的编程语言。Python程序文件通常以.py为文件拓展名,被广泛应用于各种领域,包括Web开发、机器学习、科学计算等。为了更好地发挥P…

    编程 2025-04-29
  • Python购物车程序

    Python购物车程序是一款基于Python编程语言开发的程序,可以实现购物车的相关功能,包括商品的添加、购买、删除、统计等。 一、添加商品 添加商品是购物车程序的基础功能之一,用…

    编程 2025-04-29
  • 爬虫是一种程序

    爬虫是一种程序,用于自动获取互联网上的信息。本文将从如下多个方面对爬虫的意义、运行方式、应用场景和技术要点等进行详细的阐述。 一、爬虫的意义 1、获取信息:爬虫可以自动获取互联网上…

    编程 2025-04-29
  • Vb运行程序的三种方法

    VB是一种非常实用的编程工具,它可以被用于开发各种不同的应用程序,从简单的计算器到更复杂的商业软件。在VB中,有许多不同的方法可以运行程序,包括编译器、发布程序以及命令行。在本文中…

    编程 2025-04-29
  • Python一元二次方程求解程序

    本文将详细阐述Python一元二次方程求解程序的相关知识,为读者提供全面的程序设计思路和操作方法。 一、方程求解 首先,我们需要了解一元二次方程的求解方法。一元二次方程可以写作: …

    编程 2025-04-29
  • 如何使用GPU加速运行Python程序——以CSDN为中心

    GPU的强大性能是众所周知的。而随着深度学习和机器学习的发展,越来越多的Python开发者将GPU应用于深度学习模型的训练过程中,提高了模型训练效率。在本文中,我们将介绍如何使用G…

    编程 2025-04-29
  • Web程序和桌面程序的区别

    Web程序和桌面程序都是进行软件开发的方式,但是它们之间存在很大的区别。本文将从多角度进行阐述。 一、运行方式 Web程序运行于互联网上,用户可以通过使用浏览器来访问它。而桌面程序…

    编程 2025-04-29

发表回复

登录后才能评论