Python僵尸集群统计代码报NameError: name 'zombie_array'未定义
NameError: name 'zombie_array' is not defined in Your Zombie Cluster Code Hey there, let's break down and fix this issue step by step!
What's Causing the Error?
The NameError means you're trying to use a variable named zombie_array that hasn't been defined anywhere in your code. This almost always happens when you call the zombieCluster function with zombie_array as an argument, but you never created or assigned a value to that variable first.
On top of that, your zombieCluster function code is incomplete (the final line cuts off at for j in range(row)...), which would cause additional syntax or logic errors once you fix the variable issue.
Step-by-Step Fixes
Let's address both problems with a complete, working version of your code:
Finish the
zombieClusterfunction logic
The goal of this function is to count connected zombie clusters using DFS. We need to check each zombie, and if they haven't been part of a scanned cluster yet, start a DFS and increment our count.Define a valid input variable
Before callingzombieCluster, make sure you've created a properly formatted zombie array (a list of strings where each string represents connections between zombies).
Complete Working Code
#!/usr/bin/env python def DFS(j, visited, zombies, row): for k in range(row): # Check if zombie j and k are connected, and we haven't marked this connection yet if zombies[j][k] == '1' and not visited[j][k]: # Mark both directions of the connection as visited visited[j][k] = True visited[k][j] = True DFS(k, visited, zombies, row) def zombieCluster(zombies): row = len(zombies) # Edge case: empty input if row == 0: return 0 count = 0 # Initialize a 2D visited array to track checked connections visited = [[False for _ in range(row)] for _ in range(row)] for i in range(row): # Use the diagonal element to check if this zombie is part of an unscanned cluster if not visited[i][i]: count += 1 DFS(i, visited, zombies, row) return count # Example usage: Define your zombie array first, then call the function if __name__ == "__main__": # Sample input: 4 zombies, 2 clusters (first 3 are connected, last is alone) my_zombies = [ "1100", "1110", "0110", "0001" ] # Call the function with your defined variable (not the undefined zombie_array) cluster_count = zombieCluster(my_zombies) print(f"Total zombie clusters: {cluster_count}")
Key Improvements & Notes
- Fixed DFS condition: Removed redundant checks for
visited[k][j]since we mark both directions when we first detect a connection. - Completed cluster counting logic: We use the diagonal entry
visited[i][i]to check if a zombie hasn't been part of any scanned cluster yet—this avoids counting the same cluster multiple times. - Valid input example: The
my_zombiesvariable is properly defined before calling the function, so no moreNameError.
If you were previously calling zombieCluster(zombie_array) elsewhere (like in a script or interactive shell), make sure you first define zombie_array with a valid value, e.g.:
zombie_array = ["101", "010", "101"] print(zombieCluster(zombie_array))
内容的提问来源于stack exchange,提问作者Rowle

