返回列表中的最低点,由多个列表组成

def is_sink(m: List[List[int]], c: List[int]) -> bool:

    lowest_point = m[0][0]

    for i in range(len(m)):

        for j in range(len(m)):

            if m[i][j] < lowest_point:

                lowest_point = m[i][j]

    if c == lowest_point:

        return True

    else:

        return False

    """

    m = [[1,2,3],

         [2,3,3],

         [5,4,3]]

   >>> is_sink(m,[0,0])

        True

   >>> is_sink(m,[2,2])

        True

   >>> is_sink(m,[3,0])

        False

如果 c 是 m 中最低的,我试图返回 True。代码不会打印 true 或 false


凤凰求蛊
浏览 158回答 2
2回答

翻阅古今

我理解这个问题的方式:(a)c列表有 2 个元素,并被解释为矩阵的索引m;(b) 如果这些索引指定的值是其子列表中的最小值,则返回 true。def is_sink(m, c):&nbsp; &nbsp; ci, cj = c&nbsp; &nbsp; for i, xs in enumerate(m):&nbsp; &nbsp; &nbsp; &nbsp; mn = min(xs)&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i == ci and mn == xs[cj]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return True&nbsp; &nbsp; &nbsp; &nbsp; except IndexError:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pass&nbsp; &nbsp; return FalseMATRIX = [&nbsp; &nbsp; [1, 2, 3],&nbsp; &nbsp; [2, 3, 3],&nbsp; &nbsp; [5, 4, 3],]print(is_sink(MATRIX, [0, 0]))&nbsp; # Trueprint(is_sink(MATRIX, [2, 2]))&nbsp; # Trueprint(is_sink(MATRIX, [3, 0]))&nbsp; # False, because out of boundsprint(is_sink(MATRIX, [1, 0]))&nbsp; # True

白猪掌柜的

您的代码检查最低点,而不是该点是否为汇。另外,我相信这是您的功课,您不应该在网上获得帮助。来我的办公时间。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python