This file is indexed.

/usr/share/doc/diveintopython-zh/html/native_data_types/lists.html is in diveintopython-zh 5.4b-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   
      <title>3.2.&nbsp;List 介绍</title>
      <link rel="stylesheet" href="../diveintopython.css" type="text/css">
      <link rev="made" href="mailto:f8dy@diveintopython.org">
      <meta name="generator" content="DocBook XSL Stylesheets V1.52.2">
      <meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free">
      <meta name="description" content="Python from novice to pro">
      <link rel="home" href="../toc/index.html" title="Dive Into Python">
      <link rel="up" href="index.html" title="第&nbsp;3&nbsp;章&nbsp;内置数据类型">
      <link rel="previous" href="index.html" title="第&nbsp;3&nbsp;章&nbsp;内置数据类型">
      <link rel="next" href="tuples.html" title="3.3.&nbsp;Tuple 介绍">
   </head>
   <body>
      <table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td id="breadcrumb" colspan="5" align="left" valign="top">导航:<a href="../index.html">起始页</a>&nbsp;&gt;&nbsp;<a href="../toc/index.html">Dive Into Python</a>&nbsp;&gt;&nbsp;<a href="index.html">内置数据类型</a>&nbsp;&gt;&nbsp;<span class="thispage">List 介绍</span></td>
            <td id="navigation" align="right" valign="top">&nbsp;&nbsp;&nbsp;<a href="index.html" title="上一页: “内置数据类型”">&lt;&lt;</a>&nbsp;&nbsp;&nbsp;<a href="tuples.html" title="下一页: “Tuple 介绍”">&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3" id="logocontainer">
               <h1 id="logo"><a href="../index.html" accesskey="1">深入 Python :Dive Into Python 中文版</a></h1>
               <p id="tagline">Python 从新手到专家 [Dip_5.4b_CPyUG_Release]</p>
            </td>
            <td colspan="3" align="right">
               <form id="search" method="GET" action="http://www.google.com/custom">
                  <p><label for="q" accesskey="4">Find:&nbsp;</label><input type="text" id="q" name="q" size="20" maxlength="255" value=""> <input type="submit" value="搜索"><input type="hidden" name="domains" value="woodpecker.org.cn"><input type="hidden" name="sitesearch" value="www.woodpecker.org.cn/diveintopython"></p>
               </form>
            </td>
         </tr>
      </table>
      <!--#include virtual="/inc/ads" -->
      <div class="section" lang="zh_cn">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title"><a name="odbchelper.list"></a>3.2.&nbsp;List 介绍
                  </h2>
               </div>
            </div>
            <div></div>
         </div>
         <div class="toc">
            <ul>
               <li><span class="section"><a href="lists.html#d0e5747">3.2.1. List 的定义</a></span></li>
               <li><span class="section"><a href="lists.html#d0e6014">3.2.2. 向 list 中增加元素</a></span></li>
               <li><span class="section"><a href="lists.html#d0e6242">3.2.3. 在 list 中搜索</a></span></li>
               <li><span class="section"><a href="lists.html#d0e6404">3.2.4. 从 list 中删除元素</a></span></li>
               <li><span class="section"><a href="lists.html#d0e6522">3.2.5. 使用 list 的运算符</a></span></li>
            </ul>
         </div>
         <div class="abstract">
            <p>List 是 <span class="application">Python</span> 中使用最频繁的数据类型。如果您对 list 仅有的经验就是在 <span class="application">Visual Basic</span> 中的数组或 <span class="application">Powerbuilder</span> 中的数据存储,那么就打起精神学习 <span class="application">Python</span> 的 list 吧。
            </p>
         </div><a name="compare.list.perl"></a><table class="note" border="0" summary="">
            <tr>
               <td rowspan="2" align="center" valign="top" width="1%"><img src="../images/note.png" alt="注意" title="" width="24" height="24"></td>
            </tr>
            <tr>
               <td colspan="2" align="left" valign="top" width="99%"><span class="application">Python</span> 的 list 如同 <span class="application">Perl</span> 中的数组。在 <span class="application">Perl</span> 中,用来保存数组的变量总是以 <tt class="literal">@</tt> 字符开始;在 <span class="application">Python</span> 中,变量可以任意取名,并且 <span class="application">Python</span> 在内部会记录下其数据类型。
               </td>
            </tr>
         </table><a name="compare.list.java"></a><table class="note" border="0" summary="">
            <tr>
               <td rowspan="2" align="center" valign="top" width="1%"><img src="../images/note.png" alt="注意" title="" width="24" height="24"></td>
            </tr>
            <tr>
               <td colspan="2" align="left" valign="top" width="99%"><span class="application">Python</span> 中的 list 更像 <span class="application">Java</span> 中的数组 (您可以简单地这样理解,但 <span class="application">Python</span> 中的 list 远比 <span class="application">Java</span> 中的数组强大)。一个更好的类比是 <tt class="classname">ArrayList</tt> 类,它可以保存任意对象,并且可以在增加新元素时动态扩展。
               </td>
            </tr>
         </table>
         <div class="section" lang="zh_cn">
            <div class="titlepage">
               <div>
                  <div>
                     <h3 class="title"><a name="d0e5747"></a>3.2.1.&nbsp;List 的定义
                     </h3>
                  </div>
               </div>
               <div></div>
            </div>
            <div class="example"><a name="d0e5750"></a><h3 class="title">&nbsp;3.6.&nbsp;定义 List</h3><pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li = [<span class='pystring'>"a"</span>, <span class='pystring'>"b"</span>, <span class='pystring'>"mpilgrim"</span>, <span class='pystring'>"z"</span>, <span class='pystring'>"example"</span>]</span> <a name="odbchelper.list.1.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'mpilgrim', 'z', 'example']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li[0]</span>                                       <a name="odbchelper.list.1.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="computeroutput">'a'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li[4]</span>                                       <a name="odbchelper.list.1.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<span class="computeroutput">'example'</span></pre><div class="calloutlist">
                  <table border="0" summary="Callout list">
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.1.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">首先我们定义了一个有 5 个元素的 list。注意它们保持着初始的顺序。这不是偶然。List 是一个用方括号包括起来的有序元素的集合。</td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.1.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">List 可以作为以 0 下标开始的数组。任何一个非空 list 的第一个元素总是 <tt class="literal">li[0]</tt></td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.1.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">这个包含 5 个元素 list 的最后一个元素是 <tt class="literal">li[4]</tt>,因为列表总是从 0 开始。
                        </td>
                     </tr>
                  </table>
               </div>
            </div>
            <div class="example"><a name="odbchelper.negative.example"></a><h3 class="title">&nbsp;3.7.&nbsp;负的 list 索引</h3><pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'mpilgrim', 'z', 'example']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li[-1]</span> <a name="odbchelper.list.2.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<span class="computeroutput">'example'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li[-3]</span> <a name="odbchelper.list.2.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="computeroutput">'mpilgrim'</span></pre><div class="calloutlist">
                  <table border="0" summary="Callout list">
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.2.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">负数索引从 list 的尾部开始向前计数来存取元素。任何一个非空的 list 最后一个元素总是 <tt class="literal">li[-1]</tt></td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.2.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">如果负数索引使您感到糊涂,可以这样理解:<tt class="literal">li[-n] == li[len(li) - n]</tt>。所以在这个 list 里,<tt class="literal">li[-3] == li[5 - 3] == li[2]</tt></td>
                     </tr>
                  </table>
               </div>
            </div>
            <div class="example"><a name="odbchelper.list.slice"></a><h3 class="title">&nbsp;3.8.&nbsp;list 的分片 (slice)</h3><pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'mpilgrim', 'z', 'example']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li[1:3]</span>  <a name="odbchelper.list.3.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<span class="computeroutput">['b', 'mpilgrim']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li[1:-1]</span> <a name="odbchelper.list.3.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="computeroutput">['b', 'mpilgrim', 'z']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li[0:3]</span>  <a name="odbchelper.list.3.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<span class="computeroutput">['a', 'b', 'mpilgrim']</span></pre><div class="calloutlist">
                  <table border="0" summary="Callout list">
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.3.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">您可以通过指定 2 个索引得到 list 的子集,叫做一个 “<span class="quote">slice</span>” 。返回值是一个新的 list,它包含了 list 中按顺序从第一个 slice 索引 (这里为 <tt class="literal">li[1]</tt>) 开始,直到但是不包括第二个 slice 索引 (这里为 <tt class="literal">li[3]</tt>) 的所有元素。
                        </td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.3.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">如果一个或两个 slice 索引是负数,slice 也可以工作。如果对您有帮助,您可以这样理解:从左向右阅读 list,第一个 slice 索引指定了您想要的第一个元素,第二个 slice 索引指定了第一个您不想要的元素。返回的值为在其间的每个元素。</td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.3.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">List 从 0 开始,所以 <tt class="literal">li[0:3]</tt> 返回 list 的前 3 个元素,从 <tt class="literal">li[0]</tt> 开始,直到但不包括 <tt class="literal">li[3]</tt></td>
                     </tr>
                  </table>
               </div>
            </div>
            <div class="example"><a name="odbchelper.list.slicing.example"></a><h3 class="title">&nbsp;3.9.&nbsp;Slice 简写</h3><pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'mpilgrim', 'z', 'example']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li[:3]</span> <a name="odbchelper.list.4.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<span class="computeroutput">['a', 'b', 'mpilgrim']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li[3:]</span> <a name="odbchelper.list.4.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"> <a name="odbchelper.list.4.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<span class="computeroutput">['z', 'example']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li[:]</span>  <a name="odbchelper.list.4.4"></a><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12">
