Write a NumPy program to create a 4x4 array, now create a new array from the said array swapping first and last, second and third columns

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

Write a NumPy program to create a 4x4 array, now create a new array from the said array swapping first and last, second and third columns.

Create the array using numpy.arange which returns evenly spaced values within a given interval.

الأجوبة

import numpy as np 
nums = np.arange(16, dtype='int').reshape(-1, 4)
print("Original array:")
print(nums)
print("\nNew array after swapping first and last columns of the said array:")
new_nums = nums[:, ::-1]
print(new_nums)

Sample Output:

Original array:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]
 [12 13 14 15]]

New array after swapping first and last columns of the said array:
[[ 3  2  1  0]
 [ 7  6  5  4]
 [11 10  9  8]
 [15 14 13 12]
هل كان المحتوى مفيد؟

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

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