I got a reference for the below snippet of program from a website, but going through it I am not able to understand the output:
data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
def fun(m):
v = m[0][0]
for row in m:
for element in row:
if v < element: v = element
return v
print(fun(data[0]))
I can figure couple of output through the print statement, e.g.
v = m[0][0] --> output 1
1st iteration
for element in row: --> output 1, 2
if v > element: v = element --> output 2.
But the 1st for loop is confusing for me as well the print statement outside the function.
Sorry for dumb question, but I need to clear my logic.
Thank you in advance
4 Answers 4
In your code
data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
subsequently data[0], that is passed to the function as m, is
[[1, 2], [3, 4]]
then
v = m[0][0]
is
1
Then your loop
for row in m:
for element in row:
if v < element: v = element
Here you're iterating over [[1, 2], [3, 4]] , and then over each element in nested list [1, 2] and then [3, 4], and trying to compare each number in the list to 1, so v becomes 2, then 3 and then 4, so upon the code prints
4
Comments
Given your input, the function will return the maximum value in the list of list, but it makes some assumptions on the format of your input.
This is that your m is a list of lists with the internal lists having numerical values in them. As the other answers say, the output for your case is 4 and the value of v will increase by one on each loop.
However, to give an example where v < element doesn't give True everytime if your input was [[1,4],[2,1]] the value of v will be as follows (the comments will give the values in order of the loop) :
# your input : m = [[1,4],[2,1]]
def fun(m):
v = m[0][0] # v = 1
for row in m: # v = 1, v = 4, v = 4, v = 4
for element in row:
if v < element: # False, True, False False
v = element
return v # v = 4
1 Comment
Just running the code print
4
Line by Line :
data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
We have a list of list of list (depth 3)
so : fum(data[0]) = fum([[1, 2], [3, 4]])
v = data[0][0][0] = 1
for row in data[0] means we iterate in the elements of [[1, 2], [3, 4]], so first element is [1,2] second element is [3,4]
for element in row means we will iterate in the elements of row so : element = 1, then 2, 3, 4
if v < element: v = element
Just means v takes the value of the max value of (v, element)
So this functions takes the first element of the list data and returns the max value of the n lists of m elements contained by the first element of data.
Comments
First, let's go over what's being passed to fun():
print(data[0]) # This outputs the first item in 'data[]'
# Output: [[1,2], [3,4]]
Now let's break down the first line of the function. We have v=m[0][0].
print(m)
# [[1,2], [3, 4]] (What we passed to the function)
print(m[0])
# [1,2] (The first item in the list)
print(m[0][0])
# 1 (The first item of the the first item in the original list)
So this line is just getting the very first number in the nested list.
Next, the for loops:
for row in m:
print(row)
# [1,2] (iter 1)
# [3,4] (iter 2)
This is just cycling through all the list items in the list that was passed to the function.
Then, for each of those cases, we're iterating through each number:
for row in m:
for element in row:
print(element)
# 1
# 2
# 3
# 4
Lastly, the if statement is comparing the current element (1, 2, 3, and then 4) to the current value of v (which starts off as 1). If v is less than the current element, we replace v with the current element.
So, for example, in the first iteration of the loop, we compare:
if v < element (if 1 < 1)
Which is false, so we move on to the next loop, where we compare:
if 1 < 2
This is True, so we execute the next line which states v = element (or v = 2)
So on the next loop, we are now comparing:
if 2 < 3
Which is True, so v will again be assigned the value of element
At the end of both for loops, we end up returning the largest value of m, in this case: 4
data[0]. You seem to have the right idea in the question, just keep moving to the nextrowinmv(withvstarting out as the first number). Sincevstarts out as1, no other number is smaller.