Python LPTHW:这个循环语句在他的函数中做了什么

时间:2015-04-13 21:07:22

标签: python function loops

我正在阅读“以艰难的方式学习Python”这本书。我不明白练习39中这个功能发生了什么。

def new(num_buckets=256): #So the function is made and its named "new" and the argument inside is
    """Initializes a Map with the given number of buckets."""
    aMap = [] #then it creates an empty list and names it aMap 
    for i in range(0, num_buckets): #This loop is created named i and it is ranging from 0-255.
        aMap.append([]) #then it adds a list inside our aMap variable.
    return aMap

我不知道“for i in range(0,num_bucketss)”在这里做了什么。它在做什么是追加命令?

1 个答案:

答案 0 :(得分:0)

它在aMap中添加了与num_buckets指定的空列表一样多的空列表。因此,如果num_buckets = 5aMap将是5个空列表的列表。

至于为什么这样做,我们需要更多的背景。

编辑:

看到context,它正在这样做为hashmap创建一些空桶。您可以在此处详细了解hashmap如何使用存储区: What is meant by number of buckets in the HashMap?