目录
1、直接寻址表
2、直接寻址表缺点
3、哈希
4、哈希表
5、解决哈希冲突
6、拉链法
7、常见哈希函数
8、哈希表的实现
8.1迭代器iter()和__iter__
8.2str()和repr()
8.3代码实现哈希表
8.4哈希表的应用
def iter_test():
i = iter('happy')#!!!
try:
while True:
print i.next()
except StopIteration:
pass
s = {'one':1,'two':2,'three':3}
print s
m = iter(s) #!!!
try:
while True:
print m.next() #s[m.next()]
except StopIteration:
pass
if __name__ == '__main__':
iter_test()
h
a
p
p
y
{'three': 3, 'two': 2, 'one': 1}
three
two
one
__iter__()
和next()
函数__iter__()和next()
方法的类可以作为迭代器使用。class Fib(object):
def __init__(self):
self.a, self.b = 0, 1 # 初始化两个计数器a,b
def __iter__(self):
return self # 实例本身就是迭代对象,故返回自己
def next(self):
self.a, self.b = self.b, self.a + self.b # 计算下一个值
if self.a > 10: # 退出循环的条件
raise StopIteration();
return self.a # 返回下一个值
if __name__ == '__main__':
for n in Fib():
print n
1
1
2
3
5
8
repr()
和 str()
分别调用对象的__repr__
和__str__
repr()
更能显示出对象的类型、值等信息,对象描述清晰的。 而str()
能够让我们最快速了解到对象的内容,可读性较高__repr__
import datetime
s = 'hello'
d = datetime.datetime.now()
print(str(s))
print(repr(s))
print(str(d))
print(repr(d))
hello
'hello'
2023-6-13 22:39:18.014587
datetime.datetime(2023, 6, 13, 22, 39, 18, 14587)
Note:
def square(x) : # 计算平方数
... return x ** 2
...
>>> map(square, [1,2,3,4,5]) # 计算列表各个元素的平方
[1, 4, 9, 16, 25]
>>> map(lambda x: x ** 2, [1, 2, 3, 4, 5]) # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]
# hash
class Linklist:
class Node:
def __init__(self, item=None):
self.item = item
self.next = None
class LinkListIterator:
def __init__(self, node):
self.node = node
def __next__(self):
if self.node:
cur_node = self.node
self.node = cur_node.next
return cur_node.item
else:
raise StopIteration
def __iter__(self):
return self
def __init__(self, iterable=None):
self.head = None
self.tail = None
if iterable:
self.extend(iterable)
def append(self, obj):
s = Linklist.Node(obj)
if not self.head:
self.head = s
self.tail = s
else:
self.tail.next = s
self.tail = s
def extend(self, iterable):
for obj in iterable:
self.append(obj)
def find(self, obj):
for n in self:
if n == obj:
return True
else:
return False
def __iter__(self):
return self.LinkListIterator(self.head)
def __repr__(self):
return "<<" + ",".join(map(str, self)) + ">>"
# 类似于集合的结构
class HashTable:
def __init__(self, size=101):
self.size = size
self.T = [Linklist() for i in range(self.size)]
def h(self, k):
return k % self.size
def insert(self, k):
i = self.h(k)
if self.find(k):
print("Douplicated insert")
else:
self.T[i].append(k)
def find(self, k):
i = self.h(k)
return self.T[i].find(k)
lk = Linklist([1, 2, 3, 4])
print(lk)
ht = HashTable()
ht.insert(0)
ht.insert(1)
# hash
class Linklist:
class Node:
def __init__(self, item=None):
self.item = item
self.next = None
class LinkListIterator:
def __init__(self, node):
self.node = node
def __next__(self):
if self.node:
cur_node = self.node
self.node = cur_node.next
return cur_node.item
else:
raise StopIteration
def __iter__(self):
return self
def __init__(self, iterable=None):
self.head = None
self.tail = None
if iterable:
self.extend(iterable)
def append(self, obj):
s = Linklist.Node(obj)
if not self.head:
self.head = s
self.tail = s
else:
self.tail.next = s
self.tail = s
def extend(self, iterable):
for obj in iterable:
self.append(obj)
def find(self, obj):
for n in self:
if n == obj:
return True
else:
return False
def __iter__(self):
return self.LinkListIterator(self.head)
def __repr__(self):
return "<<" + ",".join(map(str, self)) + ">>"
# 类似于集合的结构
class HashTable:
def __init__(self, size=101):
self.size = size
self.T = [Linklist() for i in range(self.size)]
def h(self, k):
return k % self.size
def insert(self, k):
i = self.h(k)
if self.find(k):
print("Douplicated insert")
else:
self.T[i].append(k)
def find(self, k):
i = self.h(k)
return self.T[i].find(k)
lk = Linklist([1, 2, 3, 4])
print(lk)
ht = HashTable()
ht.insert(0)
ht.insert(1)
ht.insert(3)
ht.insert(102)
print(",".join(map(str, ht.T)))
结果
<<1,2,3,4>>
<<1,2,3,4>>
<<0>>,<<1,102>>,<<>>,<<3>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>,<<>>
Process finished with exit code 0
MD5算法