Write a NumPy program to calculate the difference between neighboring elements, element-wise, and prepend [0, 0] and append[200] to a given array

  • برمجة بايثون

Write a NumPy program to calculate the difference between neighboring elements, element-wise, and prepend [0, 0] and append[200] to a given array.

الأجوبة

import numpy as np
x = np.array([1, 3, 5, 7, 0])
print("Original array: ")
print(x)
r1 = np.ediff1d(x, to_begin=[0, 0], to_end=[200])
r2 = np.insert(np.append(np.diff(x), 200), 0, [0, 0])
assert np.array_equiv(r1, r2)
print("Difference between neighboring elements, element-wise, and prepend [0, 0] and append[200] to the said array:")
print(r2)

Sample Output:

Original array: 
[1 3 5 7 0]
Difference between neighboring elements, element-wise, and prepend [0, 0] and append[200] to the said array:
[  0   0   2   2   2  -7 200]
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...