import numpy as np a=[ [[2, 2] ,[2, 2] ,[2, 2] ,[2, 2] ], [[4 ,4] ,[4,4] , [4,4], [4,4]], [[3, 3] ,[3,3] ,[3,3] ,[3,3]], [[1, 1] ,[1,1] ,[1 ,1] ,[1,1]] ] x=np.array(a) for i in range(3): for j in range(i+1,4): if(x[i][0][0]>x[j][0][0]): temp=np.array(x[i])#交换数组中的元素的时候一定要这样写 #temp=x[i]这样写是错误的 这样temp只是x[i]的别名,没有起到交换的作用! x[i]=x[j] x[j]=temp print(x)
注意 交换的时候 注意不要用别名,这样不能真正的交换值!!!!