قطع arrays في NumPy
- Previous Page مؤشرات arrays في NumPy
- Next Page أنواع البيانات في NumPy
قطع المجموعة
يعني قطع البايثون نقل العناصر من مؤشر معين إلى مؤشر آخر.
نرسل القطعة بدلاً من المؤشرات:[البداية:النهاية]
.
يمكننا أيضًا تعريف الخطوة، مثلما يلي:[البداية:النهاية:الخطوة]
.
إذا لم نرسل البداية، يعتبر 0.
إذا لم نرسل النهاية، يعتبر الطول للمجموعة في البعد.
إذا لم نرسل الخطوة، يعتبر 1.
Example
من التالية قص المكونات من المؤشر 1 إلى المؤشر 5 من المجموعة:
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[1:5])
Note:النتيجة تتضمن المؤشر البدءي، لكن لا تتضمن المؤشر النهائي.
Example
Cut the elements from index 4 to the end of the array:
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[4:])
Example
Cut the elements from the beginning to index 4 (excluding):
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[:4])
Negative Cutting
Use the minus operator to reference the index from the end:
Example
Cut the array from index 3 from the end to index 1 from the end:
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[-3:-1])
STEP
Please use the step value to determine the cutting step length:
Example
Return the elements from index 1 to index 5:
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[1:5:2])
Example
Return the elements at an interval in the array:
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[::2])
Cut 2-D Array
Example
Start from the second element, slice the elements from index 1 to index 4 (excluding):
import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) print(arr[1, 1:4])
Note:Remember that the index of the second element is 1.
Example
Return index 2 from two elements:
import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) print(arr[0:2, 2])
Example
Cut the index from two elements 1 to index 4 (excluding), which will return a 2-D array:
import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]) print(arr[0:2, 1:4])
- Previous Page مؤشرات arrays في NumPy
- Next Page أنواع البيانات في NumPy