如何获得该代码,请参见下文,在IDLE中运行?我现在卡在我的课程中,我无法找到解释。 我知道如何在IDLE中运行一个" def"或代码,按F5,例如, shell中的hash_string(' udacity',3),"输入",结果。当然,在该代码中,使用多个代码时它将无法工作。为了更深入地理解我想在Python Online Tutor中运行它,条件是它将在IDLE中工作,反之亦然。 进一步我想知道,为什么输入#print hashtable_get_bucket(table," Zoe") 结果为:#>>> [[' Bill',17],[' Zoe',14]]为什么' Bill',17出现在列表中?
# Define a procedure, hashtable_get_bucket,
# that takes two inputs - a hashtable, and
# a keyword, and returns the bucket where the
# keyword could occur.
def hashtable_get_bucket(htable,keyword):
return htable[hash_string(keyword,len(htable))]
def hash_string(keyword,buckets):
out = 0
for s in keyword:
out = (out + ord(s)) % buckets
return out
def make_hashtable(nbuckets):
table = []
for unused in range(0,nbuckets):
table.append([])
return table
#table = [[['Francis', 13], ['Ellis', 11]], [], [['Bill', 17],
#['Zoe', 14]], [['Coach', 4]], [['Louis', 29], ['Rochelle', 4], ['Nick', 2]]]
#print hashtable_get_bucket(table, "Zoe")
#>>> [['Bill', 17], ['Zoe', 14]]
#print hashtable_get_bucket(table, "Brick")
#>>> []
#print hashtable_get_bucket(table, "Lilith")
#>>> [['Louis', 29], ['Rochelle', 4], ['Nick', 2]]
感谢您花时间阅读该帖子以及您的建议!
答案 0 :(得分:0)
首先回答您的上一个问题:table
是一个三重列表,因此包含列表的列表列表。其中一个列表是[['Bill', 17], ['Zoe', 14]]
。由于hash_string
返回一个索引,它会从table
中检索一个列表,这个列表恰好是Bill和Zoe的列表。
此外,要在IDLE中运行多个函数,您必须创建一个调用其他函数的新函数(新的def my_assignment
或其他东西)。
希望这有帮助。