如何在没有numpy的情况下更改数组行的位置

How to change array rows places whithouth numpy

大家好,这里是我的代码:

n =[[34,2,55,24,22],[31,22,4,7,333],[87,74,44,12,48]]
for r in n:
   for c in r:
      print(c,end = " ")
   print()
   
sums=[]

for i in n:
  sum=0
  for num in i:
    sum+=int(num)
  sums.append(sum)
print(*sums)

print(*(min(row) for row in n))
    

这是它打印出来的内容:

34 2 55 24 22 
31 22 4 7 333 
87 74 44 12 48 
137 397 265
2 4 12

我需要更改最小数字和最大数字的行,因此这意味着第 1 行和第 2 行如下所示:

31 22 4 7 333
34 2 55 24 22 
87 74 44 12 48

#end result needs to look like this:

34 2 55 24 22 
31 22 4 7 333 
87 74 44 12 48 
137 397 265
2 4 12

31 22 4 7 333
34 2 55 24 22 
87 74 44 12 48

请帮助我,我不能使用 numpy,因为它不起作用我尝试使用它,但它给出的都是错误。

我假设您想要第一个索引为最大值的列表,最后一个索引为最小值的列表,

maxs = [max(i) for i in n]
mins = [min(i) for i in n]

max_idx = maxs.index(max(maxs))
min_idx = mins.index(min(mins))
n[max_idx], n[min_idx] = n[min_idx], n[max_idx]
# you need to think about when min_idx = max_idx 
# or when there's more than one max/min

如果你不介意numpy,你可以使用:

max_idx = np.argmax(np.max(n, axis=1))
min_idx = np.argmin(np.min(n, axis=1))