国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python列表操作使用示例分享

2019-11-25 18:30:39
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

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)用以及邏輯

復(fù)制代碼 代碼如下:

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ò)
"""

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 漳平市| 习水县| 上林县| 江孜县| 凤山县| 陆河县| 尼玛县| 东山县| 龙井市| 江达县| 静乐县| 泊头市| 北川| 兰坪| 宾阳县| 黑河市| 社旗县| 沁水县| 右玉县| 宝兴县| 香格里拉县| 磐安县| 同心县| 宣恩县| 嘉祥县| 静宁县| 华亭县| 扶绥县| 郧西县| 麻栗坡县| 泽州县| 富川| 安平县| 丰镇市| 宝山区| 古浪县| 汾西县| 土默特右旗| 安岳县| 浪卡子县| 长海县|