<span class="computeroutput">['a', 'b', 'mpilgrim', 'z', 'example']</span></pre><div class="calloutlist">
                  <table border="0" summary="Callout list">
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.4.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">如果左侧分片索引为 0,您可以将其省略,默认为 0。所以 <tt class="literal">li[:3]</tt><a href="lists.html#odbchelper.list.slice" title="例&nbsp;3.8.&nbsp;list 的分片 (slice)">&nbsp;3.8 “list 的分片 (slice)”</a><tt class="literal">li[0:3]</tt> 是一样的。
                        </td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.4.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">同样的,如果右侧分片索引是 list 的长度,可以将其省略。所以 <tt class="literal">li[3:]</tt><tt class="literal">li[3:5]</tt> 是一样的,因为这个 list 有 5 个元素。
                        </td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.4.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">请注意这里的对称性。在这个包含 5 个元素的 list 中,<tt class="literal">li[:3]</tt> 返回前 3 个元素,而 <tt class="literal">li[3:]</tt> 返回后 2 个元素。实际上,<tt class="literal">li[:n]</tt> 总是返回前 <tt class="literal">n</tt> 个元素,而 <tt class="literal">li[n:]</tt> 将返回剩下的元素,不管 list 有多长。
                        </td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.4.4"><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">如果将两个分片索引全部省略,这将包括 list 的所有元素。但是与原始的名为 <tt class="varname">li</tt> 的 list 不同,它是一个新 list,恰好拥有与 <tt class="varname">li</tt> 一样的全部元素。<tt class="literal">li[:]</tt> 是生成一个 list 完全拷贝的一个简写。
                        </td>
                     </tr>
                  </table>
               </div>
            </div>
         </div>
         <div class="section" lang="zh_cn">
            <div class="titlepage">
               <div>
                  <div>
                     <h3 class="title"><a name="d0e6014"></a>3.2.2.&nbsp;向 list 中增加元素
                     </h3>
                  </div>
               </div>
               <div></div>
            </div>
            <div class="example"><a name="d0e6017"></a><h3 class="title">&nbsp;3.10.&nbsp;向 list 中增加元素</h3><pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'mpilgrim', 'z', 'example']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li.append(<span class='pystring'>"new"</span>)</span>               <a name="odbchelper.list.5.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'mpilgrim', 'z', 'example', 'new']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li.insert(2, <span class='pystring'>"new"</span>)</span>            <a name="odbchelper.list.5.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li.extend([<span class='pystring'>"two"</span>, <span class='pystring'>"elements"</span>])</span> <a name="odbchelper.list.5.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']</span></pre><div class="calloutlist">
                  <table border="0" summary="Callout list">
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.5.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left"><tt class="function">append</tt> 向 list 的末尾追加单个元素。
                        </td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.5.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left"><tt class="function">insert</tt> 将单个元素插入到 list 中。数值参数是插入点的索引。请注意,list 中的元素不必唯一,现在有两个独立的元素具有 <tt class="literal">'new'</tt> 这个值,<tt class="literal">li[2]</tt><tt class="literal">li[6]</tt></td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.5.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left"><tt class="function">extend</tt> 用来连接 list。请注意不要使用多个参数来调用 <tt class="function">extend</tt>,要使用一个 list 参数进行调用。在本例中,这个 list 有两个元素。
                        </td>
                     </tr>
                  </table>
               </div>
            </div>
            <div class="example"><a name="odbchelper.list.append.vs.extend"></a><h3 class="title">&nbsp;3.11.&nbsp;<tt class="function">extend</tt> (扩展) 与 <tt class="function">append</tt> (追加) 的差别
               </h3><pre class="screen">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li = [<span class='pystring'>'a'</span>, <span class='pystring'>'b'</span>, <span class='pystring'>'c'</span>]</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li.extend([<span class='pystring'>'d'</span>, <span class='pystring'>'e'</span>, <span class='pystring'>'f'</span>])</span> <a name="odbchelper.list.5.4"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'c', 'd', 'e', 'f']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">len(li)</span>                    <a name="odbchelper.list.5.5"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="computeroutput">6</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li[-1]</span>
