बाकी array की तरह ही , numpy array के elements की indexing भी 0 से start होती है। लेकिन इसमें हमें कुछ नई functionalities भी मिल जाती हैं जिससे array को manage करना थोड़ा easy हो जाता है।

Access NumPy Array Elements Example

Copy Fullscreen Close Fullscreen Run
import numpy as np
arr = np.array([12,23,34])
#select first element.
print(arr[0])
#last element.
print(arr[2]) 
Output
C:\Users\Rahulkumar\Desktop\python>python module_numpy.py
12
34

Access 2-dimensional Array Elements

इसी तरह से 2 - dimensional Array के elements को Access कर सकते हैं। यहाँ अच्छी चीज़ यह है कि value select करते समय आप , 2 से अधिक dimensions array के लिए [element, dimension] भी pass कर सकते हैं।


यह एक row , column की तरह work करता है -
For Example :

arr[0, 1] => means , 1st array element का 2nd element.
#it is same as : arr[0][1]
arr[2, 2] => means , 3rd array element का 3rd element.
#it is same as : arr[2][3]

Example

Copy Fullscreen Close Fullscreen Run
import numpy as np
arr = np.array([ [12,23,3],[43,2,89], [2,3,4], [45,56,45]])
print('2nd array element\'s 1st value')
#both are same.
print(arr[1][0])
print(arr[1, 0])

#once again.
print('4th array element\'s 3rd value')
print(arr[3][2])
print(arr[3, 2]) 
Output
C:\Users\Rahulkumar\Desktop\python>python module_numpy.py
2nd array element's 1st value
43
43
4th array element's 3rd value
45
45

ठीक 2 - dimensional Array की तरह ही आप इससे ज्यादा dimensions वाले array को इसी तरह से handle कर सकते हैं। जैसे 3 - dimensional array के लिए arr[0 , 1 , 2] = arr[0][1][2] same ही है।

Negative Indexing

numpy array के लिए negative indexing तो normal python list , tuple , string जैसे ही है। negative indexing में -1 का मतलब last element होता है।

Example

Copy Fullscreen Close Fullscreen Run
import numpy as np
#for one dimensional.
arr = np.array([12,23,34])
print('last element', arr[-1])
print('second last element', arr[-2])

#for 2 dimensional arry.
arr2 = np.array([ [12,23,3],[43,2,89], [2,3,4], [45,56,45]])
print('2nd array element\'s last value')
#both are same.
print(arr2[1, -1])
print(arr2[1][-1]) 
Output
C:\Users\Rahulkumar\Desktop\python>python module_numpy.py
last element 34
second last element 23
2nd array element's last value
89
89

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers