Python无法理解循环中的条件语句

2024-03-29 07:25:43 发布

您现在位置:Python中文网/ 问答频道 /正文

需要python方面的帮助。这段代码来自leetcode.com,这是problem的解决方案之一,无法理解那里的条件语句,正是代码“stack[-1][1]

class Solution(object):

def dailyTemperatures(self, temps):

    if not temps:
        return []

    result = [0] * len(temps)
    stack = []

    for curr_idx, curr_temp in enumerate(temps):

        while stack and curr_temp > stack[-1][1]: # not clear, and I know, it is not a type of access to list element

            last_idx, last_temp = stack.pop()
            result[last_idx] = curr_idx - last_idx

        stack.append((curr_idx, curr_temp))

    return result

Tags: and代码comreturnstacknotresult解决方案