首页 > 代码库 > 【head first python】1.初识python 人人都爱列表

【head first python】1.初识python 人人都爱列表

#coding:utf-8

#创建简单的python列表
movies = ["The Holy Grail",
          "The Life of Brain",
          "The Meaning of Life"]
#可以放在同一行,但分开会更易读
#和数组一样,列表的项从零开始
print movies[1]
#>>>The Life of Brain

print movies
#>>>[‘The Holy Grail‘, ‘The Life of Brain‘, ‘The Meaning of Life‘]

print len(movies)
#>>>3

#在列表尾部增加append(),或删除pop()数据项,还可以在末尾增加一个数据项集合extend()
movies.pop()
print movies
#>>>[‘The Holy Grail‘, ‘The Life of Brain‘]
movies.append("my movie")
print movies
#>>>[‘The Holy Grail‘, ‘The Life of Brain‘, ‘my movie‘]
movies.extend(["another movie_1","another movie_2"])
print movies
#>>>[‘The Holy Grail‘, ‘The Life of Brain‘, ‘my movie‘, ‘another movie_1‘, ‘another movie_2‘]

#在列表中找到并删除一个特定项remove(),在某个特定项前插入一个数据项insert()
movies.remove("my movie")
print movies
#>>>[‘The Holy Grail‘, ‘The Life of Brain‘, ‘another movie_1‘, ‘another movie_2‘]
movies.insert(2,"insert movie")#第一个参数是位置,第二个是要插入的项
print movies
#>>>[‘The Holy Grail‘, ‘The Life of Brain‘, ‘insert movie‘, ‘another movie_1‘, ‘another movie_2‘]

#向列表增加更多的数据
#现在我们要向每个电影名后面添加上电影的发行年份,年份是数字,python允许在列表中混合类型
movies = ["The Holy Grail",
          "The Life of Brain",
          "The Meaning of Life"]
#初始化列表
#方案一:插入年份
movies.insert(1,1975)
movies.insert(3,1979)#注意每插入一项列表长度扩大一
movies.append(1983)
print movies
#>>>[‘The Holy Grail‘, 1975, ‘The Life of Brain‘, 1979, ‘The Meaning of Life‘, 1983]

movies = ["The Holy Grail",
          "The Life of Brain",
          "The Meaning of Life"]
#初始化列表
#方案二:从头构造列表
movies = ["The Holy Grail",1975,
          "The Life of Brain",1979,
          "The Meaning of Life",1983]
print movies
#>>>[‘The Holy Grail‘, 1975, ‘The Life of Brain‘, 1979, ‘The Meaning of Life‘, 1983]
‘‘‘对于小列表来说,第二种方法更好,比如现在,不必做复杂的运算‘‘‘

#处理列表数据
#该使用迭代了,for循环可以适用于任意大小的列表
for each_flick in movies:
    print each_flick
‘‘‘每个输出语句自动换行
>>>
The Holy Grail
1975
The Life of Brain
1979
The Meaning of Life
1983
‘‘‘
‘‘‘
字符串使用单引号和双引号均可
python大小写敏感
‘‘‘
#在列表中存储列表(列表的嵌套)
movies = [The Holy Grail, 1975, The Life of Brain, 1979, ["Graham Chapman",["Michael Palin","John Cleese","Terry Gilliam","Eric Idle"]]]
#打印列表中的某一项
print movies[4][1][2]
#>>>Terry Gilliam
#打印嵌套列表中的每一项
for each_item in movies:
    print each_item
‘‘‘>>>
The Holy Grail
1975
The Life of Brain
1979
[‘Graham Chapman‘, [‘Michael Palin‘, ‘John Cleese‘, ‘Terry Gilliam‘, ‘Eric Idle‘]] 
‘‘‘
#嵌套在内列表的下一层列表回原样打印,需要一种机制来发现列表中的某一项其实好似一个列表
#在列表中查找列表if…else
#使用什么判断条件?python有一个内置BIF可用,是isinstance(),用于检查某个特定标识符是否含有某个特定类型的数据
name = [Micheael,Terry]
a = isinstance(name,list)
print a
#>>>True
num_name = len(name)
a = isinstance(num_name,list)
print a
#>>>False
#该函数的返回值为True或False
#BIF有71多个,使用dir(__builtins__)查询python的内置方法表,具体查询使用help(某个BIF)

#重写使得嵌套列表逐项打印
movies = [The Holy Grail, 1975, The Life of Brain, 1979,
          ["Graham Chapman",["Michael Palin","John Cleese","Terry Gilliam","Eric Idle"]]]
for item in movies:
    if isinstance(item,list):
        for item_2 in item:
            if isinstance(item_2,list):
                for i in item_2:
                    print i
            else:
                print item_2
    else:
        print item
‘‘‘
>>> 
The Holy Grail
1975
The Life of Brain
1979
Graham Chapman
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
‘‘‘
#但是每增加一层嵌套,就要多写一层重复的代码来进行判断和打印
#不重复代码,应当创建一个函数
#函数需要反复调用,在函数代码组内调用自己
def print_lol(the_list):
    for item in the_list:
        if isinstance(item,list):
            print_lol(item)
        else:
            print item
print_lol(movies)
‘‘‘
>>>
The Holy Grail
1975
The Life of Brain
1979
Graham Chapman
Michael Palin
John Cleese
Terry Gilliam
Eric Idle
‘‘‘
#太棒了,递归不必改变任何代码就可以处理任意深度的嵌套列表
#第一章就到这里结束了=w=

本文原创,转载请注明出处http://www.cnblogs.com/Archimedes/p/7140622.html

【head first python】1.初识python 人人都爱列表