<span class="computeroutput">'f'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li = [<span class='pystring'>'a'</span>, <span class='pystring'>'b'</span>, <span class='pystring'>'c'</span>]</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li.append([<span class='pystring'>'d'</span>, <span class='pystring'>'e'</span>, <span class='pystring'>'f'</span>])</span> <a name="odbchelper.list.5.6"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'c', ['d', 'e', 'f']]</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">len(li)</span>                    <a name="odbchelper.list.5.7"></a><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12">
<span class="computeroutput">4</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li[-1]</span>
<span class="computeroutput">['d', 'e', 'f']</span>
</pre><div class="calloutlist">
                  <table border="0" summary="Callout list">
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.5.4"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">Lists 的两个方法 <tt class="function">extend</tt><tt class="function">append</tt> 看起来类似,但实际上完全不同。<tt class="function">extend</tt> 接受一个参数,这个参数总是一个 list,并且把这个 list 中的每个元素添加到原 list 中。
                        </td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.5.5"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">在这里 list 中有 3 个元素 (<tt class="literal">'a'</tt><tt class="literal">'b'</tt><tt class="literal">'c'</tt>),并且使用另一个有 3 个元素 (<tt class="literal">'d'</tt><tt class="literal">'e'</tt><tt class="literal">'f'</tt>) 的 list 扩展之,因此新的 list 中有 6 个元素。
                        </td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.5.6"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">另一方面,<tt class="function">append</tt> 接受一个参数,这个参数可以是任何数据类型,并且简单地追加到 list 的尾部。在这里使用一个含有 3 个元素的 list 参数调用 <tt class="function">append</tt> 方法。
                        </td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.5.7"><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">原来包含 3 个元素的 list 现在包含 4 个元素。为什么是 4 个元素呢?因为刚刚追加的最后一个元素<span class="emphasis"><em>本身是个 list</em></span>。List 可以包含任何类型的数据,也包括其他的 list。这或许是您所要的结果,或许不是。如果您的意图是 <tt class="function">extend</tt>,请不要使用 <tt class="function">append</tt></td>
                     </tr>
                  </table>
               </div>
            </div>
         </div>
         <div class="section" lang="zh_cn">
            <div class="titlepage">
               <div>
                  <div>
                     <h3 class="title"><a name="d0e6242"></a>3.2.3.&nbsp;在 list 中搜索
                     </h3>
                  </div>
               </div>
               <div></div>
            </div>
            <div class="example"><a name="odbchelper.list.search"></a><h3 class="title">&nbsp;3.12.&nbsp;搜索 list</h3><pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li.index(<span class='pystring'>"example"</span>)</span> <a name="odbchelper.list.6.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<span class="computeroutput">5</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li.index(<span class='pystring'>"new"</span>)</span>     <a name="odbchelper.list.6.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="computeroutput">2</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li.index(<span class='pystring'>"c"</span>)</span>       <a name="odbchelper.list.6.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<span class="traceback">Traceback (innermost last):
  File "&lt;interactive input&gt;", line 1, in ?
