通过使用此代码,我只能对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文件中的所有记录进行哈希处理
您需要做的是正确使用您的方法。现在您__init__
一个新对象t
。然后,您将引用list
中的索引并将"Tanzania"
设置为值。 så实际上您没有在对象t
中仅使用函数list
,因此您可以做的是(我希望我正确理解了您的问题。):