最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中的列表知識(shí)點(diǎn)匯總

 更新時(shí)間:2015年04月14日 10:00:20   投稿:goldensun  
這篇文章主要總結(jié)了一些Python中的列表的知識(shí)點(diǎn),來自于IBM官網(wǎng)技術(shù)文檔,需要的朋友可以參考下

Python list

在介紹 Python tuple 時(shí),我使用了類比的方法,將其比做一個(gè)袋子,您可以在袋子中存放不同的東西。Python list 與此非常類似,因此,它的功能與袋子的功能也非常類似。但有一點(diǎn)是不同的,即您可以使用方括號(hào)創(chuàng)建 list,如清單 1 所示。
清單 1. 在 Python 中創(chuàng)建一個(gè) list

>>> l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> type(l)
<type 'list'>
>>> el = []  # Create an empty list
>>> len(el)
0
>>> sl = [1]  # Create a single item list
>>> len(sl)
1
>>> sl = [1,]  # Create 
a single item list, as with a tuple
>>> len(sl)
1

本例展示如何創(chuàng)建包含從 0 到 9(包括 0 和 9)的簡單 list,以及如何創(chuàng)建一個(gè)空列表和一個(gè)包含單個(gè)條目的列表。如果您還記得的話,創(chuàng)建單個(gè)條目的 tuple 還需要在單個(gè)條目后面跟一個(gè)逗號(hào)。這是區(qū)分單個(gè)條目 tuple 與方法調(diào)用的必要條件,這一點(diǎn)將在以后的文章中詳細(xì)討論。而對(duì)于 list,則是不必要的,盡管也允許使用單個(gè)逗號(hào)。

與往常一樣,要獲取有關(guān) Python 主題的更多信息,您可以使用內(nèi)置的幫助解釋器,例如,清單 2 展示了如何開始 list 類的幫助描述。
清單 2. 獲取有關(guān) list 的幫助

>>> help(list)
Help on class list in module __builtin__:
class list(object)
 | list() -> new list
 | list(sequence) -> new list initialized from sequence's items
 | 
 | Methods defined here:
 | 
 | __add__(...)
 |   x.__add__(y) <==> x+y
 | 
 | __contains__(...)
 |   x.__contains__(y) <==> y in x
 | 
...

如果仔細(xì)觀察清單 2 中對(duì) list 類的描述,您會(huì)看到其中提供了兩個(gè)不同的構(gòu)造函數(shù):一個(gè)沒有參數(shù),另一個(gè)接受一個(gè)序列類作為參數(shù)。因此,使用構(gòu)造函數(shù)及方括號(hào)簡化符號(hào),可以創(chuàng)建 list。這就提供了很大的靈活性,原因是您可以方便地將現(xiàn)有的序列,如 tuple 或 string 轉(zhuǎn)換為 list,如清單 3 所示。不過,請(qǐng)注意,傳遞的參數(shù)必須是序列 —— 并且不只是對(duì)象序列 —— 否則將會(huì)出現(xiàn)錯(cuò)誤。對(duì)于任何序列類型,您都可以使用 len 方法容易地查找序列中條目的數(shù)量。
清單 3. 直接創(chuàng)建 list 對(duì)象

>>> l = list()
>>> type(l)
<type 'list'>
>>> len(l)
0
>>> l
[]
>>> l = list((0, 1, 2, 3, 4, 5, 6, 7, 
8, 9))  # Create a list from a tuple
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> len(l)
10
>>> l = list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])  # Create a list from a list
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> len(l)
10
>>> l = list(0, 1, 2, 3, 4, 5, 6, 7, 
8, 9)   # Error: Must pass in a sequence
Traceback (most recent call last):
 File "<stdin>", line 1, in ?
TypeError: list() takes at most 1 argument (10 given)
>>> l = list("0123456789") # Create a list from a string
>>> l
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> type(l)
<type 'list'>
>>> len(l)
10

正如您看到的,創(chuàng)建 list 是很容易的,如果還沒有嘗試過,現(xiàn)在可以試一試。您不僅能夠?qū)⑿蛄兄苯觽鬟f給構(gòu)造函數(shù),還可以將擁有元組或字符串的變量傳遞給 list 構(gòu)造函數(shù)。

很明顯,序列較為有用的主要原因是它可以非常方便地訪問序列中的條目。如果還記得對(duì) tuple 的討論,便知道可以在序列中一次訪問一個(gè)條目或者通過將條目切片來訪問條目。Python list 也可以使用相同的技術(shù),如清單 4 所示。
清單 4. 從 list 訪問條目

