繁体   English   中英

PYthon-在哈希图中构建列表的第3层

[英]PYthon - Building 3rd layer of list within a hashmap

我目前在“学习Python困难方式” 课程的前39岁: http : //learnpythonthehardway.org/book/ex39.html

目前,我确实对这段代码感到困惑,我试图在代码中添加“存储桶”的“第三层”,以便每个键都有自己的字典,以便键可以容纳多个值。

代码太多,因此无法将其全部粘贴到此处,但是如果您单击链接并向下滚动,则会看到一个名为“三级列表”的子标题,该子标题下的最后一句话/第三段为:

“如果要进一步使用此代码,请对其进行更改以支持每个键的多个值。”

我一直在尝试这样做大约两天,现在它已经开始流行起来。 我只是看不到一种方法。

如果有人可以给我一些指导,我将不胜感激。

实施代码后,出现此错误:

File "ex39_test.py", line 34, in <module>
print "Michigan has: %s" % hashmap2.get(cities, hashmap2.get(states,     'Michigan'))
File "C:\python27\hashmap2.py", line 27, in get
i, k, vlist = get_slot(aMap, key, default=default)
File "C:\python27\hashmap2.py", line 18, in get_slot
bucket = get_bucket(aMap, key)
File "C:\python27\hashmap2.py", line 13, in get_bucket
bucket_id = hash_key(aMap, key)
File "C:\python27\hashmap2.py", line 10, in hash_key

在实施本节代码建议之后:

def set(aMap, key, value):
bucket = get_bucket(aMap, key)
i, k, vlist = get_slot(aMap, key)

if i >= 0:
    # if value is already in the key-list
    if value in vlist:
        return
    vlist.append(value)
else:
    bucket.append((key, list(value)))

我把'key'变量变成了一个可变项-它现在作为值以及它自己的列表的键在列表中。

所以现在我无法在以前的哈希函数中对其进行哈希处理

我相信,通过添加第3级,他们说您应该有一个列表,而不是每个键都有一个值。

因此,我们更改集:

def set(aMap, key, value):
"""Sets the key to the value, only adds new values"""
  bucket = get_bucket(aMap, key)
  i, k, vlist = get_slot(aMap, key)
  if i >= 0:
      # check if value is already in list, if so, do nothing
      if value in vlist:
            return
      # the key exists, so just add they value to it
      vlist.append(value)
  else:
      # the key is not in table, add a list for the key
      # allows multiple values for each key
      bucket.append((key, list(value)))

我相信其他功能仍保持不变。 请注意,get()将返回一个列表,而不是单个值。

我还尝试在哈希图中构建第3层,并且做到了,这就是我的代码所实现的。

由于类型'list'不可散列,因此让'key'类型仍为'string',并使'value'类型列表成为可能。 附言:我是中国学生,请原谅我奇怪的英语表达。只想对您有所帮助!

这是我的代码的关键部分:

def set(aMap, key, value):
'''Sets the key to the value, replacing an existing value.'''
    bucket = get_bucket(aMap, key)
    i, k, vlist = get_slot(aMap, key)

    if i >= 0:
    # the key exists, append it
        vlist.append(value)
    else:
    # the key does not, append to creat it
        bucket.append((key,[value])) 
    # attention! the key is string and value becomes list,which is the 3rd layer of bucket.

其他功能也需要一些相应的更改。

并且,请注意ex39_test.py中的此语句:

    print "Michigan has: %s" % hashmap.get(cities, hashmap.get(states, 'Michigan'))

在上面的语句中,当我们第一次调用hashmap.get函数时,它将返回一个值,但是该值的类型希望是列表,当再次调用它时,它将成为hashmap.get函数中的第二个参数! 而且,如果您没有注意到它,计算机将散列该密钥(类型为列表),这将导致无法散列的错误。

这是我在hashmap.py中修复它的方法:

def get(aMap, key , default = None):
'''Gets the value in a bucket for the given key, or the default.'''
    if type(key) == type (' ') :
    # if key's type is string,then nothing changes.
        i, k, vlist = get_slot(aMap, key, default)
    else: 
        i, k, vlist = get_slot(aMap, key[0], default) 
# in this way the second parameter is string again and it won't cause unhashable mistake.
    return vlist 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM