Python3报错:'NoneType'对象不支持元素赋值,链表添加bottom失败求助
Hey there! Let's figure out why you're hitting that frustrating 'NoneType' object does not support item assignment error in your Eight() function when trying to add elements from a Python list to a linked list's bottom attribute. This is a super common pitfall with linked lists, so let's break it down clearly.
Common Causes & Fixes
1. You're trying to assign to None instead of a valid node
The error pops up when you attempt to set the bottom property (or key, if you're using dictionaries for nodes) on a None value. Usually this happens in two scenarios:
Scenario A: Uninitialized head node
If your linked list starts with head = None and you immediately try to set head.bottom (or head['bottom'] for dictionaries) without first creating a node, Python throws the error because None has no attributes or keys.
Bad Example (Class-based Nodes):
class ListNode: def __init__(self, val=0, bottom=None): self.val = val self.bottom = None def Eight(): input_list = [1, 2, 3] head = None for val in input_list: # Error: head is None, can't access head.bottom head.bottom = ListNode(val) head = head.bottom
Fix: Initialize the head node when adding the first element, then track the current node as you iterate:
class ListNode: def __init__(self, val=0, bottom=None): self.val = val self.bottom = bottom def Eight(): input_list = [1, 2, 3, 4] head = None current_node = None for val in input_list: new_node = ListNode(val) if not head: # First element: set head and current to the new node head = new_node current_node = head else: # Assign to current node's bottom, then move current forward current_node.bottom = new_node current_node = current_node.bottom # Test the linked list temp = head while temp: print(temp.val, end=" -> ") temp = temp.bottom print("None")
Scenario B: Lost reference to a valid node during traversal
If your code incorrectly sets current_node to None mid-traversal, trying to assign to current_node.bottom will trigger the error. This often happens if you loop until current_node becomes None instead of stopping at the last valid node.
Bad Example (Dictionary-based Nodes):
def Eight(): input_list = [1, 2, 3] head = None current = None for val in input_list: new_node = {"val": val, "bottom": None} if not head: head = new_node current = head else: # If current was accidentally set to None, this fails current["bottom"] = new_node current = current["bottom"]
Fix: Always ensure current points to a valid node before modifying its bottom property. The code above works if you avoid setting current to None—just track the last valid node as you go.
Key Takeaway
The root issue is that at some point in your Eight() function, you're trying to modify a None object instead of a valid linked list node. Double-check:
- That you initialize the head node properly when adding the first element.
- That your
currentpointer always references a valid node (notNone) when you assign to itsbottomattribute/key.
内容的提问来源于stack exchange,提问作者Jax Kevin

