Python中的哈希函数生成错误

时间:2014-10-26 19:06:51

标签: python hash

所以我试图掌握Hash函数及其工作原理。我有以下代码,但是当我尝试运行代码时,我一直收到错误。

import sys 

def part_one():   

        foo = open('input_table.txt')
        for line in foo:
            id, make, model, year = line.split(",")
            print(make, model) 
            tuple_list = (make+model,)
        return tuple_list



def hash_one(num_buffers, tuple_list):
        #part_one()
    # A being the first constant prime number to multiply by
    # B being the prime number that we add to A*sum_of_chars
        tuple_list = part_one()
        A = 3
        B = 5
        count = 0 

        for item in tuple_list:
            for char in item:
        # sum_of_chars is the total of each letter in the word
                count = ord(char)
                count = count + tuple_list
        index = ((A * sum_of_chars + B)) % num_buffers
        return index    



if __name__ == '__main__':

    input_table = sys.argv[1] 
    num_buffers = int(sys.argv[2])
    chars_per_buffer = int(sys.argv[3])
    sys.argv[4] = 'make'
    sys.argv[5] = 'model'
    lst = []
    for item in range(4, len(sys.argv)):
        lst.append(sys.argv[item])
    print(lst)
    hash_one(lst)

我的代码导致错误有什么问题?任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

1

你没有参数调用hash(),你必须哈希。

数字的哈希值只会返回相同的数字,所以它不是很有趣。它是用于散列像字符串这样的东西。

2

part_one不返回任何内容,因此当您致电tuple_list = part_one()时,其值已设置为None,您无法通过它进行迭代。

3

通过参数传递列表然后覆盖它无论如何都没有任何意义。如果要返回列表,请使用return语句。

4

在代码中设置参数变量很奇怪,他们可以从命令行中读取参数。

5

(不是错误,但......) 您可以使用切片(lst = sys.argv[4:])作为获取列表子部分的更简单方法。