# -*- coding: UTF-8 -*-
class Assoc:
def __init__(self, key, value):
self.key = key
self.value = value
def __Lt__(self, other):
return self.key < other.key
def __Le__(self, other):
return self.key < other.key or self.key == other.key
def __str__(self):
return "Assoc({0},{1})".format(self.key, self.value)
def bi_search(lst, key): # 二分搜索
low, high = 0, len(lst) - 1
while low <= high:
mid = (low + high) // 2
if key == lst[mid].key:
return lst[mid].value
if key < lst[mid].key:
high = mid - 1
else:
low = mid + 1
字典和集合(python语言)
最新推荐文章于 2022-11-28 22:25:48 发布