[Head First Python]1. 初始python-人人都爱列表

 movies = [ "hello", "world",["xin","lover",["Jerry","cat"]]]

 print movies
print "\n" for each_1 in movies:
if isinstance(each_1,list):
for each_2 in each_1:
if isinstance(each_2,list):
for each_3 in each_2:
print(each_3)
else:
print(each_2)
else:
print(each_1) 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 "\n"
print_lol(movies)

python列表看起来可能像数组, 不过还不只如此, 列表是完备的python集合对象,列表通过列表方法的形式提供一些现成得功能.

 >>> cast = ["hello","world","!!!"]    #创建列表
>>> print(cast); #打印列表 print
['hello', 'world', '!!!']
>>> print(cast[1]); #列表类似数组访问
world
>>> print(len(cast)); #列表元素个数 len
3
>>> cast.append("testadd"); #列表尾添加元素 append
>>> print(cast)
['hello', 'world', '!!!', 'testadd']
>>> cast.pop() #删除列表尾元素 pop
'testadd'
>>> print(cast)
['hello', 'world', '!!!']
>>> cast.extend(["ni","hao"]) #添加列表 extend
>>> print(cast)
['hello', 'world', '!!!', 'ni', 'hao']
>>> cast.remove("!!!") #删除指定元素 remove
>>> print(cast)
['hello', 'world', 'ni', 'hao']
>>> cast.insert(0,"test") #插入指定元素 insert
>>> print(cast)
['test', 'hello', 'world', 'ni', 'hao']
上一篇:python 如何将md5转为16字节


下一篇:计算机图形学DDA画线法+中点画线法+Bresenham画线法