Write a NumPy program to replace all numbers in a given array which is equal, less and greater to a given number

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

Write a NumPy program to replace all numbers in a given array which is equal, less and greater to a given number

الأجوبة

import numpy as np
nums = np.array([[5.54, 3.38, 7.99],
              [3.54, 8.32, 6.99],
              [1.54, 2.39, 9.29]])
print("Original array:")
print(nums)
n = 8.32
r = 18.32
print("\nReplace elements of the said array which are equal to ",n,"with",r)
print(np.where(nums == n, r, nums))
print("\nReplace elements with of the said array which are less than",n,"with",r)
print(np.where(nums < n, r, nums))
print("\nReplace elements with of the said array which are greater than",n,"with",r)
print(np.where(nums > n, r, nums))

Sample Output:

Original array:
[[5.54 3.38 7.99]
 [3.54 8.32 6.99]
 [1.54 2.39 9.29]]

Replace elements of the said array which are equal to  8.32 with 18.32
[[ 5.54  3.38  7.99]
 [ 3.54 18.32  6.99]
 [ 1.54  2.39  9.29]]

Replace elements with of the said array which are less than 8.32 with 18.32
[[18.32 18.32 18.32]
 [18.32  8.32 18.32]
 [18.32 18.32  9.29]]

Replace elements with of the said array which are greater than 8.32 with 18.32
[[ 5.54  3.38  7.99]
 [ 3.54  8.32  6.99]
 [ 1.54  2.39 18.32]]
هل كان المحتوى مفيد؟

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

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