[英]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.