Home > Enterprise >  How to print minimal value from two dimensional array
How to print minimal value from two dimensional array

Time:05-06

Hello everyone here is my code:

n =[[34,2,55,24,22],[31,22,4,7,333]]
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)


    
mini = min([min(r) for r in n])
print(mini)
#This is what it prints out
 34 2 55 24 22 
 31 22 4 7 333 
 137 397
 2

As you can see it prints out smallest number from all array how i can print out smallest number from both rows i have tried using numpy but i have error and then i need to do something to files to fix it which i dont want to do can you please tell me another solution Last thing i need to print it out by changing rows like this and then all other stuff:

31 22 4 7 333
34 2 55 24 22
137 397
4 2

CodePudding user response:

You only need to get the minimum of each row, not the minimum of that.

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

CodePudding user response:

you can use numpy

>>> n = np.array([[34,2,55,24,22],[31,22,4,7,333]])
>>> n.min(axis=1)
array([2, 4])
>>> 
  • Related