弄清楚为什么代码块可以提供一定的输出

考虑以下功能:


def fun(lst):

    for item in lst:

        cmp = 0

        for other in lst:

            if item < other: 

                cmp -= 1

            elif item > other:

                cmp += 1

         if not cmp:

            return item




nums = [1,3,2,2]

print("fun({0}) = {1}".format(nums,fun(nums)))

我知道这段代码的输出是:


fun([1,3,2,2])= 2


但是我不知道为什么。有人可以解释为什么这是输出吗?


是否有人对如何简化代码块的解释有任何提示?


由于我显然在考试中无法访问python,因此我正在努力找出某些代码块的实际作用。


郎朗坤
浏览 168回答 3
3回答

慕村9548890

首先,您的代码有错误的对齐方式(“ if not cmp:”应保留一个位置,最后两行应与第一行位于同一列),如下所示:def fun(lst):&nbsp; &nbsp; for item in lst:&nbsp; &nbsp; &nbsp; &nbsp; cmp = 0&nbsp; &nbsp; &nbsp; &nbsp; for other in lst:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if item < other:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmp -= 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif item > other:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmp += 1&nbsp; &nbsp; &nbsp; &nbsp; if not cmp:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return itemnums = [1,3,2,2]print("fun({0}) = {1}".format(nums,fun(nums)))fun函数返回第一个数组的项,以使“ not cmp”,即cmp!=0。cmp是小于给定项的数组元素数减去大于项的数组元素数&nbsp; if item < other:&nbsp; &nbsp; cmp -= 1&nbsp; elif item > other:&nbsp; &nbsp; cmp += 1现在,让我们看一下数组项[1、3、2、2]1:比self(3,2,2)多3个项目,且不小于self,因此cmp = 0-3 = -3,不返回3:有3个项目,没有比自己少的项目,有3个(3、2、2)多于自己,因此cmp = 3-0 = 3,不返回2:比self(3)多一件商品,而比(1)小一件商品,cmp = 0,函数将其返回(2)

动漫人物

def fun(lst):&nbsp; &nbsp; for item in lst:&nbsp; &nbsp; &nbsp; &nbsp; cmp = 0&nbsp; &nbsp; &nbsp; &nbsp; for other in lst:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if item < other:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmp -= 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif item > other:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmp += 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if not cmp:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return itemnums = [1, 3, 2, 2]您的代码有一个嵌套循环,一个for循环中的一个for循环。外部for循环是for item in lst,内部for循环是:for other in lst:循环看起来像这样:1(outer) - > 1, 3, 2, 2&nbsp; &nbsp;# 1,3,2,2 are assigned one by one to `other`3(outer) - > 1, 3, 2, 22(outer) - > 1, 3, 2, 22(outer) - > 1, 3, 2, 2它首先从外循环开始,分配给item的值是1,并cmp设置为0。现在,在内部循环中遍历整个列表。分配给other的第一个值是1。现在,它检查它是否大于或小于item(在这种情况下为1),并cmp基于此值增加或减少。在下一次迭代中,现在分配了other 3,再次将其与item(1)比较,并cmp根据该值更改了值。同样,它移至下两个项目2,2。现在出现这种情况:if not cmp: return item它检查的值是否cmp为falsey,如果cmp为0(0为falsey值),则返回该项目,函数终止。(not 0是True在python)如果条件为假,则返回到外部循环,并为该时间item分配值3,然后内部循环按照上面已经描述的那样继续,但事实item是now 3。内部循环实际上所做的是,它实际上比较有多少个项目大于或小于当前项目。1(outer) - > 1, 3, 2, 2&nbsp; &nbsp;# cmp is -3, because 3,2,2 are bigger than 13(outer) - > 1, 3, 2, 2&nbsp; &nbsp;# cmp is 3, because 1,2,2 are smaller than 32(outer) - > 1, 3, 2, 2&nbsp; &nbsp;# cmp is 0, because 3 is greater than 2 and 1 is smaller&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # than 2, so the condition `if cmp` is True for this case&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # and the function&nbsp; return 2 (i,e. item)2(outer) - > 1, 3, 2, 2&nbsp; &nbsp;# this is never reached as function already returned如果所有循环都结束并且cmp永远都不会变为0,则您的函数将返回None(函数的默认返回值)。

收到一只叮咚

是否有人对如何简化代码块的解释有任何提示?把它写出来。在页面中为每个名称分配自己的空间,并在运行代码时跟踪对值的更改。经过一些练习,您将能够轻松跟踪简单值,只需要写出非标量值即可。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python