← Back to NumPy Course | Chapter 10: Boolean & Comparison | Lesson 6 of 6

np.nonzero() का इस्तेमाल

nonzero हर उस element की positions return करता है जो zero (या False) नहीं है।

In this page:

  1. np.nonzero()
Syntax
python
indices = np.nonzero(arr)

np.nonzero()

np.nonzero(a) प्रति dimension एक index array वाला एक tuple return करता है, list करते हुए कहाँ non-zero elements हैं। एक comparison के साथ combine होकर यह एक condition match करने वाली positions ढूंढता है। np.argwhere coordinates की rows की तरह same information देता है।

Note: np.count_nonzero यह count करने का एक fast तरीका है कि कितने elements non-zero हैं।

उदाहरण: np.nonzero()

python
import numpy as np

a = np.array([0, 3, 0, 5, 7])
print(np.nonzero(a))
print(np.count_nonzero(a))
m = np.array([[0, 1], [2, 0]])
print(np.argwhere(m))

# Output:
# (array([1, 3, 4]),)
# 3
# [[0 1]
#  [1 0]]
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि result एक tuple है
  2. 0 और False को non-zero treat करना
  3. values पर nonzero इस्तेमाल करना जब आपको values खुद चाहिए थीं
चैप्टर सारांश
  • nonzero index arrays return करता है
  • प्रति dimension एक array
  • argwhere coordinate rows return करता है
  • count_nonzero इन्हें count करता है
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.