مؤشر مصفوفات NumPy

زيارة عنصر المصفوفة

مؤشر المصفوفة يساوي الوصول إلى عنصر المصفوفة.

يمكنك الوصول إلى عنصر المصفوفة من خلال تسمية INDEX.

بداية مؤشرات NumPy هي 0، مما يعني أن INDEX الأول هو 0، والثاني هو 1، وهكذا.

Example

الحصول على الأول عنصر من النص:

import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[0])

Run Instance

Example

الحصول على الثاني عنصر من النص:

import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[1])

Run Instance

Example

الحصول على الثالث والرابع عشر عنصرين من النص ويضيفهما:

import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[2] + arr[3])

Run Instance

زيارة مصفوفة ثنائية الأبعاد

للوصول إلى عنصر مصفوفة ثنائية الأبعاد، يمكننا استخدام أرقام مفرغة بفواصل للتمثيل أبعاد العنصر وINDEX.

Example

زيارة الثاني عنصر في البعد الأول:

import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('الثاني عنصر في البعد الأول: ', arr[0, 1])

Run Instance

Example

زيارة الخامس عشر عنصر في البعد الثاني:

import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('الرابع عشر عنصر في البعد الثاني: ', arr[1, 4])

Run Instance

زيارة مصفوفة ثلاثية الأبعاد

للوصول إلى عنصر مصفوفة ثلاثية الأبعاد، يمكننا استخدام أرقام مفرغة بفواصل للتمثيل أبعاد العنصر وINDEX.

Example

Access the third element of the second array of the first array:

import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr[0, 1, 2])

Run Instance

Example Explanation

arr[0, 1, 2] Print Value 6.

Working Principle:

The first number represents the first dimension, which contains two arrays:

[[1, 2, 3], [4, 5, 6]]

Then:

[[7, 8, 9], [10, 11, 12]]

Because we selected 0Therefore, the remaining first array is:

[[1, 2, 3], [4, 5, 6]]

The second number represents the second dimension, which also contains two arrays:

[1, 2, 3]

Then:

[4, 5, 6]

Because we selected 1Therefore, the remaining second array is:

[4, 5, 6]

The third number represents the third dimension, which contains three values:

4
5
6

Because we selected 2Therefore, the final value obtained is the third value:

6

Negative Indexing

Use negative indexing to access the array from the end.

Example

Print the last element of the second dimension:

import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('Last element from 2nd dim: ', arr[1, -1])

Run Instance