Python 链表中的索引问题

Python 链表中的索引问题,python,indexing,Python,Indexing,我只是在为部分代码设置索引时遇到了一些问题。关键功能是切换,其他一切正常。SwitchFunctions任务是在给定索引处获取节点,并将其与列表开头的节点进行切换。我非常接近,但我不明白为什么当我为第一个案例运行测试函数testswitch时,它不会输出前两个节点,然后因为列表已经更改,第二个测试案例由于类型错误而无法运行。谢谢你的帮助 """ Creates and returns a linked list containing all of the elements of the Pyth

我只是在为部分代码设置索引时遇到了一些问题。关键功能是切换,其他一切正常。SwitchFunctions任务是在给定索引处获取节点,并将其与列表开头的节点进行切换。我非常接近,但我不明白为什么当我为第一个案例运行测试函数testswitch时,它不会输出前两个节点,然后因为列表已经更改,第二个测试案例由于类型错误而无法运行。谢谢你的帮助

"""
Creates and returns a linked list containing all of the elements
of the Python-style list parameter.
"""

def createList(plist):

    myList = None 
    for index in range(len(plist)-1, -1, -1):
        myList = insertValueHead(myList, plist[index])
    return myList

'''
Creates a string representation of the values in the linked list such as:
5->6->9->14.
'''

def listString(linkedList):


      ptr = linkedList
      str1 = ''
      while ptr != None:
        str1 += str(ptr['data'])
        ptr = ptr['next']
        if ptr != None:
          str1 += "->"
      str1 = str1
      return str1


'''
Inserts a new node containing the value "value" to the head of the list.
LinkedList is the head of the list to be added to
Value is the data to be stored in the node
'''

def insertValueHead(myList, value):

    newnode = {}
    newnode["data"] = value
    #set the next pointer of this new node to the head of the list, linkedList
    #newnode is now the head of the list 
    newnode["next"] = myList
    return newnode

def nthNode(myList, index):

    ptr = myList
    count = 0
    if index < 0:
        return None
    while ptr != None and count < index:
        ptr = ptr['next']
        count += 1
    return ptr

def switch(myList, index):

    newnode = {}
    newnode['data'] = 0
    newnode['next'] = None
    temphead = myList
    head = myList
    head = head['next']
    if index != 0:
        output = myList
    else:
        output = newnode
    prev, ptr = None, myList
    while ptr != None and index >0:
        prev = ptr
        ptr = ptr['next']
        index = index - 1
    if index > 0:
        print ('Index out of range')
    temphead['next'] = ptr['next']
    ptr['next'] = head
    prev['next'] = temphead
    print ptr['data']
    return output



def testSwitch():

    #test code to ensure that switch() is working correctly.
    myList = createList([10, 20, 30, 40, 50, 60])
    print "The initial list", listString(myList)
    myList = switch(myList, 2)
    print "Switching the head and the 30.  Resulting list is ", listString(myList)
    myList = switch(myList, 5)
    print "Switching the head and the 60.  Resuling list is ", listString(myList)
    myList = switch(myList, 29)  #should result in an error   
testSwitch()  
这是回溯

Traceback (most recent call last):
  File "C:/Users/Hassan/Desktop/Python/assignment4.py", line 122, in <module>
    testSwitch()
  File "C:/Users/Hassan/Desktop/Python/assignment4.py", line 119, in testSwitch
    myList = switch(myList, 5)
  File "C:/Users/Hassan/Desktop/Python/assignment4.py", line 105, in switch
    temphead['next'] = ptr['next']
TypeError: 'NoneType' object has no attribute '__getitem__'

你能把所有的追踪都包括进去吗?Nvm我知道了!但是,你知道为什么会发生这种类型的错误吗?因为它也发生在我的固定代码中,我想知道是什么原因导致了它。当一个操作或函数被应用于一个不合适类型的对象时,docs把它放在TypeError上。在这种情况下,之所以会发生这种情况,是因为当您尝试执行temphead['next']=ptr['next']时,ptr很可能为None。当您使用[]符号访问字典的键时,python调用dict对象的\uuuu getitem\uuuu方法。None没有定义_ugetItem。因此出现了错误。我现在使用不同的代码,只是将变量设置为等于ptr['data']中的值,但仍然会出现此错误。我该如何修复它?