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

2024-04-24 08:49:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习“艰苦地学习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命令吗?你知道吗


Tags: andthe函数newforisitnum
1条回答
网友
1楼 · 发布于 2024-04-24 08:49:43

它在aMap中添加了num_buckets指定数量的空列表。因此,如果num_buckets = 5aMap将是一个包含5个空列表的列表。你知道吗

至于它为什么这么做,我们需要更多的背景。你知道吗

编辑:

看到context,它这样做是为了为hashmap创建一些空的bucket。您可以在此处阅读有关hashmap如何使用bucket的更多信息: What is meant by number of buckets in the HashMap?

相关问题 更多 >