编程指南中文版数据结构

上传人:鲁** 文档编号:508033704 上传时间:2023-02-13 格式:DOC 页数:15 大小:104KB
返回 下载 相关 举报
编程指南中文版数据结构_第1页
第1页 / 共15页
编程指南中文版数据结构_第2页
第2页 / 共15页
编程指南中文版数据结构_第3页
第3页 / 共15页
编程指南中文版数据结构_第4页
第4页 / 共15页
编程指南中文版数据结构_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《编程指南中文版数据结构》由会员分享,可在线阅读,更多相关《编程指南中文版数据结构(15页珍藏版)》请在金锄头文库上搜索。

1、v2 6编程指南中文版数据结构Navigati on in dexmodules |n ext|previous|Pytho n v2.6.5 docume ntati on? The Pyth on Tutorial?5.Data Structures数据结构?This chapter describes some things youve lear ned about already in more detail,a nd adds some new thi ngs as well.本章将更加详细的介绍一些你已经了解的东西,另外我们还会添加一些新的东西。5.1. More on Lists

2、 List 的更多细节?The list data type has some more methods.Here are all of the methods of list objects :List数据类型还有其他的一些方法。如下是 List的所有方法:list.appe nd(x)Add an item to the end of the list; equivale nt toalen(a) : =x.list.extend(L)Exte nd the list by appe nding all the items in the give n list; equivale ntto

3、 alen(a) : =L.list.insert(i,x)Insert an item at agiven position.The first argument is the index of the eleme nt before which to in sert,so a.i nsert(0,x)i nserts at the front of the list,a nd a.i nsert(le n(a),x)is equivale nt to a.appe nd(x ).l ist.remove(x)Remove the first item from the list whose

4、 value is x.It is an error ifthere is no such item.list.pop(i)Remove the item at the given position in the list,and return it.If no index is specified,a.pop()removes and returns the last item in the list.(The square brackets around the iin the method sig nature denote that the parameter is optio nal

5、, not that you should type square brackets at that positi on.You will see this no tati on freque ntly in the Pyth on Library Refere nce.)list.i ndex(x)Return the index in the list of the first item whose value is x.lt is an error if there is no such item.list.count(x)Return the number of times xappe

6、ars in the list.list.sort()Sort the items of the list,in place.list.reverse()Reverse the elements of the list,in place.An example that uses most of the list methods :如下的例子用到了上述的大部分方法:a=66.25,333,333,1,1234.5pri nta.co un t(333),a.cou nt(66.25),a.cou nt(x)2 10 a.i nsert(2,-1)a. appe nd(333)a66.25,333

7、,-1,333,1,1234.5,333a.i ndex(333)1 a.remove(333)a66.25,-1,333,1,1234.5,333a.reverse()a333,1234.5,1,333,- 1,66.25a.sort()a-1,1,66.25,333,333,1234.55.1.1. Using Lists as Stacks将 list 作为堆栈使用?The list methods make it very easy to use alist as astack,where the last element added is the first element retr

8、ieved(last-in,first-out).To add an item to the top of the stack,use append().To retrieve an item from the top of the stack,use pop()without an explicit in dex.For example:list的方法可以让List很轻易的变成一个堆栈来使用,而堆栈的特点就是最后一 个被添加的数字最先被返回(last-in,first-out)。在堆栈的顶部添加一个元素,使用append()方法,返回堆栈顶部的元素,使用没有显式指定索引的pop()方法:st

9、ack=3,4,5stack. appe nd(6)stack. appe nd(7)stack3,4,5,6,7stack.pop()7 stack3,4,5,6stack.pop()6 stack.pop()5 stack3,45.1.2. Using Lists as Queues将 List 当做队列?It is also possible to use alist as aqueue,where the first element addedis the first element retrieved(first-in,first-out); however,lists arenot

10、 efficie nt for this purpose.While appe nds and pops from the end of list are fast,doing inserts or pops from the beginning of alist is slow(because all of the other eleme nts have to be shifted by on e).同样也可以将List作为队列使用,队列的特点是最先添加的数据最先返回(first-in,first-out);不过,List对此来说并不高效。将一个元素从尾部添加或者取出非常快,但是在List

11、的顶部添加或者取出就会变得较慢了 (因为其他的 元素都将产生移位)。To impleme nt aqueue,use collecti on s.deque which was desig ned to havefast appe nds and pops from both en ds.For example:可以用collections.deque去实现一个队列,它恰恰是为了快速从两端添加或者取出数据的。比如:from collecti ons import dequequeue=deque(Eric,Joh n,Michae门)queue.appe nd(Terry)#Terry arr

12、ives queue.appe nd(Graham)#Graham arrives queue.popleft()#The first to arrive now leavesEricqueue.popleft()#The sec ond to arrive now leavesJoh nq ueue#Rema ining queue in order of arrival deque(Michael,Terry,Graham)5.1.3. Functional Programming Tools功能性编程工具?There are three built-in functions that a

13、re very useful whe n used with lists : filter(),map(),and reduce().有三个内置的函数在同list 一起使用的时候非常有用:filter(),map(),a nd reduce()。filter(function,sequence)returins asequence consisting of those items from the seque nce for which fun cti on( item)is true .If seque nce is astri ng or tuple,the result will be

14、 of the same type; otherwise,it is alwaysalist.For example,to compute some primes :filter(function,sequence)返回序列中function(item) 测试为真的所有元素的列表。如果sequence是一个string或者tuple,会返回和它一样的类型,否则返回 一个 list 。def f(x) : return x%2 ! =0 and x%3!=0.filter(f,ra nge(2,25)5,7,11,13,17,19,23m ap(fu nctio n,seque nce)call

15、s fun cti on( item)for each of the seque nees items and retur ns alist of the retur n values.For example,to compute some cubes :map(fun ctio n,seque nce) 对队列中的每个元素调用 fun ctio n(item)函数,并且返回函数所有返回值的列表。比如,计算一些立方数:def cube(x) : returnx*x*x.m ap(cube,ra nge(1,11)1,8,27,64,125,216,343,512,729,1000More th

16、a n one sequenee may be passed ; the function must then have as many argume nts as there are seque nces and is called with the corresp onding item from each seque nce(or None if some seque nee is shorter tha n ano ther).For example:可以传入多个队列;不过函数必须也要有和序列数(or No ne if some seque nee isshorter than ano

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号