What does “Bucket” mean in python?

ⅰ亾dé卋堺 提交于 2019-12-13 02:20:05

问题


I'm reading the book "Learn Python the Hard way" by Zed Shaw, and currently I'm learning about dictionaries.

In one of the exercises he creates a hashmap.py module.

def new(num_buckets=256):
    """Intializes a Map with a given number of buckets"""
    aMap = []
    for i in range(0, num_buckets):
        aMap.append([])
    return aMap

This is the first function he creates. I still have no idea what he is doing i lost track a while ago, (He claims if you just keep copying and try to understand the code it will eventually start to make sense) so now i am trying to dissect the code by googling and looking at forums. Anyways what the heck is a bucket? Is it some kind of programmer gibberish or does it have a meaning? The name appears in almost every function that he creates.

Here is another function he creates with bucket in it.

def get_bucket(aMap,key):
    bucket_id = hash_key(aMap,key)
    return aMap[bucket_id]

I searched stack-overflow and the web and couldn't fine a definite answer on what a bucket is... I'm still a beginner almost intermediate programmer so please take note of that.

Thanks in advance. - Allen


回答1:


With a hash, you store your data in key-value pairs.

Each key can be added dynamically and must be stored internally in some kind of table. But since the number of such entries is limited, multiple keys can map to a single entry.

So, you will have to store more than one value for a single table entry in a 'bucket' which could be an array, linked list, etc. and so can hold multiple key-value pairs for a single hash table entry.



来源:https://stackoverflow.com/questions/36576318/what-does-bucket-mean-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!