Chenille

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

一些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 palin
10 >>> 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_lol
10         else:print(each_item)#如果不是,输出这一项
11 
12 print_lol(movies)#在movies列表中调用函数
13 """
14 之前if else语句不对齐导致报错
15 """

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

posted on 2014-02-18 23:44  Chenille  阅读(295)  评论(0)    收藏  举报