• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

python只在需要的时候应用装饰排序

python 水墨上仙 2083次浏览

python只在需要的时候应用装饰排序

    def lazyDSU_sort(seq, keys, warn=False):
        """Return sorted seq, breaking ties by applying keys only when needed.
        If ``warn`` is True then an error will be raised if there were no
        keys remaining to break ties.
        Examples
        ========
        Here, sequences are sorted by length, then sum:
        >>> seq, keys = [[[1, 2, 1], [0, 3, 1], [1, 1, 3], [2], [1]], [
        ...    lambda x: len(x),
        ...    lambda x: sum(x)]]
        ...
        >>> lazyDSU_sort(seq, keys)
        [[1], [2], [1, 2, 1], [0, 3, 1], [1, 1, 3]]
        If ``warn`` is True, an error will be raised if there were not
        enough keys to break ties:
        >>> lazyDSU_sort(seq, keys, warn=True)
        Traceback (most recent call last):
        ...
        ValueError: not enough keys to break ties
        Notes
        =====
        The decorated sort is one of the fastest ways to sort a sequence for
        which special item comparison is desired: the sequence is decorated,
        sorted on the basis of the decoration (e.g. making all letters lower
        case) and then undecorated. If one wants to break ties for items that
        have the same decorated value, a second key can be used. But if the
        second key is expensive to compute then it is inefficient to decorate
        all items with both keys: only those items having identical first key
        values need to be decorated. This function applies keys successively
        only when needed to break ties.
        """
        from collections import defaultdict
        d = defaultdict(list)
        keys = list(keys)
        f = keys.pop(0)
        for a in seq:
            d[f(a)].append(a)
        seq = []
        for k in sorted(d.keys()):
            if len(d[k]) > 1:
                if keys:
                    d[k] = lazyDSU_sort(d[k], keys, warn)
                elif warn:
                  raise ValueError('not enough keys to break ties')
                seq.extend(d[k])
            else:
                seq.append(d[k][0])
        return seq


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明python只在需要的时候应用装饰排序
喜欢 (0)
加载中……