Negative Indexing कैसे करें
Negative numbers अंत से backwards count करते हैं, इसलिए -1 last element है।
In this page:
Syntax
arr[-1]
arr[-2]
Negative Indexing
-1 का एक index last element है, -2 इससे पहले वाला, वगैरह। यह हर dimension में काम करता है। यह आपको len minus one compute करने से बचाता है।
Note:
arr[-1] last element पाने का idiomatic तरीका है।
उदाहरण: Negative indexing
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. #}
आम गलतियां
- -0 इस्तेमाल करना और last item की उम्मीद करना
- -len से आगे जाना
- यह भूल जाना कि 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: