當前位置: 首頁>>代碼示例>>Python>>正文


Python __builtin__.cmp方法代碼示例

本文整理匯總了Python中__builtin__.cmp方法的典型用法代碼示例。如果您正苦於以下問題:Python __builtin__.cmp方法的具體用法?Python __builtin__.cmp怎麽用?Python __builtin__.cmp使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在__builtin__的用法示例。


在下文中一共展示了__builtin__.cmp方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: cmpToKey

# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import cmp [as 別名]
def cmpToKey(mycmp):
    'Convert a cmp= function into a key= function'
    class K(object):
        def __init__(self, obj, *args):
            self.obj = obj
        def __lt__(self, other):
            return mycmp(self.obj, other.obj) < 0
        def __gt__(self, other):
            return mycmp(self.obj, other.obj) > 0
        def __eq__(self, other):
            return mycmp(self.obj, other.obj) == 0
        def __le__(self, other):
            return mycmp(self.obj, other.obj) <= 0
        def __ge__(self, other):
            return mycmp(self.obj, other.obj) >= 0
        def __ne__(self, other):
            return mycmp(self.obj, other.obj) != 0
    return K 
開發者ID:SrikanthVelpuri,項目名稱:tf-pose,代碼行數:20,代碼來源:python2_3.py

示例2: cmp

# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import cmp [as 別名]
def cmp(x, y):
        """
        cmp(x, y) -> integer

        Return negative if x<y, zero if x==y, positive if x>y.
        """
        return (x > y) - (x < y) 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:9,代碼來源:misc.py

示例3: cmp

# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import cmp [as 別名]
def cmp(a,b):
        if a>b:
            return 1
        elif b > a:
            return -1
        else:
            return 0 
開發者ID:SrikanthVelpuri,項目名稱:tf-pose,代碼行數:9,代碼來源:python2_3.py

示例4: lower_bound

# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import cmp [as 別名]
def lower_bound(haystack, needle, lo = 0, hi = None, cmp = None, key = None):
    """lower_bound(haystack, needle[, lo = 0[, hi = None[, cmp = None[, key = None]]]]) => n

Find \var{needle} via a binary search on \var{haystack}.  Returns the
index of the first match if \var{needle} is found, else a negative
value \var{N} is returned indicating the index where \var{needle}
belongs with the formula "-\var{N}-1".

\var{haystack} - the ordered, indexable sequence to search.
\var{needle} - the value to locate in \var{haystack}.
\var{lo} and \var{hi} - the range in \var{haystack} to search.
\var{cmp} - the cmp function used to order the \var{haystack} items.
\var{key} - the key function used to extract keys from the elements.
"""
    if cmp is None: cmp = __builtin__.cmp
    if key is None: key = lambda x: x
    if lo < 0: raise ValueError( 'lo cannot be negative' )
    if hi is None: hi = len(haystack)

    val = None
    while lo < hi:
        mid = (lo + hi) >> 1
        val = cmp(key(haystack[mid]), needle)
        if val < 0:
            lo = mid + 1
        else:
            hi = mid
    if val is None: return -1
    elif val == 0: return lo
    elif lo >= len(haystack): return -1 - lo
    elif cmp(key(haystack[lo]), needle) == 0: return lo
    else: return -1 - lo 
開發者ID:ActiveState,項目名稱:code,代碼行數:34,代碼來源:recipe-577565.py

示例5: upper_bound

# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import cmp [as 別名]
def upper_bound(haystack, needle, lo = 0, hi = None, cmp = None, key = None):
    """upper_bound(haystack, needle[, lo = 0[, hi = None[, cmp = None[, key = None]]]]) => n

Find \var{needle} via a binary search on \var{haystack}.  Returns the
non-negative index \var{N} of the element that immediately follows the
last match of \var{needle} if \var{needle} is found, else a negative
value \var{N} is returned indicating the index where \var{needle}
belongs with the formula "-\var{N}-1".

\var{haystack} - the ordered, indexable sequence to search.
\var{needle} - the value to locate in \var{haystack}.
\var{lo} and \var{hi} - the range in \var{haystack} to search.
\var{cmp} - the cmp function used to order the \var{haystack} items.
\var{key} - the key function used to extract keys from the elements.
"""
    if cmp is None: cmp = __builtin__.cmp
    if key is None: key = lambda x: x
    if lo < 0: raise ValueError( 'lo cannot be negative' )
    if hi is None: hi = len(haystack)

    val = None
    while lo < hi:
        mid = (lo + hi) >> 1
        val = cmp(key(haystack[mid]), needle)
        if val > 0:
            hi = mid
        else:
            lo = mid + 1
    if val is None: return -1
    elif val == 0: return lo
    elif lo > 0 and cmp(key(haystack[lo - 1]), needle) == 0: return lo
    else: return -1 - lo 
開發者ID:ActiveState,項目名稱:code,代碼行數:34,代碼來源:recipe-577565.py

示例6: rcmp

# 需要導入模塊: import __builtin__ [as 別名]
# 或者: from __builtin__ import cmp [as 別名]
def rcmp(x, y):
        return cmp(y, x) 
開發者ID:ActiveState,項目名稱:code,代碼行數:4,代碼來源:recipe-577565.py


注:本文中的__builtin__.cmp方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。