【发布时间】:2015-04-13 21:07:21
【问题描述】:
我正在关注“Learn Python The Hard Way”一书,但我不明白练习 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) 在这里做什么。它在做什么 append 命令?
【问题讨论】:
-
加一行
print(i),想想每次aMap如何变化。另外,想想变量名num_buckets... -
我试过添加打印命令。它给了我这个:“
” -
好吧,那么您肯定做错了某事——您使用了我建议的确切打印语句吗?在学习过程中,能够明确循环是很有帮助的。