python列表操作使用示例分享
Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> cast=["cleese","palin","jones","idle"]
>>> print(cast)
['cleese', 'palin', 'jones', 'idle']
>>> print(len(cast))#顯示數(shù)據(jù)項(xiàng)數(shù)量
4
>>> print(cast[1])#顯示列表中第2個(gè)數(shù)據(jù)項(xiàng)的值
palin
>>> cast.append("gilliam")#在列表末尾添加一個(gè)數(shù)據(jù)項(xiàng)
>>> print(cast)
['cleese', 'palin', 'jones', 'idle', 'gilliam']
>>> cast.pop()#刪除列表末尾的數(shù)據(jù)項(xiàng)
'gilliam'
>>> print(cast)
['cleese', 'palin', 'jones', 'idle']
>>> cast.extend(["gilliam","chapman"])#在列表末尾增加一個(gè)數(shù)據(jù)項(xiàng)集合
>>> print(cast)
['cleese', 'palin', 'jones', 'idle', 'gilliam', 'chapman']
>>> cast.remove("chapman")#刪除指定的數(shù)據(jù)項(xiàng)
>>> print(cast)
['cleese', 'palin', 'jones', 'idle', 'gilliam']
>>> cast.insert(0,"chapman")#在指定的位置增加數(shù)據(jù)項(xiàng)
>>> print(cast)
['chapman', 'cleese', 'palin', 'jones', 'idle', 'gilliam']
>>>
下面是講定義一個(gè)def函數(shù),isinstance()函數(shù),for in,if else等的運(yùn)用以及邏輯
movies=["the holy grail",1975,"terry jone & terry gilliam",91,
["graham chapman",
["michael palin","john cleese","terry gilliam",
"eric idle","terry jones"]]]
def print_lol(the_list):#定義一種函數(shù)
for each_item in the_list:#for in循環(huán)迭代處理列表,從列表起始位置到末尾
if isinstance(each_item,list):#isinstance()檢測each_item里每一項(xiàng)
#是不是list類型
print_lol(each_item)#如果是,調(diào)用函數(shù)print_lol
else:print(each_item)#如果不是,輸出這一項(xiàng)
print_lol(movies)#在movies列表中調(diào)用函數(shù)
"""
之前if else語句不對齊導(dǎo)致報(bào)錯(cuò)
"""
- Python統(tǒng)計(jì)列表中的重復(fù)項(xiàng)出現(xiàn)的次數(shù)的方法
- Python中無限元素列表的實(shí)現(xiàn)方法
- Python3基礎(chǔ)之list列表實(shí)例解析
- Python常用列表數(shù)據(jù)結(jié)構(gòu)小結(jié)
- Python中字典(dict)和列表(list)的排序方法實(shí)例
- Python對兩個(gè)有序列表進(jìn)行合并和排序的例子
- python中的列表推導(dǎo)淺析
- Python 列表(List)操作方法詳解
- python列表去重的二種方法
- Python操作列表的常用方法分享
- Python中列表(list)操作方法匯總
相關(guān)文章
python append、extend與insert的區(qū)別
這篇文章主要介紹了python append、extend與insert的區(qū)別的相關(guān)資料,初學(xué)者對這幾個(gè)概念經(jīng)常搞混,這里就幫大家理清楚,需要的朋友可以參考下2016-10-10numpy刪除單行、刪除單列、刪除多列實(shí)現(xiàn)方式
這篇文章主要介紹了numpy刪除單行、刪除單列、刪除多列實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02Python數(shù)據(jù)可視化Pyecharts庫實(shí)現(xiàn)桑葚圖效果
這篇文章主要介紹了Python數(shù)據(jù)可視化如何使用Pyecharts庫來實(shí)現(xiàn)桑葚圖效果圖,文中給出實(shí)現(xiàn)的示例代碼,有需要的朋友可以借鑒參考想,希望能夠有所幫助2021-09-09python測試驅(qū)動(dòng)開發(fā)實(shí)例
這篇文章主要介紹了python測試驅(qū)動(dòng)開發(fā)實(shí)例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10使用Python實(shí)現(xiàn)控制攝像頭的方法詳解
當(dāng)今,隨著計(jì)算機(jī)技術(shù)的發(fā)展,攝像頭已經(jīng)成為了人們生活中不可或缺的一部分。而Python作為一種流行的編程語言,也可以輕松地控制和操作攝像頭。本文將介紹如何使用Python中的常用庫(例如OpenCV和Tkinter)來控制和操作攝像頭,需要的可以參考一下2023-03-03Python利用watchdog模塊監(jiān)控文件變化
這篇文章主要為大家介紹一個(gè)Python中的模塊:watchdog模塊,它可以實(shí)現(xiàn)監(jiān)控文件的變化。文中通過示例詳細(xì)介紹了watchdog模塊的使用,需要的可以參考一下2022-06-06