Python?列表操作全面教程示例
將元組的元素添加到列表中
thislist = ["apple", "banana", "cherry"] thistuple = ("kiwi", "orange") thislist.extend(thistuple) print(thislist)
remove() 方法用于移除指定的項目
示例,移除 "banana":
thislist = ["apple", "banana", "cherry"] thislist.remove("banana") print(thislist)
如果存在多個具有指定值的項目,則 remove() 方法將刪除第一個出現(xiàn)的項目:
示例,移除第一個出現(xiàn)的 "banana":
thislist = ["apple", "banana", "cherry", "banana", "kiwi"] thislist.remove("banana") print(thislist)
pop() 方法用于移除指定的索引
示例,移除第二個項目:
thislist = ["apple", "banana", "cherry"] thislist.pop(1) print(thislist)
如果不指定索引,pop() 方法將移除最后一個項目。
示例,移除最后一個項目:
thislist = ["apple", "banana", "cherry"] thislist.pop() print(thislist)
使用 del 關鍵字也可以移除指定的索引
示例,移除第一個項目:
thislist = ["apple", "banana", "cherry"] del thislist[0] print(thislist)
del 關鍵字還可以完全刪除列表。
示例,刪除整個列表:
thislist = ["apple", "banana", "cherry"] del thislist
clear() 方法用于清空列表
列表仍然存在,但沒有內容。
示例,清空列表內容:
thislist = ["apple", "banana", "cherry"] thislist.clear() print(thislist)
通過列表進行循環(huán),您可以使用 for 循環(huán)遍歷列表項:
示例,逐個打印列表中的所有項目:
thislist = ["apple", "banana", "cherry"] for x in thislist: print(x)
您還可以通過引用它們的索引編號來遍歷列表項。
range() 和 len() 函數(shù)創(chuàng)建一個合適的可迭代對象
示例,通過引用它們的索引編號打印所有項目:
thislist = ["apple", "banana", "cherry"] for i in range(len(thislist)): print(thislist[i])
上面示例中創(chuàng)建的可迭代對象是 [0, 1, 2]。您可以使用 while 循環(huán)遍歷列表項。使用 len() 函數(shù)來確定列表的長度,然后從 0 開始,通過引用它們的索引遍歷列表項。記得在每次迭代后將索引增加 1。
示例,使用 while 循環(huán)打印所有項目,通過遍歷所有索引編號:
thislist = ["apple", "banana", "cherry"] i = 0 while i < len(thislist): print(thislist[i]) i = i + 1
列表推導式
列表推導式在您想要基于現(xiàn)有列表的值創(chuàng)建新列表時提供了更短的語法。
示例:假設有一個水果列表,您想要一個新列表,其中僅包含名稱中帶有字母 "a" 的水果。
如果不使用列表推導式,您將不得不編寫一個帶有條件測試的 for 語句:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"] newlist = [] for x in fruits: if "a" in x: newlist.append(x) print(newlist)
使用列表推導式,您只需要一行代碼就可以完成所有操作:
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]
返回值是一個新列表,不會改變舊列表。條件就像一個篩選器,只接受計算結果為 True 的項目。
示例,僅接受不是 "apple" 的項目:
newlist = [x for x in fruits if x != "apple"]
條件 if x != "apple" 會對除了 "apple" 之外的所有元素返回 True,使新列表包含除 "apple" 之外的所有水果。條件是可選的,可以省略:
示例,沒有 if 語句:
newlist = [x for x in fruits]
可迭代對象可以是任何可迭代的對象,如列表、元組、集合等。
示例,您可以使用 range() 函數(shù)創(chuàng)建一個可迭代對象:
newlist = [x for x in range(10)]
同樣的示例,但帶有條件:
示例,只接受小于 5 的數(shù)字:
newlist = [x for x in range(10) if x < 5]
表達式是迭代中的當前項目,但它也是結果,您可以在最終成為新列表中的列表項之前對其進行操作:
示例,將新列表中的值設置為大寫:
newlist = [x.upper() for x in fruits]
您可以將結果設置為您喜歡的任何內容
以上就是Python 列表操作全面教程示例的詳細內容,更多關于Python 列表操作的資料請關注腳本之家其它相關文章!
相關文章
Python pypinyin注音庫輕松絲滑實現(xiàn)漢字轉換成拼音
pypinyin 庫,能像功夫熊貓那樣,輕松、快捷地幫你把漢字轉換成拼音,有了 pypinyin,不僅可以節(jié)省寶貴的時間,還可以更準確地展示中文字符的讀音,使文化交流更為順暢,本文帶大家一起探索 pypinyin 庫的魅力2024-01-01Python正則表達式匹配數(shù)字和小數(shù)的方法
這篇文章主要介紹了Python正則匹配數(shù)字和小數(shù)的方法,本文通過示例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07Python動態(tài)規(guī)劃實現(xiàn)虛擬機部署的算法思想
這篇文章主要介紹了Python動態(tài)規(guī)劃實現(xiàn)虛擬機部署的算法思想,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07詳解如何在VS Code中安裝Spire.PDF for Python
這篇文章主要為大家詳細介紹了如何在VS Code中安裝Spire.PDF for Python,文中的示例代碼簡潔易懂,有需要的小伙伴可以跟隨小編一起學習一下2023-10-10