亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python?列表操作全面教程示例

 更新時(shí)間:2023年10月04日 11:05:16   作者:小萬哥  
這篇文章主要為大家介紹了Python?列表操作的全面教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

將元組的元素添加到列表中

thislist = ["apple", "banana", "cherry"]
thistuple = ("kiwi", "orange")
thislist.extend(thistuple)
print(thislist)

remove() 方法用于移除指定的項(xiàng)目

示例,移除 "banana":

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

如果存在多個(gè)具有指定值的項(xiàng)目,則 remove() 方法將刪除第一個(gè)出現(xiàn)的項(xiàng)目:

示例,移除第一個(gè)出現(xiàn)的 "banana":

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

pop() 方法用于移除指定的索引

示例,移除第二個(gè)項(xiàng)目:

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

如果不指定索引,pop() 方法將移除最后一個(gè)項(xiàng)目。

示例,移除最后一個(gè)項(xiàng)目:

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

使用 del 關(guān)鍵字也可以移除指定的索引

示例,移除第一個(gè)項(xiàng)目:

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

del 關(guān)鍵字還可以完全刪除列表。

示例,刪除整個(gè)列表:

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

clear() 方法用于清空列表

列表仍然存在,但沒有內(nèi)容。

示例,清空列表內(nèi)容:

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

通過列表進(jìn)行循環(huán),您可以使用 for 循環(huán)遍歷列表項(xiàng):

示例,逐個(gè)打印列表中的所有項(xiàng)目:

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

您還可以通過引用它們的索引編號來遍歷列表項(xiàng)。

range() 和 len() 函數(shù)創(chuàng)建一個(gè)合適的可迭代對象

示例,通過引用它們的索引編號打印所有項(xiàng)目:

thislist = ["apple", "banana", "cherry"]
for i in range(len(thislist)):
  print(thislist[i])

上面示例中創(chuàng)建的可迭代對象是 [0, 1, 2]。您可以使用 while 循環(huán)遍歷列表項(xiàng)。使用 len() 函數(shù)來確定列表的長度,然后從 0 開始,通過引用它們的索引遍歷列表項(xiàng)。記得在每次迭代后將索引增加 1。

示例,使用 while 循環(huán)打印所有項(xiàng)目,通過遍歷所有索引編號:

thislist = ["apple", "banana", "cherry"]
i = 0
while i < len(thislist):
  print(thislist[i])
  i = i + 1

列表推導(dǎo)式

列表推導(dǎo)式在您想要基于現(xiàn)有列表的值創(chuàng)建新列表時(shí)提供了更短的語法。

示例:假設(shè)有一個(gè)水果列表,您想要一個(gè)新列表,其中僅包含名稱中帶有字母 "a" 的水果。

如果不使用列表推導(dǎo)式,您將不得不編寫一個(gè)帶有條件測試的 for 語句:

fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []

for x in fruits:
  if "a" in x:
    newlist.append(x)

print(newlist)

使用列表推導(dǎo)式,您只需要一行代碼就可以完成所有操作:

fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]
print(newlist)

語法

newlist = [expression for item in iterable if condition == True]

返回值是一個(gè)新列表,不會(huì)改變舊列表。條件就像一個(gè)篩選器,只接受計(jì)算結(jié)果為 True 的項(xiàng)目。

示例,僅接受不是 "apple" 的項(xiàng)目:

newlist = [x for x in fruits if x != "apple"]

條件 if x != "apple" 會(huì)對除了 "apple" 之外的所有元素返回 True,使新列表包含除 "apple" 之外的所有水果。條件是可選的,可以省略:

示例,沒有 if 語句:

newlist = [x for x in fruits]

可迭代對象可以是任何可迭代的對象,如列表、元組、集合等。

示例,您可以使用 range() 函數(shù)創(chuàng)建一個(gè)可迭代對象:

newlist = [x for x in range(10)]

同樣的示例,但帶有條件:

示例,只接受小于 5 的數(shù)字:

newlist = [x for x in range(10) if x < 5]

表達(dá)式是迭代中的當(dāng)前項(xiàng)目,但它也是結(jié)果,您可以在最終成為新列表中的列表項(xiàng)之前對其進(jìn)行操作:

示例,將新列表中的值設(shè)置為大寫:

newlist = [x.upper() for x in fruits]

您可以將結(jié)果設(shè)置為您喜歡的任何內(nèi)容

以上就是Python 列表操作全面教程示例的詳細(xì)內(nèi)容,更多關(guān)于Python 列表操作的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論