我如何散列十万条记录作为CSV文件的输入?

问题描述 投票:0回答:1

通过使用此代码,我只能对1条记录进行哈希处理,而不会出现任何错误或警告。如何散列从CSV文件输入的十万条记录?

import pandas as pd
proper = []
with open("C:\\Users\\krupa\\Downloads\\proper.csv","r") as f:
    for line in f:
        tokens = line.split(',')
        order_id =tokens[0]
        country = tokens[1]

        proper.append([order_id,country])

        #print(proper)
proper = {}
with open("C:\\Users\\krupa\\Downloads\\proper.csv","r") as f:
    for line in f:
        tokens = line.split(',')
        order_id =tokens[0]
        country = tokens[1]
        proper[order_id] = country
#print(proper)
def get_hash(key):
    key = int(key, base=10)
    hash_key = 0
    for i in range(key):
        hash_key += 1
    return hash_key % 100
get_hash('503618705')

class HashTable:  
    def __init__(self):
        self.MAX = 100
        self.arr = [None for i in range(self.MAX)]

    def get_hash(self, key):
        key = int(key, base=10)
        hash_key = 0
        for i in range(key):
            hash_key += 1
        return hash_key % self.MAX

    def __getitem__(self, index):
        h = self.get_hash(index)
        return self.arr[h]

    def __setitem__(self, key, val):
        h = self.get_hash(key)
        self.arr[h] = val    

    def __delitem__(self, key):
        h = self.get_hash(key)
        self.arr[h] = None
t = HashTable()
t["503618705"] = "Tanzania"
t.arr
print(t.arr)

该代码没有错误,但我想对CSV文件中的所有记录进行哈希处理

python pandas list csv hash
1个回答
0
投票

您需要做的是正确使用您的方法。现在您__init__一个新对象t。然后,您将引用list中的索引并将"Tanzania"设置为值。 så实际上您没有在对象t中仅使用函数list,因此您可以做的是(我希望我正确理解了您的问题。):

© www.soinside.com 2019 - 2024. All rights reserved.