>>> l = list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> l[0]     # Get the first item in the list
0
>>> type(l[0])
<type 'int'>
>>> l[5]     # Get the sixth item in the list
5
>>> l[1:5]    # Get the second through fifth items
[1, 2, 3, 4]
>>> type(l[1:5])
<type 'list'>
>>> l[0::2]   # Get every second item 
[0, 2, 4, 6, 8]
>>> l[0], l[1], l[2]  
(0, 1, 2)

在以前的文章中已經(jīng)了解到,切片 是一個(gè)非常有用的概念,其一般形式為 l[start:end:step],其中 start 和 end 分別是開始和結(jié)束索引,step 是在切片時(shí)要跨過的條目數(shù)量。此外,還可以對(duì)結(jié)束索引使用負(fù)值,即從序列的結(jié)尾往回計(jì)數(shù)。另一個(gè)有用的功能是以一種很合適的方式處理錯(cuò)誤(如超過序列的長度)。如前一個(gè)例子所示,您還可以選擇忽略切片中使用的三個(gè)值中的一個(gè)或多個(gè)值。例如,我在切片 l[0::2] 中沒有使用結(jié)束索引。

可變的序列

在本文的開頭,我提到過 list 和 tuple 之間的主要區(qū)別在于 list 是一個(gè)可變的序列,這就意味著您不但可以方便地訪問 list 中的條目,而且可以方便地修改它們。但這會(huì)引起一個(gè)并發(fā)癥狀:您只能修改序列中的條目。若要向序列中添加條目(而不僅僅是修改條目),可使用 append 方法,如清單 5 所示。
清單 5. 修改 list

>>> l = []
>>> l[0] = 0   # The list is empty
Traceback (most recent call last):
 File "<stdin>", line 1, in ?
IndexError: list assignment index out of range
>>> l.append(0)
>>> l
[0]
>>> l[0] = 1
>>> l
[1]

正如前一個(gè)例子所演示的,嘗試修改不存在的 list 條目會(huì)導(dǎo)致出現(xiàn)錯(cuò)誤。這一點(diǎn)意義重大,并演示了 Python 方法生成錯(cuò)誤的情況。當(dāng)問題較為嚴(yán)重時(shí),將會(huì)產(chǎn)生一個(gè)錯(cuò)誤,如果問題較小并且可以很容易地處理,則忽略它。
異構(gòu)的可變序列

您可能想了解更為復(fù)雜的修改。通過綜合切片知識(shí)以及如何修改 list 的知識(shí),您應(yīng)該已經(jīng)獲得了非常重要的見識(shí):可以通過多種方式修改列表。就像 tuple 一樣,list 也可以持有不同類型的數(shù)據(jù)(或不同類型的對(duì)象),這就是我所說的異構(gòu)的可變序列。這兩種功能在清單 6 中進(jìn)行了更完整的描述。
清單 6. 異構(gòu)的可變 list

>>> l=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l[2] = 2
>>> type(l[2])
<type 'int'>
>>> l[2] = "two"   # Change the type of an element
>>> ,ype(l[2])
<type 'str'>
>>> l
[0, 1, 'two', 3, 4, 5, 6, 7, 8, 9]
>>> l[2] = l[2:5] * 2 
>>> l
[0, 1, ['two', 3, 4, 'two', 3, 4], 3, 4, 5, 6, 7, 8, 9]
>>> del(l[2])     # Remove single element
>>> l
[0, 1, 3, 4, 5, 6, 7, 8, 9]
>>> l[1:3] = []    # Remove a slice
>>> l 
[0, 4, 5, 6, 7, 8, 9]

修改 list 中的條目相當(dāng)容易:您可以適當(dāng)?shù)卦O(shè)置條目的值,甚至設(shè)置成另一種不同的類型,如 string 或另一 list。您還可以使用重復(fù)運(yùn)算符,可以將該運(yùn)算符識(shí)別為乘法運(yùn)算符,以便從小片段中構(gòu)建更大的列表。

前面的例子向您展示了如何向 list 中添加元素,以及如何修改 list 中的條目。前一個(gè)例子還演示了如何從 list 中刪除對(duì)象。刪除條目的第一個(gè)方法是使用 del 方法。使用此方法可以刪除一個(gè)條目或一個(gè)條目范圍。您還可以使用靈活而強(qiáng)大的切片方法從 list 中刪除切片。
數(shù)組

