HeadFirstPython-初识python

HeadFirstPython-初识python

下面我们来看一下实例: 在书中,作者给我们展示了一个电影清单。在这里我们要学习列表的基本操作方法。

cast = ["Cless","plain","Jones","Idle"]

针对这个列表的实例,我们能做哪些操作?

打印出这个实例:

In [28]: print(cast) [‘Cless‘, ‘plain‘, ‘Jones‘, ‘Idle‘]

向这个实例添加一个新的数据项

In [29]: cast.append("iamappend")

In [30]: print(cast) [‘Cless‘, ‘plain‘, ‘Jones‘, ‘Idle‘, ‘iamappend‘]

从这个实例中弹出最后一个数据项In [31]: cast.pop() Out[31]: ‘iamappend‘

In [32]: print(cast) [‘Cless‘, ‘plain‘, ‘Jones‘, ‘Idle‘]

从这个实例中移除指定的数据项。比如我要移除plain In [33]: cast.remove("plain")

In [34]: print(cast) [‘Cless‘, ‘Jones‘, ‘Idle‘]

现在数据项比以前少了,我再插入到指定的位置上。In [35]: cast.insert(1,"plain")

In [36]: cast Out[36]: [‘Cless‘, ‘plain‘, ‘Jones‘, ‘Idle‘]

如何合并列表?In [37]: newa = ["Gmail","baidu","sina"]

In [38]: print(newa) [‘Gmail‘, ‘baidu‘, ‘sina‘]

In [39]: cast.extend(newa)

In [40]: cast Out[40]: [‘Cless‘, ‘plain‘, ‘Jones‘, ‘Idle‘, ‘Gmail‘, ‘baidu‘, ‘sina‘]

以上只是简单的描述了一个如何对列表做一个操作。

下一节: 向列表增加更多的数据。 在书13页中,第一题: 原题: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]

操作方法1.直接在进行人工插入In [42]: movies = ["The Holy Grail","The Life of Brain","The Meaning of Life"]

In [43]: movies.insert(1,1975)

In [44]: movies.insert(3,1979)

In [45]: movies.insert(5,1983)

这里可以直接使用 movies.append(1983) 因为这是最后一个了

In [46]: print(movies) [‘The Holy Grail‘, 1975, ‘The Life of Brain‘, 1979, ‘The Meaning of Life‘, 1983]

操作方法2.就是直接新建列表In [47]: movies = [‘The Holy Grail‘, 1975, ‘The Life of Brain‘, 1979, ‘The Meaning of Life‘, 1983]

In [48]: print(movies) [‘The Holy Grail‘, 1975, ‘The Life of Brain‘, 1979, ‘The Meaning of Life‘, 1983]

下一节 处理列表数据

简单的迭代fav_movies = ["The Holy Grail","The Life of Brian"]

In [54]: for eachfclick in favmovies: ....: print(each_fclick) ....: The Holy Grail The Life of Brian

基本的迭代都是由for 或者while进行操作。 两者的区别:while必须要使用状态信息,需要使用计数标识。否则有可能死循环下去。下面例子中 count就是作为计数标识符存在In [57]: while count < len(movies): ....: print(movies[count]) ....: count=count+1 ....: The Holy Grail 1975 The Life of Brain 1979 The Meaning of Life 1983

使用for作迭代时,python解释器将你考虑状态信息。

下一节,在列表中存储列表

很长的一个列表了In [62]: movies = ["The Holy Grail",1975,"Terry Jone & Terry Gillam",91, ["Graham chapman",["Michael Palin","Jone Cleese","Terry Gillam","Eric Idle","Terry Jone"]]]

In [63]: print(movies) [‘The Holy Grail‘, 1975, ‘Terry Jone & Terry Gillam‘, 91, [‘Graham chapman‘, [‘Michael Palin‘, ‘Jone Cleese‘, ‘Terry Gillam‘, ‘Eric Idle‘, ‘Terry Jone‘]]]

这么多层的列表肯定不是人工写的,使用Python如何来实现这么多层的列表呢In [74]: a = ["aaa","ccc"]

In [75]: b = ["aaa111","ccc111"]

In [76]: a.append(b) #如果是用a.extend的话,就是把两个列表进行合并了

In [77]: a Out[77]: [‘aaa‘, ‘ccc‘, [‘aaa111‘, ‘ccc111‘]]

多列表嵌套的迭代问题In [65]: for eachitem in movies: ....: print(eachitem) ....: The Holy Grail 1975 Terry Jone & Terry Gillam 91 [‘Graham chapman‘, [‘Michael Palin‘, ‘Jone Cleese‘, ‘Terry Gillam‘, ‘Eric Idle‘, ‘Terry Jone‘]]

使用简单的for循环无法做打打出第一个数据项

做一个迭代并且加上元素的类型判断In [66]: for eachitem in movies: ....: if isinstance(eachitem,list): ....: for each1item in eachitem: ....: if isinstance(each1item,list): ....: for each2item in each1item: ....: print(each2item) ....: else: ....: print(each1item) ....: else: ....: print(eachitem) ....: The Holy Grail 1975 Terry Jone & Terry Gillam 91 Graham chapman Michael Palin Jone Cleese Terry Gillam Eric Idle Terry Jone

上面的一个例子中,代码有得用的存在,随着列表的嵌套越来越多层的时候,这个循环是有问题。 下面使用一个函数进行简化操作:

In [69]: def printlol(thelist): ....: for eachitem in thelist: ....: if isinstance(eachitem,list): ....: printlol(eachitem) #重点在这一行上 ....: else: ....: print(eachitem) ....:

In [73]: print_lol(movies) The Holy Grail 1975 Terry Jone & Terry Gillam 91 Graham chapman Michael Palin Jone Cleese Terry Gillam Eric Idle Terry Jone

这里有一段按照书上指导写的函数

def printlol(thelist,indent=False,level=0): """ 模块参数说明: indent是一个用于控制是否缩进,默认值为False,启用值为Ture. level是一个用于控制缩进的级别,默认为0 """ for eachitem in thelist: if isinstance(eachitem,list): printlol(eachitem,indent,level+1) else: if indent: "这条语句表示一旦indent为Ture的时候,表示缩进已经打开" #for tabstop in range(level): #print "\t", print "\t" *level , #这种方式也是可以的,可以连这个循环都不用做 "在python 2.x里这使用这种方式。在python 3.x里使用其他方式" print(each_item)

HeadFirstPython-初识python

上一篇:C++笔试的几个小问题


下一篇:C++ new delete(一)