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

Python入門教程(十二)Python列表

 更新時間:2023年04月14日 10:54:45   作者:輕松學(xué)Python  
這篇文章主要介紹了Python入門教程(十二)Python列表,Python是一門非常強(qiáng)大好用的語言,也有著易上手的特性,本文為入門教程,需要的朋友可以參考下

Python 集合(數(shù)組)

Python 編程語言中有四種集合數(shù)據(jù)類型:

  • 列表(List)是一種有序和可更改的集合。允許重復(fù)的成員。
  • 元組(Tuple)是一種有序且不可更改的集合。允許重復(fù)的成員。
  • 集合(Set)是一個無序和無索引的集合。沒有重復(fù)的成員。
  • 詞典(Dictionary)是一個無序,可變和有索引的集合。沒有重復(fù)的成員。

選擇集合類型時,了解該類型的屬性很有用。

為特定數(shù)據(jù)集選擇正確的類型可能意味著保留含義,并且可能意味著提高效率或安全性。

列表

列表是一個有序且可更改的集合。在 Python 中,列表用方括號編寫。

實(shí)例

創(chuàng)建列表:

thislist = ["apple", "banana", "cherry"]
print(thislist)

運(yùn)行實(shí)例

訪問項(xiàng)目

您可以通過引用索引號來訪問列表項(xiàng):

實(shí)例

打印列表的第二項(xiàng):

thislist = ["apple", "banana", "cherry"]
print(thislist[1])

運(yùn)行實(shí)例

負(fù)的索引

負(fù)索引表示從末尾開始,-1 表示最后一個項(xiàng)目,-2 表示倒數(shù)第二個項(xiàng)目,依此類推。

實(shí)例

打印列表的最后一項(xiàng):

thislist = ["apple", "banana", "cherry"]
print(thislist[-1])

運(yùn)行實(shí)例

索引范圍

您可以通過指定范圍的起點(diǎn)和終點(diǎn)來指定索引范圍。

指定范圍后,返回值將是包含指定項(xiàng)目的新列表。

實(shí)例

返回第三、第四、第五項(xiàng):

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[2:5])

運(yùn)行實(shí)例

注釋:搜索將從索引 2(包括)開始,到索引 5(不包括)結(jié)束。

請記住,第一項(xiàng)的索引為 0。

負(fù)索引的范圍

如果要從列表末尾開始搜索,請指定負(fù)索引:

實(shí)例

此例將返回從索引 -4(包括)到索引 -1(排除)的項(xiàng)目:

thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"]
print(thislist[-4:-1])

運(yùn)行實(shí)例

更改項(xiàng)目值

如需更改特定項(xiàng)目的值,請引用索引號:

實(shí)例

更改第二項(xiàng):

thislist = ["apple", "banana", "cherry"]
thislist[1] = "mango"
print(thislist)

運(yùn)行實(shí)例

遍歷列表

您可以使用 for 循環(huán)遍歷列表項(xiàng):

實(shí)例

逐個打印列表中的所有項(xiàng)目:

thislist = ["apple", "banana", "cherry"]
for x in thislist: 
    print(x)

運(yùn)行實(shí)例

我們將在 Python For 循環(huán) 這一章中學(xué)習(xí)有關(guān) for 循環(huán)的更多知識。

檢查項(xiàng)目是否存在

如需確定列表中是否存在指定的項(xiàng),請使用 in 關(guān)鍵字:

實(shí)例

檢查列表中是否存在 “apple”:

thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
  print("Yes, 'apple' is in the fruits list")

運(yùn)行實(shí)例

列表長度

如需確定列表中有多少項(xiàng),請使用 len() 方法:

實(shí)例

打印列表中的項(xiàng)目數(shù):

thislist = ["apple", "banana", "cherry"]
print(len(thislist))

運(yùn)行實(shí)例

添加項(xiàng)目

如需將項(xiàng)目添加到列表的末尾,請使用 append() 方法:

實(shí)例

