日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:5. 數(shù)據(jù)結(jié)構(gòu)

5. 數(shù)據(jù)結(jié)構(gòu)

本章深入講解之前學過的一些內(nèi)容,同時,還增加了新的知識點。

成都創(chuàng)新互聯(lián)是工信部頒發(fā)資質(zhì)IDC服務器商,為用戶提供優(yōu)質(zhì)的移動服務器托管服務

5.1. 列表詳解

列表數(shù)據(jù)類型支持很多方法,列表對象的所有方法所示如下:

list.append(x)

在列表末尾添加一個元素,相當于 a[len(a):] = [x] 。

list.extend(iterable)

用可迭代對象的元素擴展列表。相當于 a[len(a):] = iterable 。

list.insert(i, x)

在指定位置插入元素。第一個參數(shù)是插入元素的索引,因此,a.insert(0, x) 在列表開頭插入元素, a.insert(len(a), x) 等同于 a.append(x) 。

list.remove(x)

從列表中刪除第一個值為 x 的元素。未找到指定元素時,觸發(fā) ValueError 異常。

list.pop([i])

刪除列表中指定位置的元素,并返回被刪除的元素。未指定位置時,a.pop() 刪除并返回列表的最后一個元素。(方法簽名中 i 兩邊的方括號表示該參數(shù)是可選的,不是要求輸入方括號。這種表示法常見于 python 參考庫)。

list.clear()

刪除列表里的所有元素,相當于 del a[:] 。

list.index(x[, start[, end]])

返回列表中第一個值為 x 的元素的零基索引。未找到指定元素時,觸發(fā) ValueError 異常。

可選參數(shù) startend 是切片符號,用于將搜索限制為列表的特定子序列。返回的索引是相對于整個序列的開始計算的,而不是 start 參數(shù)。

list.count(x)

返回列表中元素 x 出現(xiàn)的次數(shù)。

list.sort(**,key=None,reverse=False*)

就地排序列表中的元素(要了解自定義排序參數(shù),詳見 sorted())。

list.reverse()

翻轉(zhuǎn)列表中的元素。

list.copy()

返回列表的淺拷貝。相當于 a[:]