ValueError: list.index(x): x not in list</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><span class='pystring'>"c"</span> <span class='pykeyword'>in</span> li</span>           <a name="odbchelper.list.6.4"></a><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12">
<span class="computeroutput">False</span></pre><div class="calloutlist">
                  <table border="0" summary="Callout list">
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.6.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left"><tt class="function">index</tt> 在 list 中查找一个值的首次出现并返回索引值。
                        </td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.6.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left"><tt class="function">index</tt> 在 list 中查找一个值的<span class="emphasis"><em>首次</em></span> 出现。这里 <tt class="literal">'new'</tt> 在 list 中出现了两次,在 <tt class="literal">li[2]</tt><tt class="literal">li[6]</tt>,但 <tt class="function">index</tt> 只返回第一个索引,<tt class="literal">2</tt></td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.6.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">如果在 list 中没有找到值,<span class="application">Python</span> 会引发一个异常。这一点与大部分的语言截然不同,大部分语言会返回某个无效索引。尽管这种处理可能令人讨厌,但它仍然是件好事,因为它说明您的程序会由于源代码的问题而崩溃,好于在后面当您使用无效索引而引起崩溃。
                        </td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.6.4"><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">要测试一个值是否在 list 内,使用 <tt class="function">in</tt>。如果值存在,它返回 <tt class="constant">True</tt>,否则返为 <tt class="constant">False</tt></td>
                     </tr>
                  </table>
               </div>
            </div><a name="tip.boolean"></a><table class="note" border="0" summary="">
               <tr>
                  <td rowspan="2" align="center" valign="top" width="1%"><img src="../images/note.png" alt="注意" title="" width="24" height="24"></td>
               </tr>
               <tr>
                  <td colspan="2" align="left" valign="top" width="99%">在 2.2.1 版本之前,<span class="application">Python</span> 没有单独的布尔数据类型。为了弥补这个缺陷,<span class="application">Python</span> 在布尔环境 (如 <tt class="literal">if</tt> 语句) 中几乎接受所有东西,遵循下面的规则:
                     <div class="itemizedlist">
                        <ul>
                           <li><tt class="constant">0</tt> 为 false; 其它所有数值皆为 true。
                           </li>
                           <li>空串 (<tt class="literal">""</tt>) 为 false; 其它所有字符串皆为 true。
                           </li>
                           <li>空 list (<tt class="literal">[]</tt>) 为 false; 其它所有 list 皆为 true。
                           </li>
                           <li>空 tuple (<tt class="literal">()</tt>) 为 false; 其它所有 tuple 皆为 true。
                           </li>
                           <li>空 dictionary (<tt class="literal">{}</tt>) 为 false; 其它所有 dictionary 皆为 true。
                           </li>
                        </ul>
                     </div>这些规则仍然适用于 <span class="application">Python</span> 2.2.1 及其后续版本,但现在您也可以使用真正的布尔值,它的值或者为 <tt class="literal">True</tt> 或者为 <tt class="literal">False</tt>。请注意第一个字母是大写的;这些值如同在 <span class="application">Python</span> 中的其它东西一样都是大小写敏感的。
                  </td>
               </tr>
            </table>
         </div>
         <div class="section" lang="zh_cn">
            <div class="titlepage">
               <div>
                  <div>
                     <h3 class="title"><a name="d0e6404"></a>3.2.4.&nbsp;从 list 中删除元素
                     </h3>
                  </div>
               </div>
               <div></div>
            </div>
            <div class="example"><a name="odbchelper.list.removingelements"></a><h3 class="title">&nbsp;3.13.&nbsp;从 list 中删除元素</h3><pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li.remove(<span class='pystring'>"z"</span>)</span>   <a name="odbchelper.list.7.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'new', 'mpilgrim', 'example', 'new', 'two', 'elements']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li.remove(<span class='pystring'>"new"</span>)</span> <a name="odbchelper.list.7.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'mpilgrim', 'example', 'new', 'two', 'elements']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li.remove(<span class='pystring'>"c"</span>)</span>   <a name="odbchelper.list.7.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<span class="traceback">Traceback (innermost last):
  File "&lt;interactive input&gt;", line 1, in ?
