I’m new to python and i don’t know how to find the smallest number in each array. The input is [ [2], [3, 4], [6, 5, 7], [4, 1, 8, 3] ]
and the output should be [2, 3, 5, 1]
. I have try this:
JavaScript
x
7
1
x = [ [2], [3, 4], [6, 5, 7], [4, 1, 8, 3] ]
2
minimal = x[0]
3
for i in range(len(x)):
4
if (x[i]< minimal):
5
minimal = x[i]
6
print(minimal)
7
And the output that i got is [2]
, i have no idea about this. Please help me…
Advertisement
Answer
First, we need to get individual lists from list x
using for lst in x:
. Then we find the minimum value of that list using min()
function & append it to your minimal
list using minimal.append(min(lst))
, then print the final minimal list
Now you will have your output as [2, 3, 5, 1]
Now try & understand this code:
JavaScript
1
8
1
x = [ [2], [3, 4], [6, 5, 7], [4, 1, 8, 3] ]
2
3
minimal = []
4
5
for lst in x:
6
minimal.append(min(lst))
7
print(minimal)
8