【问题标题】:How can I Hash hundred thousand records taken as a input from CSV file?如何散列十万条记录作为 CSV 文件的输入?
【发布时间】:2020-09-18 09:02:06
【问题描述】:

通过使用此代码,我只能散列 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】:

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

    lst = ['Sweden', 'Germany', 'Pakistan', 'Syria', 'Norway']
    idx = ["1234", "30", "500", "2034", "443"]
    
    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('50')
    
    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()
    for n, i in enumerate(lst):
        t.__setitem__(idx[n], lst[n])
    for i in range(len(idx)):
        print(t.__getitem__(idx[i]))
        print(t.get_hash(idx[i]))
    

    【讨论】:

    • 我想我的问题还不清楚,抱歉。我必须从 CSV 文件中获取输入。我希望通过代码读取整个 order_id 列并散列每个 order_id
    • 在get_hash函数中用order_id[i]改变idx。
    猜你喜欢
    • 2016-01-11
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 2021-11-27
    • 2021-04-24
    • 1970-01-01
    相关资源
    最近更新 更多