You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中Dijkstra最短路径算法报错:生成器不支持元素赋值

Fixing "TypeError: generator does not support item assignment" in Your Node Mapping Code

Hey there! Let's break down this error and fix it for your small/large map node setup.

What's Causing This Error?

This error pops up when you try to assign a value to an index of a generator object—but generators are one-time iterators, not mutable collections like lists. They don't support direct indexing or item assignment operations at all.

Likely Culprits in Your Code

Looking at your code snippet, the issue is almost certainly related to how you're initializing or populating node_array (or node_list). Here are the most common scenarios that trigger this:

  1. Using generator expressions instead of list comprehensions
    If you wrote something like this (using parentheses instead of square brackets):

    # ❌ Wrong: Creates a generator, not a modifiable list
    node_array = (f"node_{i}" for i in range(node_count))
    

    Generators can't be modified via index. Swap the parentheses for square brackets to create a mutable list:

    # ✅ Correct: Creates a list that supports item assignment
    node_array = [f"node_{i}" for i in range(node_count)]
    
  2. Forgetting to convert map() results to lists (Python 3+)
    In Python 3, map() returns a generator-like object instead of a list. If you're using it to process node data:

    # ❌ Wrong: map returns a generator in Python 3
    node_array = map(str, some_node_ids)
    

    Convert it to a list first to enable assignment operations:

    # ✅ Correct: Convert generator to a mutable list
    node_array = list(map(str, some_node_ids))
    

How to Verify the Issue

To confirm if you're dealing with a generator, add a quick debug line right where you initialize the array:

print(type(node_array))  # If this outputs `<class 'generator'>`, that's your problem!

Quick Fix Example for Your Scenario

Since you're handling different node naming rules for small maps (<27 nodes), here's an adjusted snippet that avoids generators entirely:

global array_type
node_list = []
node_array = []

def check_int(x):
    while True:
        try:
            node_count = int(input(x))
            if node_count < 1 or node_count > 100:  # Adjust valid range to fit your needs
                print("Please enter a number between 1 and 100.")
                continue
            return node_count
        except ValueError:
            print("That's not a valid integer. Try again.")

# Example setup for small/large maps
node_count = check_int("Enter number of nodes: ")
if node_count < 27:
    # Use list comprehension for small map naming (A, B, C...)
    node_array = [f"Node_{chr(65 + i)}" for i in range(node_count)]
else:
    # Use list comprehension for large map naming (1, 2, 3...)
    node_array = [f"Node_{i+1}" for i in range(node_count)]

# Now you can safely assign to indexes without errors
node_array[0] = "Start_Node"

This ensures node_array is always a list, which supports item assignment without triggering that generator-related TypeError.

内容的提问来源于stack exchange,提问作者aidan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:02:43