使用 append() 方法追加項(xiàng)目:

thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)

運(yùn)行實(shí)例

要在指定的索引處添加項(xiàng)目,請使用 insert() 方法:

實(shí)例

插入項(xiàng)目作為第二個位置:

thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)

運(yùn)行實(shí)例

刪除項(xiàng)目

有幾種方法可以從列表中刪除項(xiàng)目:

實(shí)例 remove() 方法刪除指定的項(xiàng)目:

thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)

運(yùn)行實(shí)例

實(shí)例 pop() 方法刪除指定的索引(如果未指定索引,則刪除最后一項(xiàng)):

thislist = ["apple", "banana", "cherry"]
thislist.pop()
print(thislist)

運(yùn)行實(shí)例

實(shí)例

del 關(guān)鍵字刪除指定的索引:

thislist = ["apple", "banana", "cherry"]
del thislist[0]
print(thislist)

運(yùn)行實(shí)例

實(shí)例

del 關(guān)鍵字也能完整地刪除列表:

thislist = ["apple", "banana", "cherry"]
del thislist

運(yùn)行實(shí)例

實(shí)例

clear() 方法清空列表:

thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)

運(yùn)行實(shí)例

復(fù)制列表

您只能通過鍵入 list2 = list1 來復(fù)制列表,因?yàn)椋簂ist2 將只是對 list1 的引用,list1 中所做的更改也將自動在 list2 中進(jìn)行。

有一些方法可以進(jìn)行復(fù)制,一種方法是使用內(nèi)置的 List 方法 copy()。

實(shí)例

使用 copy() 方法來復(fù)制列表:

thislist = ["apple", "banana", "cherry"]
mylist = thislist.copy()
print(mylist)

運(yùn)行實(shí)例

制作副本的另一種方法是使用內(nèi)建的方法 list()。

實(shí)例

使用 list() 方法復(fù)制列表:

thislist = ["apple", "banana", "cherry"]
mylist = list(thislist)
print(mylist)

運(yùn)行實(shí)例

合并兩個列表

在 Python 中,有幾種方法可以連接或串聯(lián)兩個或多個列表。

最簡單的方法之一是使用 + 運(yùn)算符。

實(shí)例

合并兩個列表:

list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)

運(yùn)行實(shí)例

連接兩個列表的另一種方法是將 list2 中的所有項(xiàng)一個接一個地追加到 list1 中:

實(shí)例

把 list2 追加到 list1 中:

list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
for x in list2: 
    list1.append(x)
    print(list1)

運(yùn)行實(shí)例

或者,您可以使用 extend() 方法,其目的是將一個列表中的元素添加到另一列表中:

實(shí)例

使用 extend() 方法將 list2 添加到 list1 的末尾:

list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)

運(yùn)行實(shí)例

list() 構(gòu)造函數(shù)

也可以使用 list() 構(gòu)造函數(shù)創(chuàng)建一個新列表。

實(shí)例

使用 list() 構(gòu)造函數(shù)創(chuàng)建列表:

thislist = list(("apple", "banana", "cherry")) # 請注意雙括號
print(thislist)

運(yùn)行實(shí)例

列表方法

Python 有一組可以在列表上使用的內(nèi)建方法。

到此這篇關(guān)于Python入門教程(十二)Python列表的文章就介紹到這了,更多相關(guān)Python列表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

荥阳市| 汽车| 长治县| 黄冈市| 河池市| 老河口市| 合江县| 遵义市| 合水县| 蒙城县| 玉环县| 蒙自县| 水城县| 如皋市| 成安县| 兴文县| 新邵县| 扶余县| 视频| 山西省| 朔州市| 疏附县| 汤阴县| 瑞安市| 边坝县| 莲花县| 旬阳县| 娄烦县| 扎囊县| 扎兰屯市| 广水市| 武功县| 祁连县| 乌审旗| 库伦旗| 赤城县| 卫辉市| 鹤壁市| 庐江县| 吴江市| 普洱|