Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to change array rows places whithouth numpy

Hello everyone here is my code:

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))
    

And here is what it prints out:

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

I need to change row whith smallest number and bigest number so it means row 1 and 2 like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Please help me i cant use numpy because it doesnt work I tried using it but all it gives are errors.

>Solution :

I assume you want the list with max at the first index and the one with the min at the end,

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

If you don’t mind numpy, you can use:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading