我正在学习“艰苦地学习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”(0,num\u buckets)在这里做什么。它在执行append命令吗?你知道吗
它在
aMap
中添加了num_buckets
指定数量的空列表。因此,如果num_buckets = 5
,aMap
将是一个包含5个空列表的列表。你知道吗至于它为什么这么做,我们需要更多的背景。你知道吗
编辑:
看到context,它这样做是为了为hashmap创建一些空的bucket。您可以在此处阅读有关hashmap如何使用bucket的更多信息: What is meant by number of buckets in the HashMap?
相关问题 更多 >
编程相关推荐