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/zh-hant/n/129195.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
W7O6Z的頭像W7O6Z
上一篇 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

發表回復

登錄後才能評論