简体   繁体   English

Python - 如果我增加列表的大小,为什么我的 for 循环的长度 function 不会改变

[英]Python - Why doesn't the length function change for my for loop if I'm increasing the size of a list

I was working on a BFS problem and confused about this line of code for time in range(len(rotting_oranges)): mainly when I added the new positions onto my queue我正在处理一个 BFS 问题,并且对range(len(rotting_oranges)):主要是当我将新位置添加到队列中时
rotting_oranges.append((next_row, next_col)) My question is, a list is by reference. rotting_oranges.append((next_row, next_col))我的问题是,一个列表是通过引用。 If I added to the rotting_oranges with new positions.如果我用新位置添加到 rotting_oranges 中。 Wouldn't the new size of the list be bigger?列表的新大小不会更大吗? The question I'm asking is how does the length function called work?我要问的问题是长度 function 是如何工作的? In the for loop is it just called once (ie the len(rotting_oranges) ) Here is my full code.在 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

The list size does indeed change, but as you have already suspected in your comment, the len() method gets only called once and thus the for-loop gets assigned the return value of the method when it got called.列表大小确实发生了变化,但正如您在评论中已经怀疑的那样, len()方法只被调用一次,因此for 循环在被调用时被分配了该方法的返回值。 That's the reason, why changing the size of the list/queue, does not influence the for-loop at all.这就是为什么改变列表/队列的大小根本不会影响 for 循环的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我在 function 调用中时,为什么我的全局变量的值没有改变? - Why doesn't the value of my global variables change when I'm inside a function call? 为什么即使我在循环中更改长度,Python 的“for”也会迭代列表的长度? - Why does Python's "for" iterate the length of the list even if I change the length in the loop? 为什么我的列表没有变化? - Why doesn't my list change? Python为什么如果更改循环内的值,循环行为不会改变 - Python why loop behaviour doesn't change if I change the value inside loop 为什么我的 for 循环实际上没有循环? 我没有休息 function,并希望它循环但它没有 - Why doesn't my for loop actually loop? I do not have a break function, and expected it to loop but it does not Python - 为什么这个列表没有改变 - Python - Why this list doesn't change 为什么这个参数列表在 Python 中没有变化? - Why this parameter list doesn't change in Python? Python 2.7.10 Pygame 1.9.1。 我正在尝试通过增加或减少5来更改列表的值 - Python 2.7.10 Pygame 1.9.1. I'm trying to change the values of a list by 5 either increasing or decreasing 为什么我的apply函数不返回字符串的长度? - Why doesn't my apply function return the length of the string? 我很困惑为什么这个递归循环不会停止 - I'm confused why this recursion loop doesn't stop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM