Python: "'Nonetype' is not iterable" 没有任何迭代

Python: "'Nonetype' is not iteratable" without any iteration

我正在编写一个程序,通过线性探测形成不同大小的散列 table。在我用于线性探测的 ADT 中,我有一个函数 def insert ( ),它是从我的主脚本中通过以下函数调用的:

def insertHashTable(  loadNum, hashTable  ):
    i = 0
    while i < ((loadNum*size)-1):
        hashTable.insert(data[i],data[i])
        i = i + 1
    return hashTable 

错误本身来自从 insertHashTable( ) 调用的函数

def insert( self, key, value ):
    ( found, slot ) = self._findSlot( key ) #ERROR HERE
    if not found :
        self._table[slot] = _MapEntry( key, value ) #custom datatype
        self._count += 1
    return not found

我在这段代码的第二行收到非类型错误。最后,_findSlot( ) 如下:

def _findSlot( self, key ):        
    startLoc = self._hash1( key )

    self.slotsAccessed += 1
    if self._table.__getitem__( startLoc ) == None:
        return (False, startLoc)

    else:
        c = 0
        while (c+startLoc) < (self._size -1):
            if self._table[startLoc+c] == None:
                return (False, startLoc+c) 
            elif self._table.__getitem__( startLoc ).key == key:
                return (True, startLoc+c) 
            c = c + 1
            self.slotsAccessed += c

我不确定为什么 insertHashTable( ) 函数中会出现这样的错误,因为没有对密钥进行迭代。

不过我知道我的哈希 table 在 table 初始化时的每个槽中都有 'None',也许这有什么问题?

您的 _findSlot(self, key) 函数可以在没有 return 语句的情况下完成。 (具体来说,如果您的 while 条件变为假并且循环结束,就会发生这种情况。)在这种情况下,该函数将 return None。如果您尝试将 (found, slot) 的值 None 赋值,则会收到错误 Nonetype is not iterable.

您可能需要弄清楚您的函数在当前 returning None.

的情况下应该 return