首页 > 解决方案 > Python - 如果我增加列表的大小,为什么我的 for 循环的长度函数不会改变

问题描述

range(len(rotting_oranges)):我正在处理 BFS 问题,主要是在我将新职位添加到队列中时,对这行代码感到困惑。
rotting_oranges.append((next_row, next_col)) 我的问题是,列表是通过引用。如果我用新位置添加到 rotting_oranges 中。列表的新大小不会更大吗?我要问的问题是长度函数是如何工作的?在 for 循环中它只被调用一次(即len(rotting_oranges)) 这是我的完整代码。

    def orangesRotting(self, grid: List[List[int]]) -> int:
        #Rotting orange 
        #0 means no orange
        #1 means alive orange
        #2 is rotting orange
        # Every min the orange adj to me are rotting if I am also rotting. 
        #The first step is to find the dead oranges 
        palin = [1,2]

        seen_set = set()
        rotting_oranges = collections.deque()
        #We need to also know when we're done (i.e) could be no more oranges or 
        #No more alive oranges
        alive_oranges = 0
        
        for row in range(len(grid)):
            for col in range(len(grid[0])):
                if grid[row][col] == 2: #This means dying orange grab you 
                    rotting_oranges.append((row, col))
                    seen_set.add((row, col))
                    
                elif grid[row][col] == 1: #Alive orange add to count
                    alive_oranges += 1 
        directions = [(1,0), (-1,0), (0, 1), (0, -1)]
        
        minutes_passed = 0 
        #Now emulate the dying of oranges every minute 
        #There are two cases either there are still rotting oranges left in our (queue)
        #Or that the alive oranges are still left 
        while rotting_oranges and alive_oranges > 0:
            minutes_passed += 1 
            #This "time emulates the leveling of available" oranges to you at this time 
            #Can also do minutes = len(rotting_oranges) 
            #As one you've popped it the len(rotting_oranges doesn't change until we exit the loop )
            for time in range(len(rotting_oranges)):
                #This means that if the next_row, next_col exist an orange (1)
                #Then we can infect you since row, col are infected and you're just moving within the 
                #4 cardinal directions 
                row, col = rotting_oranges.popleft()
                
                for row_offset, col_offset in directions: 
                    next_row, next_col = row + row_offset, col + col_offset
                    #boundary check
                    if next_row < 0 or next_row >= len(grid) or \
                    next_col < 0 or next_col >= len(grid[0]):
                        continue 
                        
                    #if empty cell can't make a rotting orange so ignore
                    if grid[next_row][next_col] == 0: 
                        continue 
                        
                    #If I've seen this rotten orange before or visited this position before ignore 
                    if (next_row,next_col) in seen_set:
                        continue 
                    #Else you're an alive orange 
                    if grid[next_row][next_col] == 1: #I'd make it rotting then
                        grid[next_row][next_col] = 2 
                        alive_oranges -= 1
                        rotting_oranges.append((next_row, next_col))
                        seen_set.add((next_row,next_col))
                        
        return minutes_passed if alive_oranges == 0 else -1

标签: python

解决方案


列表大小确实发生了变化,但正如您在评论中已经怀疑的那样,该len()方法只被调用一次,因此for 循环在被调用时被分配了该方法的返回值。这就是为什么改变列表/队列的大小根本不会影响 for 循环的原因。


推荐阅读