我只想知道你对那个代码的看法!:)
import random
class Error(Exception):
def __init__(self, error=None):
self.error = error
def __str__(self):
return str(self.error)
class HashTable(object):
def __init__(self):
self.length = 20
self.keys = []
self.table = [[] for _ in range(self.length)]
def __len__(self):
return self.length
def __setitem__(self, key, value):
self.keys.append(key)
hashed_key = self._hash(key)
self.table[hashed_key].append((value)) #Wartosc jest dodawana do listy pod hashowanym indeksem
def _hash(self, key) :
return hash(key) % self.length
def show_list(self):
return self.table
def __getitem__(self, key):
for el in self.table[self._hash(key)]:
if el[0] == key:
return el[1]
def __delitem__(self, key):
for el in self.table[self._hash(key)]:
if el[0] == key:
self.table[self._hash(key)].remove(el)
def __iter__(self):
def Generator(data):
mylist = [_ for _ in data]
for i in mylist:
yield i
return Generator(self.table)
L = HashTable()
content = []
with open("song.txt", 'r', encoding = 'utf8') as travis:
for line in travis:
for word in line.split():
content.append(word)
for c in content:
L[c] = c
for x in L:
print(x)我对任何技巧和如何改进这个建议都会很有兴趣!:D
发布于 2021-09-01 09:18:47
我的观点是,在请求代码评审之前,您应该更好地进行测试。
line = " #Wartosc jest dodawana do listy pod hashowanym indeksem"
for word in line.split():
content.append(word)
for c in content:
L[c] = c
print(L["jest"])在每个函数中放入一个print语句,例如print('__getitem__ ran')。然后继续修改您的测试代码,直到看到它们。现在,您没有使用所有的方法。
编辑:而且,这并不是真正的东西,我将代码审查一旦测试。正确的审查是将其替换为:
HashTable = dict可读性和效率都比你的好!
https://codereview.stackexchange.com/questions/263552
复制相似问题