标签:
python列表的创建与访问
movies = ["The Holy Grail","The Life of Brian","The Meaning of Life"] print(movies[1])
列表常用的BIF
print() 显示列表内容
len() 计算列表的数据项
append() 在列表末尾增加一个数据项 extend() 在列表末尾增加一个数据项集合
pop() 从列表末尾删除数据
remove() 找到并删除一个特定的数据项
insert() 在特定位置前增加一个数据项
cast = ["Cleese","Palin","Jones","Idle"] print(cast) print(len(cast)) cast.append("Gilliam") //末尾增加 cast.pop() //删除末尾数据 cast.extend(["Gilliam","Chapman"]) //增加一个数据集合 cast.remove("Cleese") //删除特定项 cast.insert(0,"Chapman") //在位置0之前增加一项
python中的for循环
movies = ["The Holy Grail","The Life of Brian"] for each in movies : print(each)
查询python内置方法与功能描述
dir(__builtins__) //内置方法列表 help(input) //得到input()这个函数的功能描述
isinstance()如何工作
names = [‘Michael‘,‘Terry‘] isinstance(names,list) //判断names是否属于list类型 如果是返回true num_names = len(names) isinstance(num_names,list) //此句返回false
利用递归,建立函数迭代输出含嵌套的列表
movies = ["The Holy Grail",1975,"Terry jones & Terry Gilliam",97, ["Graham Chapman",["Michael Palin","John Cleese", "Terry Gilliam","Eric Idle","Terry Jones"]]] def print_lol(the_list): for each_item in the_list: if isinstance(each_item,list): print_lol(each_item) //递归调用 else: print(each_item) print_lol(movies)
标签:
原文地址:http://www.cnblogs.com/bing0818/p/4882778.html