描述
這個cmp()方法比較兩個元組的元素。
用法
以下是語法cmp()方法 -
cmp(tuple1, tuple2)
參數
tuple1- 這是第一個要比較的元組
tuple2- 這是要比較的第二個元組
返回值
如果元素類型相同,則執行比較並返回結果。如果元素是不同的類型,請檢查它們是否是數字。
如果是數字,則在必要時執行數字強製並進行比較。
如果其中一個元素是數字,則另一個元素是 "larger"(數字是 "smallest")。
否則,類型按名稱的字母順序排序。
如果我們到達其中一個元組的末尾,則更長的元組是 "larger." 如果我們用盡兩個元組並共享相同的數據,則結果為平局,即返回 0。
示例
下麵的例子展示了 cmp() 方法的用法。
#!/usr/bin/python3
tuple1, tuple2 = (123, 'xyz'), (456, 'abc')
print cmp(tuple1, tuple2)
print cmp(tuple2, tuple1)
tuple3 = tuple2 + (786,);
print cmp(tuple2, tuple3)
結果
當我們運行上麵的程序時,它會產生以下結果 -
-1 1 -1
相關用法
- Python 3 Tuple tuple()用法及代碼示例
- Python 3 Tuple len()用法及代碼示例
- Python 3 Tuple max()用法及代碼示例
- Python 3 Tuple min()用法及代碼示例
- Python 3 Tkinter pack()用法及代碼示例
- Python 3 Tkinter place()用法及代碼示例
- Python 3 Number tan()用法及代碼示例
- Python 3 os.fstatvfs()用法及代碼示例
- Python 3 List pop()用法及代碼示例
- Python 3 os.minor()用法及代碼示例
- Python 3 dictionary cmp()用法及代碼示例
- Python 3 String isupper()用法及代碼示例
- Python 3 os.close()用法及代碼示例
- Python 3 List index()用法及代碼示例
- Python 3 String decode()用法及代碼示例
- Python 3 os.unlink()用法及代碼示例
- Python 3 os.major()用法及代碼示例
- Python 3 Number atan()用法及代碼示例
- Python 3 List len()用法及代碼示例
- Python 3 String maketrans()用法及代碼示例
注:本文由純淨天空篩選整理自 Python 3 - Tuple cmp() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。