字典和集合(python语言)

# -*- 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

猜你喜欢

转载自blog.csdn.net/zhangyu4863/article/details/80658947