本文整理汇总了Python中bst.BST.lookup方法的典型用法代码示例。如果您正苦于以下问题:Python BST.lookup方法的具体用法?Python BST.lookup怎么用?Python BST.lookup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bst.BST
的用法示例。
在下文中一共展示了BST.lookup方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import lookup [as 别名]
class PhoneBook:
def __init__(self):
self.__phonebook = BST()
def insert(self, number, name):
entry = self.__phonebook.lookup(name)
if entry is None:
self.__phonebook.insert(name, [number])
else:
value = entry.get_value()
value.append(number)
def lookup(self, name):
entry = self.__phonebook.lookup(name)
if entry is not None:
print('{}: [{}]'.format(entry.get_key(), entry.get_value()))
else:
print("NOT FOUND!")
def list(self):
all_entries = self.__phonebook.traverse(1)
for e in all_entries:
print('{}: [{}]'.format(e[0], ', '.join(e[1])))
def remove(self, name):
self.__phonebook.remove(name)
示例2: PhoneBook
# 需要导入模块: from bst import BST [as 别名]
# 或者: from bst.BST import lookup [as 别名]
class PhoneBook():
def __init__(self):
self.tree = BST(None, compare)
#inserts a new contact
def insert(self, number, name):
found = self.tree.lookup(lambda x: x["value"]["name"] is name)
if found is not None:
found["value"]["number"] = number
else:
self.tree.insert({
"name": name,
"number": number
})
#lookup a name and print its phone number
def lookup(self, name):
return self.tree.lookup(lambda x: x["value"]["name"] is name)
#list all records in an alphabetical order
def list(self):
result = []
self.tree.traverse(lambda x: result.append(x["value"]))
return result
#remove a record for a given name
def remove(name):
pass