ValueError: list.remove(x): x not in list</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li.pop()</span>         <a name="odbchelper.list.7.4"></a><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12">
<span class="computeroutput">'elements'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'mpilgrim', 'example', 'new', 'two']</span></pre><div class="calloutlist">
                  <table border="0" summary="Callout list">
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.7.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left"><tt class="function">remove</tt> 从 list 中删除一个值的首次出现。
                        </td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.7.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left"><tt class="function">remove</tt> <span class="emphasis"><em>仅仅</em></span> 删除一个值的首次出现。在这里,<tt class="literal">'new'</tt> 在 list 中出现了两次,但 <tt class="literal">li.remove("new")</tt> 只删除了 <tt class="literal">'new'</tt> 的首次出现。
                        </td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.7.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">如果在 list 中没有找到值,<span class="application">Python</span> 会引发一个异常来响应 <tt class="function">index</tt> 方法。
                        </td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.7.4"><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left"><tt class="function">pop</tt> 是一个有趣的东西。它会做两件事:删除 list 的最后一个元素,然后返回删除元素的值。请注意,这与 <tt class="literal">li[-1]</tt> 不同,后者返回一个值但不改变 list 本身。也不同于 <tt class="literal">li.remove(<i class="replaceable">value</i>)</tt>,后者改变 list 但并不返回值。
                        </td>
                     </tr>
                  </table>
               </div>
            </div>
         </div>
         <div class="section" lang="zh_cn">
            <div class="titlepage">
               <div>
                  <div>
                     <h3 class="title"><a name="d0e6522"></a>3.2.5.&nbsp;使用 list 的运算符
                     </h3>
                  </div>
               </div>
               <div></div>
            </div>
            <div class="example"><a name="odbchelper.list.operators"></a><h3 class="title">&nbsp;3.14.&nbsp;List 运算符</h3><pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li = [<span class='pystring'>'a'</span>, <span class='pystring'>'b'</span>, <span class='pystring'>'mpilgrim'</span>]</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li = li + [<span class='pystring'>'example'</span>, <span class='pystring'>'new'</span>]</span> <a name="odbchelper.list.8.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'mpilgrim', 'example', 'new']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li += [<span class='pystring'>'two'</span>]</span>                <a name="odbchelper.list.8.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">['a', 'b', 'mpilgrim', 'example', 'new', 'two']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li = [1, 2] * 3</span>              <a name="odbchelper.list.8.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li</span>
