首页 > 编程 > Python > 正文

Python学习笔记1——人人都爱列表

2019-11-14 17:50:33
字体:
来源:转载
供稿:网友

一些BIF函数在列表中的应用:

 1 Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 >>> cast=["cleese","palin","jones","idle"] 4 >>> PRint(cast) 5 ['cleese', 'palin', 'jones', 'idle'] 6 >>> print(len(cast))#显示数据项数量 7 4 8 >>> print(cast[1])#显示列表中第2个数据项的值 9 palin10 >>> cast.append("gilliam")#在列表末尾添加一个数据项11 >>> print(cast)12 ['cleese', 'palin', 'jones', 'idle', 'gilliam']13 >>> cast.pop()#删除列表末尾的数据项14 'gilliam'15 >>> print(cast)16 ['cleese', 'palin', 'jones', 'idle']17 >>> cast.extend(["gilliam","chapman"])#在列表末尾增加一个数据项集合18 >>> print(cast)19 ['cleese', 'palin', 'jones', 'idle', 'gilliam', 'chapman']20 >>> cast.remove("chapman")#删除指定的数据项21 >>> print(cast)22 ['cleese', 'palin', 'jones', 'idle', 'gilliam']23 >>> cast.insert(0,"chapman")#在指定的位置增加数据项24 >>> print(cast)25 ['chapman', 'cleese', 'palin', 'jones', 'idle', 'gilliam']26 >>> 

下面是讲定义一个def函数,isinstance()函数,for in,if else等的运用以及逻辑

 1 movies=["the holy grail",1975,"terry jone & terry gilliam",91, 2        ["graham chapman", 3        ["michael palin","john cleese","terry gilliam", 4             "eric idle","terry jones"]]] 5 def print_lol(the_list):#定义一种函数 6         for each_item in the_list:#for in循环迭代处理列表,从列表起始位置到末尾 7         if isinstance(each_item,list):#isinstance()检测each_item里每一项 8                                               #是不是list类型 9             print_lol(each_item)#如果是,调用函数print_lol10         else:print(each_item)#如果不是,输出这一项11 12 print_lol(movies)#在movies列表中调用函数13 """14 之前if else语句不对齐导致报错15 """

下一个笔记记录Win7环境下python自定义模块的发布


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表