多數(shù)列表方法示例:

 
 
 
 
  1. >>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
  2. >>> fruits.count('apple')
  3. 2
  4. >>> fruits.count('tangerine')
  5. 0
  6. >>> fruits.index('banana')
  7. 3
  8. >>> fruits.index('banana', 4) # Find next banana starting at position 4
  9. 6
  10. >>> fruits.reverse()
  11. >>> fruits
  12. ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
  13. >>> fruits.append('grape')
  14. >>> fruits
  15. ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
  16. >>> fruits.sort()
  17. >>> fruits
  18. ['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
  19. >>> fruits.pop()
  20. 'pear'

You might have noticed that methods like insert, remove or sort that only modify the list have no return value printed — they return the default None. 1 This is a design principle for all mutable data structures in Python.

還有,不是所有數(shù)據(jù)都可以排序或比較。例如,[None, 'hello', 10] 就不可排序,因為整數(shù)不能與字符串對比,而 None 不能與其他類型對比。有些類型根本就沒有定義順序關(guān)系,例如,3+4j < 5+7j 這種對比操作就是無效的。

5.1.1. 用列表實現(xiàn)堆棧

使用列表方法實現(xiàn)堆棧非常容易,最后插入的最先取出(“后進先出”)。把元素添加到堆棧的頂端,使用 append() 。從堆棧頂部取出元素,使用 pop() ,不用指定索引。例如:

 
 
 
 
  1. >>> stack = [3, 4, 5]
  2. >>> stack.append(6)
  3. >>> stack.append(7)
  4. >>> stack
  5. [3, 4, 5, 6, 7]
  6. >>> stack.pop()
  7. 7
  8. >>> stack
  9. [3, 4, 5, 6]
  10. >>> stack.pop()
  11. 6
  12. >>> stack.pop()
  13. 5
  14. >>> stack
  15. [3, 4]

5.1.2. 用列表實現(xiàn)隊列

列表也可以用作隊列,最先加入的元素,最先取出(“先進先出”);然而,列表作為隊列的效率很低。因為,在列表末尾添加和刪除元素非???,但在列表開頭插入或移除元素卻很慢(因為所有其他元素都必須移動一位)。

實現(xiàn)隊列最好用 collections.deque,可以快速從兩端添加或刪除元素。例如:

 
 
 
 
  1. >>> from collections import deque
  2. >>> queue = deque(["Eric", "John", "Michael"])
  3. >>> queue.append("Terry") # Terry arrives
  4. >>> queue.append("Graham") # Graham arrives
  5. >>> queue.popleft() # The first to arrive now leaves
  6. 'Eric'
  7. >>> queue.popleft() # The second to arrive now leaves
  8. 'John'
  9. >>> queue # Remaining queue in order of arrival
  10. deque(['Michael', 'Terry', 'Graham'])

5.1.3. 列表推導式

列表推導式創(chuàng)建列表的方式更簡潔。常見的用法為,對序列或可迭代對象中的每個元素應用某種操作,用生成的結(jié)果創(chuàng)建新的列表;或用滿足特定條件的元素創(chuàng)建子序列。

例如,創(chuàng)建平方值的列表:

 
 
 
 
  1. >>> squares = []
  2. >>> for x in range(10):
  3. ... squares.append(x**2)
  4. ...
  5. >>> squares
  6. [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

注意,這段代碼創(chuàng)建(或覆蓋)變量 x,該變量在循環(huán)結(jié)束后仍然存在。下述方法可以無副作用地計算平方列表:

 
 
 
 
  1. squares = list(map(lambda x: x**2, range(10)))

或等價于:

 
 
 
 
  1. squares = [x**2 for x in range(10)]

上面這種寫法更簡潔、易讀。

列表推導式的方括號內(nèi)包含以下內(nèi)容:一個表達式,后面為一個 for 子句,然后,是零個或多個 forif 子句。結(jié)果是由表達式依據(jù) forif 子句求值計算而得出一個新列表。 舉例來說,以下列表推導式將兩個列表中不相等的元素組合起來:

 
 
 
 
  1. >>> [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
  2. [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

等價于:

 
 
 
 
  1. >>> combs = []
  2. >>> for x in [1,2,3]:
  3. ... for y in [3,1,4]:
  4. ... if x != y:
  5. ... combs.append((x, y))
  6. ...
  7. >>> combs
  8. [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

注意,上面兩段代碼中,for 和 if 的順序相同。

表達式是元組(例如上例的 (x, y))時,必須加上括號:

 
 
 
 
  1. >>> vec = [-4, -2, 0, 2, 4]
  2. >>> # create a new list with the values doubled
  3. >>> [x*2 for x in vec]
  4. [-8, -4, 0, 4, 8]
  5. >>> # filter the list to exclude negative numbers
  6. >>> [x for x in vec if x >= 0]
  7. [0, 2, 4]
  8. >>> # apply a function to all the elements
  9. >>> [abs(x) for x in vec]
  10. [4, 2, 0, 2, 4]
  11. >>> # call a method on each element
  12. >>> freshfruit = [' banana', ' loganberry ', 'passion fruit ']
  13. >>> [weapon.strip() for weapon in freshfruit]
  14. ['banana', 'loganberry', 'passion fruit']
  15. >>> # create a list of 2-tuples like (number, square)
  16. >>> [(x, x**2) for x in range(6)]
  17. [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]
  18. >>> # the tuple must be parenthesized, otherwise an error is raised
  19. >>> [x, x**2 for x in range(6)]
  20. File "", line 1
  21. [x, x**2 for x in range(6)]
  22. ^^^^^^^
  23. SyntaxError: did you forget parentheses around the comprehension target?
  24. >>> # flatten a list using a listcomp with two 'for'
  25. >>> vec = [[1,2,3], [4,5,6], [7,8,9]]
  26. >>> [num for elem in vec for num in elem]
  27. [1, 2, 3, 4, 5, 6, 7, 8, 9]

列表推導式可以使用復雜的表達式和嵌套函數(shù):

 
 
 
 
  1. >>> from math import pi
  2. >>> [str(round(pi, i)) for i in range(1, 6)]
  3. ['3.1', '3.14', '3.142', '3.1416', '3.14159']

5.1.4. 嵌套的列表推導式

列表推導式中的初始表達式可以是任何表達式,甚至可以是另一個列表推導式。

下面這個 3x4 矩陣,由 3 個長度為 4 的列表組成:

 
 
 
 
  1. >>> matrix = [
  2. ... [1, 2, 3, 4],
  3. ... [5, 6, 7, 8],
  4. ... [9, 10, 11, 12],
  5. ... ]

下面的列表推導式可以轉(zhuǎn)置行列:

 
 
 
 
  1. >>> [[row[i] for row in matrix] for i in range(4)]
  2. [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

As we saw in the previous section, the inner list comprehension is evaluated in the context of the for that follows it, so this example is equivalent to:

 
 
 
 
  1. >>> transposed = []
  2. >>> for i in range(4):
  3. ... transposed.append([row[i] for row in matrix])
  4. ...
  5. >>> transposed
  6. [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

反過來說,也等價于:

 
 
 
 
  1. >>> transposed = []
  2. >>> for i in range(4):
  3. ... # the following 3 lines implement the nested listcomp
  4. ... transposed_row = []
  5. ... for row in matrix:
  6. ... transposed_row.append(row[i])
  7. ... transposed.append(transposed_row)
  8. ...
  9. >>> transposed
  10. [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

實際應用中,最好用內(nèi)置函數(shù)替代復雜的流程語句。此時,zip() 函數(shù)更好用:

 
 
 
 
  1. >>> list(zip(*matrix))
  2. [(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]

關(guān)于本行中星號的詳細說明,參見 解包實參列表。

5.2. del 語句

del 語句按索引,而不是值從列表中移除元素。與返回值的 pop() 方法不同, del 語句也可以從列表中移除切片,或清空整個列表(之前是將空列表賦值給切片)。 例如:

 
 
 
 
  1. >>> a = [-1, 1, 66.25, 333, 333, 1234.5]
  2. >>> del a[0]
  3. >>> a
  4. [1, 66.25, 333, 333, 1234.5]
  5. >>> del a[2:4]
  6. >>> a
  7. [1, 66.25, 1234.5]
  8. >>> del a[:]
  9. >>> a
  10. []

del 也可以用來刪除整個變量:

 
 
 
 
  1. >>> del a

此后,再引用 a 就會報錯(直到為它賦與另一個值)。后文會介紹 del 的其他用法。

5.3. 元組和序列

列表和字符串有很多共性,例如,索引和切片操作。這兩種數(shù)據(jù)類型是 序列 (參見 序列類型 —- list, tuple, range)。隨著 Python 語言的發(fā)展,其他的序列類型也被加入其中。本節(jié)介紹另一種標準序列類型:元組。

元組由多個用逗號隔開的值組成,例如:

 
 
 
 
  1. >>> t = 12345, 54321, 'hello!'
  2. >>> t[0]
  3. 12345
  4. >>> t
  5. (12345, 54321, 'hello!')
  6. >>> # Tuples may be nested:
  7. ... u = t, (1, 2, 3, 4, 5)
  8. >>> u
  9. ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))
  10. >>> # Tuples are immutable:
  11. ... t[0] = 88888
  12. Traceback (most recent call last):
  13. File "", line 1, in
  14. TypeError: 'tuple' object does not support item assignment
  15. >>> # but they can contain mutable objects:
  16. ... v = ([1, 2, 3], [3, 2, 1])
  17. >>> v
  18. ([1, 2, 3], [3, 2, 1])

輸出時,元組都要由圓括號標注,這樣才能正確地解釋嵌套元組。輸入時,圓括號可有可無,不過經(jīng)常是必須的(如果元組是更大的表達式的一部分)。不允許為元組中的單個元素賦值,當然,可以創(chuàng)建含列表等可變對象的元組。

雖然,元組與列表很像,但使用場景不同,用途也不同。元組是 immutable (不可變的),一般可包含異質(zhì)元素序列,通過解包(見本節(jié)下文)或索引訪問(如果是 namedtuples,可以屬性訪問)。列表是 mutable (可變的),列表元素一般為同質(zhì)類型,可迭代訪問。

構(gòu)造 0 個或 1 個元素的元組比較特殊:為了適應這種情況,對句法有一些額外的改變。用一對空圓括號就可以創(chuàng)建空元組;只有一個元素的元組可以通過在這個元素后添加逗號來構(gòu)建(圓括號里只有一個值的話不夠明確)。丑陋,但是有效。例如:

 
 
 
 
  1. >>> empty = ()
  2. >>> singleton = 'hello', # <-- note trailing comma
  3. >>> len(empty)
  4. 0
  5. >>> len(singleton)
  6. 1
  7. >>> singleton
  8. ('hello',)

語句 t = 12345, 54321, 'hello!'元組打包 的例子:值 12345, 54321'hello!' 一起被打包進元組。逆操作也可以:

 
 
 
 
  1. >>> x, y, z = t

稱之為 序列解包 也是妥妥的,適用于右側(cè)的任何序列。序列解包時,左側(cè)變量與右側(cè)序列元素的數(shù)量應相等。注意,多重賦值其實只是元組打包和序列解包的組合。

5.4. 集合

Python 還支持 集合 這種數(shù)據(jù)類型。集合是由不重復元素組成的無序容器?;居梅òǔ蓡T檢測、消除重復元素。集合對象支持合集、交集、差集、對稱差分等數(shù)學運算。

創(chuàng)建集合用花括號或 set() 函數(shù)。注意,創(chuàng)建空集合只能用 set(),不能用 {},{} 創(chuàng)建的是空字典,下一小節(jié)介紹數(shù)據(jù)結(jié)構(gòu):字典。

以下是一些簡單的示例

 
 
 
 
  1. >>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
  2. >>> print(basket) # show that duplicates have been removed
  3. {'orange', 'banana', 'pear', 'apple'}
  4. >>> 'orange' in basket # fast membership testing
  5. True
  6. >>> 'crabgrass' in basket
  7. False
  8. >>> # Demonstrate set operations on unique letters from two words
  9. ...
  10. >>> a = set('abracadabra')
  11. >>> b = set('alacazam')
  12. >>> a # unique letters in a
  13. {'a', 'r', 'b', 'c', 'd'}
  14. >>> a - b # letters in a but not in b
  15. {'r', 'd', 'b'}
  16. >>> a | b # letters in a or b or both
  17. {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
  18. >>> a & b # letters in both a and b
  19. {'a', 'c'}
  20. >>> a ^ b # letters in a or b but not both
  21. {'r', 'd', 'b', 'm', 'z', 'l'}

與 列表推導式 類似,集合也支持推導式:

 
 
 
 
  1. >>> a = {x for x in 'abracadabra' if x not in 'abc'}
  2. >>> a
  3. {'r', 'd'}

5.5. 字典

字典 (參見 映射類型 —- dict) 也是一種常用的 Python 內(nèi)置數(shù)據(jù)類型。其他語言可能把字典稱為 聯(lián)合內(nèi)存聯(lián)合數(shù)組。與以連續(xù)整數(shù)為索引的序列不同,字典以 關(guān)鍵字 為索引,關(guān)鍵字通常是字符串或數(shù)字,也可以是其他任意不可變類型。只包含字符串、數(shù)字、元組的元組,也可以用作關(guān)鍵字。但如果元組直接或間接地包含了可變對象,就不能用作關(guān)鍵字。列表不能當關(guān)鍵字,因為列表可以用索引、切片、append() 、extend() 等方法修改。

可以把字典理解為 鍵值對 的集合,但字典的鍵必須是唯一的?;ɡㄌ?{} 用于創(chuàng)建空字典。另一種初始化字典的方式是,在花括號里輸入逗號分隔的鍵值對,這也是字典的輸出方式。

字典的主要用途是通過關(guān)鍵字存儲、提取值。用 del 可以刪除鍵值對。用已存在的關(guān)鍵字存儲值,與該關(guān)鍵字關(guān)聯(lián)的舊值會被取代。通過不存在的鍵提取值,則會報錯。

對字典執(zhí)行 list(d) 操作,返回該字典中所有鍵的列表,按插入次序排列(如需排序,請使用 sorted(d))。檢查字典里是否存在某個鍵,使用關(guān)鍵字 in。

以下是一些字典的簡單示例:

 
 
 
 
  1. >>> tel = {'jack': 4098, 'sape': 4139}
  2. >>> tel['guido'] = 4127
  3. >>> tel
  4. {'jack': 4098, 'sape': 4139, 'guido': 4127}
  5. >>> tel['jack']
  6. 4098
  7. >>> del tel['sape']
  8. >>> tel['irv'] = 4127
  9. >>> tel
  10. {'jack': 4098, 'guido': 4127, 'irv': 4127}
  11. >>> list(tel)
  12. ['jack', 'guido', 'irv']
  13. >>> sorted(tel)
  14. ['guido', 'irv', 'jack']
  15. >>> 'guido' in tel
  16. True
  17. >>> 'jack' not in tel
  18. False

dict() 構(gòu)造函數(shù)可以直接用鍵值對序列創(chuàng)建字典:

 
 
 
 
  1. >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
  2. {'sape': 4139, 'guido': 4127, 'jack': 4098}

字典推導式可以用任意鍵值表達式創(chuàng)建字典:

 
 
 
 
  1. >>> {x: x**2 for x in (2, 4, 6)}
  2. {2: 4, 4: 16, 6: 36}

關(guān)鍵字是比較簡單的字符串時,直接用關(guān)鍵字參數(shù)指定鍵值對更便捷:

 
 
 
 
  1. >>> dict(sape=4139, guido=4127, jack=4098)
  2. {'sape': 4139, 'guido': 4127, 'jack': 4098}

5.6. 循環(huán)的技巧

在字典中循環(huán)時,用 items() 方法可同時取出鍵和對應的值:

 
 
 
 
  1. >>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
  2. >>> for k, v in knights.items():
  3. ... print(k, v)
  4. ...
  5. gallahad the pure
  6. robin the brave

在序列中循環(huán)時,用 enumerate() 函數(shù)可以同時取出位置索引和對應的值:

 
 
 
 
  1. >>> for i, v in enumerate(['tic', 'tac', 'toe']):
  2. ... print(i, v)
  3. ...
  4. 0 tic
  5. 1 tac
  6. 2 toe

同時循環(huán)兩個或多個序列時,用 zip() 函數(shù)可以將其內(nèi)的元素一一匹配:

 
 
 
 
  1. >>> questions = ['name', 'quest', 'favorite color']
  2. >>> answers = ['lancelot', 'the holy grail', 'blue']
  3. >>> for q, a in zip(questions, answers):
  4. ... print('What is your {0}? It is {1}.'.format(q, a))
  5. ...
  6. What is your name? It is lancelot.
  7. What is your quest? It is the holy grail.
  8. What is your favorite color? It is blue.

逆向循環(huán)序列時,先正向定位序列,然后調(diào)用 reversed() 函數(shù):

 
 
 
 
  1. >>> for i in reversed(range(1, 10, 2)):
  2. ... print(i)
  3. ...
  4. 9
  5. 7
  6. 5
  7. 3
  8. 1

按指定順序循環(huán)序列,可以用 sorted() 函數(shù),在不改動原序列的基礎(chǔ)上,返回一個重新的序列:

 
 
 
 
  1. >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
  2. >>> for i in sorted(basket):
  3. ... print(i)
  4. ...
  5. apple
  6. apple
  7. banana
  8. orange
  9. orange
  10. pear

使用 set() 去除序列中的重復元素。使用 sorted() 加 set() 則按排序后的順序,循環(huán)遍歷序列中的唯一元素:

 
 
 
 
  1. >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
  2. >>> for f in sorted(set(basket)):
  3. ... print(f)
  4. ...
  5. apple
  6. banana
  7. orange
  8. pear

一般來說,在循環(huán)中修改列表的內(nèi)容時,創(chuàng)建新列表比較簡單,且安全:

 
 
 
 
  1. >>> import math
  2. >>> raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]
  3. >>> filtered_data = []
  4. >>> for value in raw_data:
  5. ... if not math.isnan(value):
  6. ... filtered_data.append(value)
  7. ...
  8. >>> filtered_data
  9. [56.2, 51.7, 55.3, 52.5, 47.8]

5.7. 深入條件控制

whileif 條件句不只可以進行比較,還可以使用任意運算符。

比較運算符 innot in 用于執(zhí)行確定一個值是否存在(或不存在)于某個容器中的成員檢測。 運算符 isis not 用于比較兩個對象是否是同一個對象。 所有比較運算符的優(yōu)先級都一樣,且低于任何數(shù)值運算符。

比較操作支持鏈式操作。例如,a < b == c 校驗 a 是否小于 b,且 b 是否等于 c。

比較操作可以用布爾運算符 andor 組合,并且,比較操作(或其他布爾運算)的結(jié)果都可以用 not 取反。這些操作符的優(yōu)先級低于比較操作符;not 的優(yōu)先級最高, or 的優(yōu)先級最低,因此,A and not B or C 等價于 (A and (not B)) or C。與其他運算符操作一樣,此處也可以用圓括號表示想要的組合。

布爾運算符 andor 也稱為 短路 運算符:其參數(shù)從左至右解析,一旦可以確定結(jié)果,解析就會停止。例如,如果 AC 為真,B 為假,那么 A and B and C 不會解析 C。用作普通值而不是布爾值時,短路操作符返回的值通常是最后一個變量。

還可以把比較操作或邏輯表達式的結(jié)果賦值給變量,例如:

 
 
 
 
  1. >>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance'
  2. >>> non_null = string1 or string2 or string3
  3. >>> non_null
  4. 'Trondheim'

注意,Python 與 C 不同,在表達式內(nèi)部賦值必須顯式使用 海象運算符 :=。 這避免了 C 程序中常見的問題:要在表達式中寫 == 時,卻寫成了 =

5.8. 序列和其他類型的比較

序列對象可以與相同序列類型的其他對象比較。這種比較使用 字典式 順序:首先,比較前兩個對應元素,如果不相等,則可確定比較結(jié)果;如果相等,則比較之后的兩個元素,以此類推,直到其中一個序列結(jié)束。如果要比較的兩個元素本身是相同類型的序列,則遞歸地執(zhí)行字典式順序比較。如果兩個序列中所有的對應元素都相等,則兩個序列相等。如果一個序列是另一個的初始子序列,則較短的序列可被視為較小(較少)的序列。 對于字符串來說,字典式順序使用 Unicode 碼位序號排序單個字符。下面列出了一些比較相同類型序列的例子:

 
 
 
 
  1. (1, 2, 3) < (1, 2, 4)
  2. [1, 2, 3] < [1, 2, 4]
  3. 'ABC' < 'C' < 'Pascal' < 'Python'
  4. (1, 2, 3, 4) < (1, 2, 4)
  5. (1, 2) < (1, 2, -1)
  6. (1, 2, 3) == (1.0, 2.0, 3.0)
  7. (1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4)

注意,對不同類型的對象來說,只要待比較的對象提供了合適的比較方法,就可以使用 <> 進行比較。例如,混合數(shù)值類型通過數(shù)值進行比較,所以,0 等于 0.0,等等。否則,解釋器不會隨便給出一個對比結(jié)果,而是觸發(fā) TypeError 異常。

備注

1

別的語言可能會返回可變對象,允許方法連續(xù)執(zhí)行,例如,d->insert("a")->remove("b")->sort();。


網(wǎng)站題目:創(chuàng)新互聯(lián)Python教程:5. 數(shù)據(jù)結(jié)構(gòu)
路徑分享:http://www.5511xx.com/article/cojpgog.html