在前一個(gè)例子中您可以看到,list 可以包含另一個(gè) list 作為條目。如果擴(kuò)展此例子,您可能想知道每個(gè)條目由一個(gè) list 替換將會(huì)發(fā)生什么樣的事情。結(jié)果是一個(gè)數(shù)組,或者從更加數(shù)學(xué)方面來講是一個(gè)矩陣。清單 7 展示了如何使用 list 保持二維 (2-D) 或三維 (3-D) 數(shù)組。
清單 7. list 作為一個(gè)數(shù)組

>>> al = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
>>> al
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
>>> al[0][0]     # First element in 2D array
0
>>> al[2][2]     # Last element in 2D array
8
>>> al[1][2]
5
>>> al = [[[0, 1], [2, 3]], [[4, 5], [6, 7]]]
>>> al
[[[0, 1], [2, 3]], [[4, 5], [6, 7]]]
>>> al[0][0][1]
1
>>> len(al)      # Length of outer dimension
2
>>> len(al[0])    # Length of middle dimension
2
>>> len(al[0][0])   # Length of inner dimension
2

其他列表操作

list 對(duì)象具有許多可以應(yīng)用于現(xiàn)有列表的有用方法。例如,您可以反轉(zhuǎn) list 中的所有條目或排序 list。不過,要記住這些操作的一個(gè)重點(diǎn)在于,它們是就地 操作,這意味著它們會(huì)修改調(diào)用它們所針對(duì)的 list。因此,如果您嘗試創(chuàng)建新列表,并將其設(shè)置為對(duì)這些方法之一調(diào)用所產(chǎn)生的結(jié)果,則會(huì)得到一個(gè)空列表。

list 除可以用于模擬數(shù)組外,還可以用于模擬其他數(shù)據(jù)結(jié)構(gòu)。例如,append 和 pop 方法對(duì) list 函數(shù)的操作要么是先進(jìn)先出 (FIFO) 數(shù)據(jù)結(jié)構(gòu)(也稱為隊(duì)列),要么是后進(jìn)先出 (LIFO) 數(shù)據(jù)結(jié)構(gòu)(也稱為堆棧)。通過允許您將條目設(shè)置為從 list 中彈出(刪除并返回),pop 方法支持這些功能。如果彈出 list 的第一項(xiàng),則是一個(gè)隊(duì)列;反之,如果彈出 list 的最后一項(xiàng),則是一個(gè)堆棧,如清單 8 所示。
清單 8. 操縱 list

>>> l=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> id(l) # This is the object id for our current list
4525432
>>> l.reverse()    # Reverse the list
>>> l
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> id(l) # The id is the same, modified list in place.
4525432
>>> l.sort()     # Sort the list in numerical order
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> id(l) # Modified the existing list
4525432
>>> l.index(5)    # Same as l[5]
5
>>> l.count(0)    # How 
many times does '0' occur in the list
1
>>> l.pop()      # Take off the last item (Stack)
9
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> l.pop(5)     # Take out the fifth element
5
>>> l
[0, 1, 2, 3, 4, 6, 7, 8]
>>> l.pop(0)     # Take the first item off the list (Queue)
0
>>> l
[1, 2, 3, 4, 6, 7, 8]

列表:切片和切塊

本文介紹了 list,它是一個(gè)容器對(duì)象,可以方便地進(jìn)行修改,而且可以持有不同類型的數(shù)據(jù)。由于它具有相當(dāng)?shù)撵`活性,因此 list 是 Python 編程語言中最常用的結(jié)構(gòu)之一已不足為怪。list 像一個(gè)口袋,可以容納不同類型的數(shù)據(jù),并可以根據(jù)需要更改。您可以像使用數(shù)組一樣使用 list,以有組織的方式容納數(shù)據(jù);您還可以像使用隊(duì)列或堆棧一樣使用 list。在以后的文章中還將更為深入地探索這一靈活性,并介紹強(qiáng)大的編程技術(shù),即列表理解。

相關(guān)文章

最新評(píng)論

吴忠市| 盐源县| 云龙县| 墨竹工卡县| 五峰| 思南县| 高唐县| 东辽县| 来凤县| 沙雅县| 永康市| 贵阳市| 依兰县| 偃师市| 南开区| 周口市| 营山县| 宝鸡市| 邮箱| 绥棱县| 桂东县| 治多县| 探索| 曲阳县| 原平市| 长海县| 双鸭山市| 广饶县| 云梦县| 茌平县| 郴州市| 平邑县| 泗洪县| 仙居县| 建德市| 肇源县| 武川县| 治多县| 萨嘎县| 罗山县| 海安县|