简体   繁体   English

我的 python 代码的 output 背后的逻辑是什么?

[英]What is the logic behind the output of my python code?

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:我从网站上获得了以下程序片段的参考,但通过它我无法理解 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, eg我可以通过打印语句计算出一对 output,例如

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.但是第一个 for 循环以及 function 之外的打印语句也让我感到困惑。

Sorry for dumb question, but I need to clear my logic.对不起,愚蠢的问题,但我需要清除我的逻辑。

Thank you in advance先感谢您

In your code在您的代码中

data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]

subsequently data[0] , that is passed to the function as m , is随后data[0] ,即作为m传递给 function ,是

[[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在这里,您将遍历[[1, 2], [3, 4]] ,然后遍历嵌套列表[1, 2][3, 4]中的每个元素,并尝试比较列表中的每个数字到 1,所以 v 变成 2,然后是 3,然后是 4,所以在代码上打印

4

Just running the code print只需运行代码打印

4 4

Line by Line:逐行:

data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]

We have a list of list of list (depth 3)我们有一个列表列表(深度 3)

so: fum(data[0]) = fum([[1, 2], [3, 4]])所以: 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 row in data[0]意味着我们迭代[[1, 2], [3, 4]]的元素,所以第一个元素是[1,2]第二个元素是[3,4]

for element in row means we will iterate in the elements of row so: element = 1, then 2, 3, 4 for element in row意味着我们将迭代行中的元素,所以: element = 1, then 2, 3, 4

if v < element: v = element

Just means v takes the value of the max value of (v, element)只是意味着 v 取 (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.因此,此函数获取列表数据的第一个元素并返回数据的第一个元素包含的 m 个元素的 n 个列表的最大值。

First, let's go over what's being passed to fun() :首先,让我们对传递给fun()的内容进行 go :

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.现在让我们分解 function 的第一行。 We have v=m[0][0] .我们有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循环:

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.这只是循环遍历列表中传递给 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语句将当前元素(1、2、3,然后是 4)与v的当前值(从 1 开始)进行比较。 If v is less than the current element, we replace v with the current element.如果v小于当前元素,我们将v替换为当前元素。

So, for example, in the first iteration of the loop, we compare:因此,例如,在循环的第一次迭代中,我们比较:

if v < element ( if 1 < 1 ) if v < elementif 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 )这是真的,所以我们执行下一行说明v = element (或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这是真的,所以v将再次被分配元素的值

At the end of both for loops, we end up returning the largest value of m , in this case: 4在两个for循环结束时,我们最终返回m的最大值,在这种情况下:4

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.鉴于您的输入,function 将返回列表列表中的最大值,但它会对您的输入格式做出一些假设。

This is that your m is a list of lists with the internal lists having numerical values in them.这是您的m是一个list lists ,其中内部列表中包含数值。 As the other answers say, the output for your case is 4 and the value of v will increase by one on each loop.正如其他答案所说,您的情况的 output 是 4 并且 v 的值将在每个循环中增加一。

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):但是,举一个例子,如果您的输入是[[1,4],[2,1]]v < element每次都不会给出Truev的值将如下所示(注释将按顺序给出值循环):

# 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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM