Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中对空列表使用索引_Python_List_Indexing_Hash - Fatal编程技术网

在python中对空列表使用索引

在python中对空列表使用索引,python,list,indexing,hash,Python,List,Indexing,Hash,对于以下程序,在addEntryself、dictKey、dictVal函数中,我不理解为什么以下代码行不生成索引错误: if hashBucket[i][0] == dictKey self.bucket最初是一个空列表: self.bucket=[],[]…[] 当第一次执行addEntry时,hashBucket只是一个空列表,所以我希望hashBucket[I][0]会生成一个索引错误,但程序实际上可以运行,为什么?非常感谢你的帮助 这是节目单 class intDict(object

对于以下程序,在addEntryself、dictKey、dictVal函数中,我不理解为什么以下代码行不生成索引错误:

if hashBucket[i][0] == dictKey
self.bucket最初是一个空列表: self.bucket=[],[]…[] 当第一次执行addEntry时,hashBucket只是一个空列表,所以我希望hashBucket[I][0]会生成一个索引错误,但程序实际上可以运行,为什么?非常感谢你的帮助

这是节目单

class intDict(object):
    """A dictionary with integer keys"""

    def __init__(self, numBuckets):
        """Create an empty dictionary"""
        self.buckets = []
        self.numBuckets = numBuckets
        for i in range(numBuckets): 
            self.buckets.append([])

    def addEntry(self, dictKey, dictVal):
        """Assumes dictKey an int. Adds an entry"""
        hashBucket = self.buckets[dictKey%self.numBuckets]
        for i in range(len(hashBucket)):
            if hashBucket[i][0] == dictKey:
                hashBucket[i] = (dictKey, dictVal)
                return
        hashBucket.append((dictKey, dictVal))

    def getValue(self, dictKey):
        """Assumes dictKey an int. Returns entry associated with the key dictKey"""
        hashBucket = self.buckets[dictKey%self.numBumBuckets]
        for e in hashBucket:
            if e[0] == dictKay:
                return e[1]
        return None

    def __str__(self):
        result = '{'
        for b in self.buckets:
            for e in b:
                result = result + str(e[0]) + ":" + str(e[1]) + ','
        return result[:-1] + '}' # result[:-1] omits the last coma

由于hashBucket一开始是一个空列表,因此rangelenhashBucket:中的i基本上是range0:中的i,这意味着在第一次调用addEntry时,如果hashBucket[i][0]==dictKey,它永远不会到达条件值。

由于hashBucket一开始是一个空列表,rangelenhashBucket:中的i基本上是range0:中的i,这意味着在第一次调用addEntry时,它永远不会到达条件if hashBucket[i][0]==dictKey。

当您尝试循环一个空列表时,不会发生任何事情,请启动python interpeter并尝试此操作

>>> for i in range(len([])):
...     print(i)
... 
>>>
不会打印任何内容,因此同样,如果hashBucket为空,则for循环中的所有内容将永远不会执行

def addEntry(self, dictKey, dictVal):
    """Assumes dictKey an int. Adds an entry"""
    hashBucket = self.buckets[dictKey%self.numBuckets]
    for i in range(len(hashBucket)):
        # This is never executed if hashBucket is empty
        if hashBucket[i][0] == dictKey:
            hashBucket[i] = (dictKey, dictVal)
            return
    hashBucket.append((dictKey, dictVal))

当您尝试循环一个空列表时,不会发生任何事情,启动python interpeter并尝试以下操作

>>> for i in range(len([])):
...     print(i)
... 
>>>
不会打印任何内容,因此同样,如果hashBucket为空,则for循环中的所有内容将永远不会执行

def addEntry(self, dictKey, dictVal):
    """Assumes dictKey an int. Adds an entry"""
    hashBucket = self.buckets[dictKey%self.numBuckets]
    for i in range(len(hashBucket)):
        # This is never executed if hashBucket is empty
        if hashBucket[i][0] == dictKey:
            hashBucket[i] = (dictKey, dictVal)
            return
    hashBucket.append((dictKey, dictVal))

第一次执行时,hashBucket为空。所以范围是空的。所以这个for循环没有任何作用。”对于rangelenhashBucket中的我:' 我想。
是这样吗?

当第一次执行hashBucket时是空的。所以范围是空的。所以这个for循环没有任何作用。”对于rangelenhashBucket中的我:' 我想。 是这样吗?

对于rangelenhashBucket中的i,它将变成range0中的i,这意味着它永远不会进入循环,因此只有hashBucket。附加。。。因为rangelenhashBucket中的i变为range0中的i,这意味着它永远不会进入循环,所以只有hashBucket.append。。。被执行。