<span class="computeroutput">[1, 2, 1, 2, 1, 2]</span></pre><div class="calloutlist">
                  <table border="0" summary="Callout list">
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.8.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left">Lists 也可以用 <tt class="literal">+</tt> 运算符连接起来。<tt class="literal"><i class="replaceable">list</i> = <i class="replaceable">list</i> + <i class="replaceable">otherlist</i></tt> 相当于 <tt class="literal"><i class="replaceable">list</i>.extend(<i class="replaceable">otherlist</i>)</tt>。但 <tt class="literal">+</tt> 运算符把一个新 (连接后) 的 list 作为值返回,而 <tt class="function">extend</tt> 只修改存在的 list。也就是说,对于大型 list 来说,<tt class="function">extend</tt> 的执行速度要快一些。
                        </td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.8.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left"><span class="application">Python</span> 支持 <tt class="literal">+=</tt> 运算符。<tt class="literal">li += ['two']</tt> 等同于 <tt class="literal">li.extend(['two'])</tt><tt class="literal">+=</tt> 运算符可用于 list、字符串和整数,并且它也可以被重载用于用户自定义的类中 (更多关于类的内容参见 <a href="../object_oriented_framework/index.html">第 5 章</a>)。
                        </td>
                     </tr>
                     <tr>
                        <td width="12" valign="top" align="left"><a href="#odbchelper.list.8.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                        </td>
                        <td valign="top" align="left"><tt class="literal">*</tt> 运算符可以作为一个重复器作用于 list。<tt class="literal">li = [1, 2] * 3</tt> 等同于 <tt class="literal">li = [1, 2] + [1, 2] + [1, 2]</tt>,即将三个 list 连接成一个。
                        </td>
                     </tr>
                  </table>
               </div>
            </div>
            <div class="furtherreading">
               <h3>进一步阅读</h3>
               <ul>
                  <li><a href="http://www.ibiblio.org/obp/thinkCSpy/" title="Python book for computer science majors"><i class="citetitle">How to Think Like a Computer Scientist</i></a> 讲述了list,并且重点讲述了如何<a href="http://www.ibiblio.org/obp/thinkCSpy/chap08.htm">把 list 作为函数参数传递</a></li>
                  <li><a href="http://www.python.org/doc/current/tut/tut.html"><i class="citetitle"><span class="application">Python</span> Tutorial</i></a> 展示了如何<a href="http://www.python.org/doc/current/tut/node7.html#SECTION007110000000000000000">把 list 作为堆栈和队列使用</a></li>
                  <li><a href="http://www.faqts.com/knowledge-base/index.phtml/fid/199/"><span class="application">Python</span> Knowledge Base</a> 回答了<a href="http://www.faqts.com/knowledge-base/index.phtml/fid/534">有关 list 的常见问题</a>并且有许多<a href="http://www.faqts.com/knowledge-base/index.phtml/fid/540">使用 list 的示例代码</a></li>
                  <li><a href="http://www.python.org/doc/current/lib/"><i class="citetitle"><span class="application">Python</span> Library Reference</i></a> 总结了<a href="http://www.python.org/doc/current/lib/typesseq-mutable.html">所有的 list 方法</a></li>
               </ul>
            </div>
         </div>
      </div>
      <table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td width="35%" align="left"><br><a class="NavigationArrow" href="index.html">&lt;&lt;&nbsp;内置数据类型</a></td>
            <td width="30%" align="center"><br>&nbsp;<span class="divider">|</span>&nbsp;<a href="index.html#odbchelper.dict" title="3.1.&nbsp;Dictionary 介绍">1</a> <span class="divider">|</span> <span class="thispage">2</span> <span class="divider">|</span> <a href="tuples.html" title="3.3.&nbsp;Tuple 介绍">3</a> <span class="divider">|</span> <a href="declaring_variables.html" title="3.4.&nbsp;变量声明">4</a> <span class="divider">|</span> <a href="formatting_strings.html" title="3.5.&nbsp;格式化字符串">5</a> <span class="divider">|</span> <a href="mapping_lists.html" title="3.6.&nbsp;映射 list">6</a> <span class="divider">|</span> <a href="joining_lists.html" title="3.7.&nbsp;连接 list 与分割字符串">7</a> <span class="divider">|</span> <a href="summary.html" title="3.8.&nbsp;小结">8</a>&nbsp;<span class="divider">|</span>&nbsp;
            </td>
            <td width="35%" align="right"><br><a class="NavigationArrow" href="tuples.html">Tuple 介绍&nbsp;&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3"><br></td>
         </tr>
      </table>
      <div class="Footer">
         <p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
         <p class="copyright">Copyright © 2001, 2002, 2003, 2004, 2005, 2006, 2007 <a href="mailto:python-cn@googlegroups.com">CPyUG (邮件列表)</a></p>
      </div>
   </body>
</html>