Write a NumPy program to place a specified element in specified time randomly in a specified 2D array

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

Write a NumPy program to place a specified element in specified time randomly in a specified 2D array.

الأجوبة

import numpy as np
n = 4
i = 3
e = 10
array_nums1 = np.zeros((n,n))
print("Original array:")
print(array_nums1)
np.put(array_nums1, np.random.choice(range(n*n), i, replace=False), e)
print("\nPlace a specified element in specified time randomly:")
print(array_nums1)

Sample Output:

Original array:
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]

Place a specified element in specified time randomly:
[[10.  0.  0.  0.]
 [10.  0.  0.  0.]
 [ 0.  0.  0. 10.]
 [ 0.  0.  0.  0.]]
هل كان المحتوى مفيد؟

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

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