资源描述:
《v26编程指南中文版数据结构》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、v26编程指南中文版数据结构Navigationindexmodules
2、next
3、previous
4、Pythonv2.6.5documentation?ThePythonTutorial?5.DataStructures数据结构?Thischapterdescribessomethingsyou'velearnedaboutalreadyinmoredetail,andaddssomenewthingsaswell.本章将更加详细的介绍一些你已经了解的东西,另外我们还会添加一些新的东西。5.1.MoreonListsList的更多细节?
5、Thelistdatatypehassomemoremethods.Hereareallofthemethodsoflistobjects:List数据类型还有其他的一些方法。如下是List的所有方法:list.append(x)Addanitemtotheendofthelist;equivalenttoa[len(a):]=[x].list.extend(L)Extendthelistbyappendingalltheitemsinthegivenlist;equivalenttoa[len(a):]=L.list.insert(i,
6、x)Insertanitematagivenposition.Thefirstargumentistheindexoftheelementbeforewhichtoinsert,soa.insert(0,x)insertsatthefrontofthelist,anda.insert(len(a),x)isequivalenttoa.append(x).list.remove(x)Removethefirstitemfromthelistwhosevalueisx.Itisanerrorifthereisnosuchitem.list.p
7、op([i])Removetheitematthegivenpositioninthelist,andreturnit.Ifnoindexisspecified,a.pop()removesandreturnsthelastiteminthelist.(Thesquarebracketsaroundtheiinthemethodsignaturedenotethattheparameterisoptional,notthatyoushouldtypesquarebracketsatthatposition.Youwillseethisno
8、tationfrequentlyinthePythonLibraryReference.)list.index(x)Returntheindexinthelistofthefirstitemwhosevalueisx.Itisanerrorifthereisnosuchitem.list.count(x)Returnthenumberoftimesxappearsinthelist.list.sort()Sorttheitemsofthelist,inplace.list.reverse()Reversetheelementsofthel
9、ist,inplace.Anexamplethatusesmostofthelistmethods:如下的例子用到了上述的大部分方法:a=[66.25,333,333,1,1234.5]printa.count(333),a.count(66.25),a.count('x')210a.insert(2,-1)a.append(333)a[66.25,333,-1,333,1,1234.5,333]a.index(333)1a.remove(333)a[66.25,-1,333,1,1234.5,333]a.reverse()a[333,1
10、234.5,1,333,-1,66.25]a.sort()a[-1,1,66.25,333,333,1234.5]5.1.1.UsingListsasStacks将list作为堆栈使用?Thelistmethodsmakeitveryeasytousealistasastack,wherethelastelementaddedisthefirstelementretrieved("last-in,first-out").Toaddanitemtothetopofthestack,useappend().Toretrieveanitemfr
11、omthetopofthestack,usepop()withoutanexplicitindex.Forexample:list的方法可以让List很轻易的变成一个堆栈来使用,而堆栈的特点就