⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node7.html

📁 适合python初学者,一本很好的python学习书籍
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//CN"><html><head><link rel="STYLESHEET" href="style.css" type='text/css' /><link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /><link rel='start' href='../index.html' title='Python Documentation Index' /><link rel="first" href="tut.html" title='Python Tutorial' /><link rel='contents' href='node2.html' title="Contents" /><link rel='last' href='about.html' title='About this document...' /><link rel='help' href='about.html' title='About this document...' /><link rel="next" href="node8.html" /><link rel="prev" href="node6.html" /><link rel="parent" href="tut.html" /><link rel="next" href="node8.html" /><meta name='aesop' content='information' /><title>5. 数据结构 Data Structures </title></head><body><DIV CLASS="navigation"><div id='top-navigation-panel' xml:id='top-navigation-panel'><table align="center" width="100%" cellpadding="0" cellspacing="2"><tr><td class='online-navigation'><a rel="prev" title="4. 深入流程控制 More Control"  href="node6.html"><img src='../icons/previous.png'  border='0' height='32'  alt='Previous Page' width='32' /></A></td><td class='online-navigation'><a rel="parent" title="Python Tutorial"  href="tut.html"><img src='../icons/up.png'  border='0' height='32'  alt='Up One Level' width='32' /></A></td><td class='online-navigation'><a rel="next" title="6. 模块 Modules"  href="node8.html"><img src='../icons/next.png'  border='0' height='32'  alt='Next Page' width='32' /></A></td><td align="center" width="100%" class='online-navigation'>Python Tutorial</td><td class='online-navigation'><a rel="contents" title="Table of Contents"  href="node2.html"><img src='../icons/contents.png'  border='0' height='32'  alt='Contents' width='32' /></A></td><td class='online-navigation'><img src='../icons/blank.png'  border='0' height='32'  alt='' width='32' /></td><td class='online-navigation'><img src='../icons/blank.png'  border='0' height='32'  alt='' width='32' /></td></tr></table><div class='online-navigation'><b class="navlabel">Previous:</b><a class="sectref" rel="prev" href="node6.html">4. 深入流程控制 More Control</A><b class="navlabel">Up:</b><a class="sectref" rel="parent" href="tut.html">Python Tutorial</A><b class="navlabel">Next:</b><a class="sectref" rel="next" href="node8.html">6. 模块 Modules</A></div><hr /></div></DIV><!--End of Navigation Panel--><div class='online-navigation'><!--Table of Child-Links--><A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a><UL CLASS="ChildLinks"><LI><A href="node7.html#SECTION007100000000000000000">5.1 深入链表 More on Lists</a><UL><LI><A href="node7.html#SECTION007110000000000000000">5.1.1 把链表当作堆栈使用 Using Lists as Stacks</a><LI><A href="node7.html#SECTION007120000000000000000">5.1.2 把链表当作队列使用 Using Lists as Queues</a><LI><A href="node7.html#SECTION007130000000000000000">5.1.3 函数化编程工具 Functional Programming Tools</a><LI><A href="node7.html#SECTION007140000000000000000">5.1.4 链表推导式 List Comprehensions</a></ul><LI><A href="node7.html#SECTION007200000000000000000">5.2 <tt class="keyword">del</tt> 语句</a><LI><A href="node7.html#SECTION007300000000000000000">5.3 元组(Tuples)和序列(Sequences )Tuples and Sequences</a><LI><A href="node7.html#SECTION007400000000000000000">5.4 Dictionaries 字典</a><LI><A href="node7.html#SECTION007500000000000000000">5.5 循环技巧 Looping Techniques</a><LI><A href="node7.html#SECTION007600000000000000000">5.6 深入条件控制 More on Conditions</a><LI><A href="node7.html#SECTION007700000000000000000">5.7 比较序列和其它类型 Comparing Sequences and Other Types</a></ul><!--End of Table of Child-Links--></div><HR><H1><A NAME="SECTION007000000000000000000"></A><A NAME="structures"></A><BR>5. 数据结构 Data Structures </H1><P>This chapter describes some things you've learned about already in
more detail, and adds some new things as well.<P>本章节深入讲述一些你已经学习过的东西,并且还加入了新的内容。<P><H1><A NAME="SECTION007100000000000000000"></A><A NAME="moreLists"></A><BR>5.1 深入链表 More on Lists </H1><P>The list data type has some more methods.  Here are all of the methods
of list objects:<P>链表类型有很多方法,这里是链表类型的所有方法:<P><dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">  <td><nobr><b><tt id='l2h-15' xml:id='l2h-15' class="method">append</tt></b>(</nobr></td>  <td><var>x</var>)</td></tr></table></dt><dd>
Add an item to the end of the list;
equivalent to <code>a[len(a):] = [<var>x</var>]</code>.<P>把一个元素添加到链表的结尾,相当于 <code>a[len(a):] = [x]</code>
</dl><P><dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">  <td><nobr><b><tt id='l2h-16' xml:id='l2h-16' class="method">extend</tt></b>(</nobr></td>  <td><var>L</var>)</td></tr></table></dt><dd>
Extend the list by appending all the items in the given list;
equivalent to <code>a[len(a):] = <var>L</var></code>.<P>通过添加指定链表的所有元素来扩充链表,相当于 <code>a[len(a):] = L</code>。
</dl><P><dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">  <td><nobr><b><tt id='l2h-17' xml:id='l2h-17' class="method">insert</tt></b>(</nobr></td>  <td><var>i, x</var>)</td></tr></table></dt><dd>
Insert an item at a given position.  The first argument is the index
of the element before which to insert, so <code>a.insert(0, <var>x</var>)</code>
inserts at the front of the list, and <code>a.insert(len(a), <var>x</var>)</code>
is equivalent to <code>a.append(<var>x</var>)</code>.<P>在指定位置插入一个元素。第一个参数是准备插入到其前面的那个元素的索引,例如<code>a.insert(0, x)</code> 会插入到整个链表之前,而<code>a.insert(len(a), x)</code> 相当于 <code>a.append(x)</code>。
</dl><P><dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">  <td><nobr><b><tt id='l2h-18' xml:id='l2h-18' class="method">remove</tt></b>(</nobr></td>  <td><var>x</var>)</td></tr></table></dt><dd>
Remove the first item from the list whose value is <var>x</var>.
It is an error if there is no such item.<P>删除链表中值为<var>x</var>的第一个元素。如果没有这样的元素,就会返回一个错误。
</dl><P><dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">  <td><nobr><b><tt id='l2h-19' xml:id='l2h-19' class="method">pop</tt></b>(</nobr></td>  <td><var></var><big>[</big><var>i</var><big>]</big><var></var>)</td></tr></table></dt><dd>
Remove the item at the given position in the list, and return it.  If
no index is specified, <code>a.pop()</code> returns the last item in the
list.  The item is also removed from the list.  (The square brackets
around the <var>i</var> in the method signature denote that the parameter
is optional, not that you should type square brackets at that
position.  You will see this notation frequently in the
<em class="citetitle"><a href="../lib/lib.html" title="Python Library Reference" >Python Library Reference</a></em>.)<P>从链表的指定位置删除元素,并将其返回。如果没有指定索引,<code>a.pop()</code>返回最后一个元素。元素随即从链表中被删除。(方法中<var>i</var>两边的方括号表示这个参数是可选的,而不是要求你输入一对方括号,你会经常在<em class="citetitle"><a href="../lib/lib.html" title="Python 库参考手册" >Python 库参考手册</a></em>中遇到这样的标记。)
</dl><P><dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">  <td><nobr><b><tt id='l2h-20' xml:id='l2h-20' class="method">index</tt></b>(</nobr></td>  <td><var>x</var>)</td></tr></table></dt><dd>
Return the index in the list of the first item whose value is <var>x</var>.
It is an error if there is no such item.<P>返回链表中第一个值为<var>x</var>的元素的索引。如果没有匹配的元素就会返回一个错误。
</dl><P><dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">  <td><nobr><b><tt id='l2h-21' xml:id='l2h-21' class="method">count</tt></b>(</nobr></td>  <td><var>x</var>)</td></tr></table></dt><dd>
Return the number of times <var>x</var> appears in the list.<P>返回<var>x</var>在链表中出现的次数。
</dl><P><dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">  <td><nobr><b><tt id='l2h-22' xml:id='l2h-22' class="method">sort</tt></b>(</nobr></td>  <td><var></var>)</td></tr></table></dt><dd>
Sort the items of the list, in place.<P>对链表中的元素进行适当的排序。
</dl><P><dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">  <td><nobr><b><tt id='l2h-23' xml:id='l2h-23' class="method">reverse</tt></b>(</nobr></td>  <td><var></var>)</td></tr></table></dt><dd>
Reverse the elements of the list, in place.<P>倒排链表中的元素。
</dl><P>An example that uses most of the list methods:<P>下面这个示例演示了链表的大部分方法:<P><div class="verbatim"><pre>
&gt;&gt;&gt; a = [66.6, 333, 333, 1, 1234.5]
&gt;&gt;&gt; print a.count(333), a.count(66.6), a.count('x')
2 1 0
&gt;&gt;&gt; a.insert(2, -1)
&gt;&gt;&gt; a.append(333)
&gt;&gt;&gt; a
[66.6, 333, -1, 333, 1, 1234.5, 333]
&gt;&gt;&gt; a.index(333)
1
&gt;&gt;&gt; a.remove(333)
&gt;&gt;&gt; a
[66.6, -1, 333, 1, 1234.5, 333]
&gt;&gt;&gt; a.reverse()
&gt;&gt;&gt; a
[333, 1234.5, 1, 333, -1, 66.6]
&gt;&gt;&gt; a.sort()
&gt;&gt;&gt; a
[-1, 1, 66.6, 333, 333, 1234.5]</pre></div><P><H2><A NAME="SECTION007110000000000000000"></A><A NAME="lists-as-stacks"></A><BR>5.1.1 把链表当作堆栈使用 Using Lists as Stacks </H2><P>The list methods make it very easy to use a list as a stack, where the
last element added is the first element retrieved (``last-in,
first-out'').  To add an item to the top of the stack, use
<tt class="method">append()</tt>.  To retrieve an item from the top of the stack, use
<tt class="method">pop()</tt> without an explicit index.  For example:<P>链表方法使得链表可以很方便的做为一个堆栈来使用,堆栈作为特定的数据结构,最先进入的元素最后一个被释放(后进先出)。用<tt class="method">append()</tt> 方法可以把一个元素添加到堆栈顶。用不指定索引的<tt class="method">pop()</tt> 方法可以把一个元素从堆栈顶释放出来。例如:<P><div class="verbatim"><pre>
&gt;&gt;&gt; stack = [3, 4, 5]
&gt;&gt;&gt; stack.append(6)
&gt;&gt;&gt; stack.append(7)
&gt;&gt;&gt; stack
[3, 4, 5, 6, 7]
&gt;&gt;&gt; stack.pop()
7
&gt;&gt;&gt; stack
[3, 4, 5, 6]
&gt;&gt;&gt; stack.pop()
6
&gt;&gt;&gt; stack.pop()
5
&gt;&gt;&gt; stack
[3, 4]</pre></div><P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -