← Back to NumPy Course | Chapter 4: Indexing & Slicing | Lesson 3 of 7

Negative Indexing कैसे करें

Negative numbers अंत से backwards count करते हैं, इसलिए -1 last element है।

In this page:

  1. Negative Indexing
Syntax
python
arr[-1]
arr[-2]

Negative Indexing

-1 का एक index last element है, -2 इससे पहले वाला, वगैरह। यह हर dimension में काम करता है। यह आपको len minus one compute करने से बचाता है।

Note: arr[-1] last element पाने का idiomatic तरीका है।

उदाहरण: Negative indexing

python
import numpy as np

a = np.array([5, 10, 15, 20])
print(a[-1], a[-2])
m = np.array([[1, 2], [3, 4]])
print(m[-1, -1])

# Output:
# 20 15
# 4
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. -0 इस्तेमाल करना और last item की उम्मीद करना
  2. -len से आगे जाना
  3. यह भूल जाना कि negative indices per axis काम करती हैं
चैप्टर सारांश
  • -1 last element है
  • हर axis पर काम करता है
  • Range -len से len-1 है
  • Manual length arithmetic रोकता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.