Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
68 views
in Technique[技术] by (71.8m points)

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
rotting_oranges.append((next_row, next_col)) My question is, a list is by reference. If I added to the rotting_oranges with new positions. Wouldn't the new size of the list be bigger? The question I'm asking is how does the length function called work? In the for loop is it just called once (i.e the len(rotting_oranges) ) Here is my full code.

    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
question from:https://stackoverflow.com/questions/65908882/python-why-doesnt-the-length-function-change-for-my-for-loop-if-im-increasin

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. That's the reason, why changing the size of the list/queue, does not influence the for-loop at all.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...