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

时间:2018-09-06 17:08:28

标签: python loops conditional-statements statements

需要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

1 个答案:

答案 0 :(得分:0)

它将返回栈的最后一个索引中的第二个元素

例如,如果堆栈是类似字符串的列表

stack = ['abc','def','ghi']

比堆栈[-1] [1]返回

stack[-1] <-- 'ghi'
stack[-1][1] <-- 'h'