Repository navigation
Expand file tree
/
Copy pathDataStructures.html
More file actions
443 lines (322 loc) · 27 KB
/
Copy pathDataStructures.html
File metadata and controls
443 lines (322 loc) · 27 KB
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
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh-CN" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh-CN" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta content="Topic: Data Structures, Difficulty: Medium, Category: Section" name="description" />
<meta content="Big-O, complexity, efficiency, algorithm, interview preparation, list, tuple, sequence" name="keywords" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>数据结构(第一部分):简介 — Python Like You Mean It</title>
<script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="https://www.googletagmanager.com/gtag/js?id=UA-115029372-1"></script>
<script src="../_static/gtag.js"></script>
<script src="../_static/translations.js"></script>
<script crossorigin="anonymous" integrity="sha256-Ae2Vz/4ePdIu6ZyI/5ZGsYnb+m0JlOmKPjt6XZ9JJkA=" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.4/require.min.js"></script>
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["$", "$"], ["\\(", "\\)"]], "processEscapes": true, "ignoreClass": "document", "processClass": "math|output_area"}})</script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/my_theme.css" type="text/css" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="next" title="数据结构(第二部分):词典" href="DataStructures_II_Dictionaries.html" />
<link rel="prev" title="作用域" href="Scope.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../index.html" class="icon icon-home"> Python Like You Mean It
</a>
<div class="version">
1.4
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">目录:</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../intro.html">Python Like You Mean It</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_1.html">模组1:Python入门</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../module_2.html">模组2:Python基础</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="Basic_Objects.html">基本对象类型</a></li>
<li class="toctree-l2"><a class="reference internal" href="SequenceTypes.html">序列类型</a></li>
<li class="toctree-l2"><a class="reference internal" href="Variables_and_Assignment.html">变量和赋值</a></li>
<li class="toctree-l2"><a class="reference internal" href="Introduction.html">控制流简介</a></li>
<li class="toctree-l2"><a class="reference internal" href="ConditionalStatements.html">条件语</a></li>
<li class="toctree-l2"><a class="reference internal" href="ForLoops.html">For循环和While循环</a></li>
<li class="toctree-l2"><a class="reference internal" href="Iterables.html">可迭代物</a></li>
<li class="toctree-l2"><a class="reference internal" href="Generators_and_Comprehensions.html">生成器和理解表达式</a></li>
<li class="toctree-l2"><a class="reference internal" href="Itertools.html">Python的“Itertools”</a></li>
<li class="toctree-l2"><a class="reference internal" href="Functions.html">函数基础</a></li>
<li class="toctree-l2"><a class="reference internal" href="Scope.html">作用域</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">数据结构(第一部分):简介</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#描述算法复杂度">描述算法复杂度</a></li>
<li class="toctree-l3"><a class="reference internal" href="#序列性数据结构:列表和元组">序列性数据结构:列表和元组</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#列表/元组复杂度">列表/元组复杂度</a></li>
<li class="toctree-l4"><a class="reference internal" href="#列表复杂度">列表复杂度</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="DataStructures_II_Dictionaries.html">数据结构(第二部分):词典</a></li>
<li class="toctree-l2"><a class="reference internal" href="DataStructures_III_Sets_and_More.html">数据结构(第三部分):集和集合模组</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../module_2_problems.html">模组2:题目</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_3.html">模组3:NumPy基础</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_3_problems.html">模组3:题目</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_4.html">模组4:面向对象编程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../module_5.html">模组5:琐碎话题</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Python Like You Mean It</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html">Docs</a> »</li>
<li><a href="../module_2.html">模组2:Python基础</a> »</li>
<li>数据结构(第一部分):简介</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/Module2_EssentialsOfPython/DataStructures.md.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<style>
/* CSS overrides for sphinx_rtd_theme */
/* 24px margin */
.nbinput.nblast.container,
.nboutput.nblast.container {
margin-bottom: 19px; /* padding has already 5px */
}
/* ... except between code cells! */
.nblast.container + .nbinput.container {
margin-top: -19px;
}
.admonition > p:before {
margin-right: 4px; /* make room for the exclamation icon */
}
/* Fix math alignment, see https://github.com/rtfd/sphinx_rtd_theme/pull/686 */
.math {
text-align: unset;
}
</style>
<div class="section" id="数据结构(第一部分):简介">
<h1>数据结构(第一部分):简介<a class="headerlink" href="#数据结构(第一部分):简介" title="永久链接至标题">¶</a></h1>
<p>我们在此介绍Python内置的数据结构(data structure)。你应该已经了解了列表和元组,两个帮助处理序列数据的数据结构。我们将会介绍另外两个重要的内置数据结构:</p>
<ul class="simple">
<li><p>词典(dictionary):一对一对应“键”(key)和“值”(value)</p></li>
<li><p>集(set):用来进行集运算(如并集和交集)的无序集合</p></li>
</ul>
<p>这些数据结构不仅仅是一些有着好用核心功能的方便构造题;它们同时也提供了用C语言(或你使用的Python直译器编译用的语言)编译的高效核心功能的接口。比如说,让我们来编写一个检查某对象是否为可迭代物成员的函数:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">is_in</span><span class="p">(</span><span class="n">seq</span><span class="p">,</span> <span class="n">target</span><span class="p">):</span>
<span class="sd">""" 如果 `target` 在 `seq` 中则返回真"""</span>
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">seq</span><span class="p">:</span>
<span class="k">if</span> <span class="n">item</span> <span class="o">==</span> <span class="n">target</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">True</span>
<span class="k">return</span> <span class="kc">False</span>
</pre></div>
</div>
<p>以上函数和Python在“幕后”使用的C语言成员检测算法(当然,这是假设你在使用CPython直译器,但极大可能是这个情况)是一样的。因为内置的成员检测函数是在“更底层”实现的并因此不需要被直译,所以它比我们在Python中定义的函数更快:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s2">"moo"</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">is_in</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># 在我的机子上花了 980 纳秒</span>
<span class="go">False</span>
<span class="gp">>>> </span><span class="o">-</span><span class="mi">1</span> <span class="ow">in</span> <span class="n">x</span> <span class="c1"># 在我的机子上花了 320 纳秒</span>
<span class="go">False</span>
</pre></div>
</div>
<p>在此,Python的内置成员检测函数比我们的版本快了三倍。同时,我们应该了解各个数据结构的优势。比如说,集(set)的成员检测比列表还要快:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># 在列表中检测是否为成员</span>
<span class="o">>>></span> <span class="o">-</span><span class="mi">1</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="s2">"moo"</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">]</span> <span class="c1"># 在我的机子上花了 295 纳秒</span>
<span class="kc">False</span>
<span class="c1"># 在集中检测是否为成员</span>
<span class="o">>>></span> <span class="o">-</span><span class="mi">1</span> <span class="ow">in</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="s2">"moo"</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">}</span> <span class="c1"># 在我的机子上花了 65 纳秒</span>
<span class="kc">False</span>
</pre></div>
</div>
<p>通过使用集而不是列表,我们得到了4.5倍的速度优化。这是因为集允许Python使用一个完全不同的成员检测算法。在Python程序员的角度上,我们仅仅将方括号替换成了花括号!我希望以上范例为你提供了足够学习Python数据结构和它们“幕后”使用的算法的理由。</p>
<div class="admonition note">
<p class="admonition-title fa fa-exclamation-circle"><strong>经验</strong>:</p>
<p>Python的数据结构有着自己内置的功能。同时,理解每个数据结构适合的工作对编写高效的Python代码极其重要。你不需要死记这些信息,但是你应该知道这些信息存在并经常查阅这些信息。</p>
</div>
<div class="section" id="描述算法复杂度">
<h2>描述算法复杂度<a class="headerlink" href="#描述算法复杂度" title="永久链接至标题">¶</a></h2>
<p>为了有意义地比较算法的效率,总结算法在问题大小变化时如何表现是很有用的信息。两个排序算法可能在排列几十个项目时速度相当,但它们完全可能在排列几千个项目时有着很不一样的效率。</p>
<p>“大O”符号(big-O notation)允许我们表达算法在问题大小变化时运算时间如何改变。具体来讲,它代表着算法在“最坏情况下”的表现。</p>
<p>比如说,拿在本节开始写的 <code class="docutils literal notranslate"><span class="pre">is_in</span></code> 函数来讲,我们迭代了一个集合并检查它是否有着某个项目。这个算法的最坏情况就是当项目并不是集合的成员——我们需要迭代整个集合才能确定它并不拥有我们要找的项目。所以如果我们将该集合的大小扩大 <span class="math notranslate nohighlight">\(n\)</span> 倍,那它会花 <span class="math notranslate nohighlight">\(n\)</span> 倍多的时间来迭代整个集合并决定一个项目不是其成员(在此同样假设最坏情况)。因为 <code class="docutils literal notranslate"><span class="pre">is_in</span></code> 的最坏情况的运行时间和集合的大小 <span class="math notranslate nohighlight">\(n\)</span> 呈线性关系,所以我们用大O符号来将该算法的运行时间复杂度写为 <span class="math notranslate nohighlight">\(\mathcal{O}(n)\)</span>。</p>
<p>现在,假设我们写的成员检查函数很糟糕,其进行了嵌套迭代:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">is_in_slow</span><span class="p">(</span><span class="n">seq</span><span class="p">,</span> <span class="n">target</span><span class="p">):</span>
<span class="sd">""" 如果 `target` 为 `seq` 成员,返回真。"""</span>
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">seq</span><span class="p">:</span>
<span class="c1"># 我们为seq中的项目重新迭代整个seq!</span>
<span class="k">for</span> <span class="n">item2</span> <span class="ow">in</span> <span class="n">seq</span><span class="p">:</span>
<span class="k">if</span> <span class="n">item</span> <span class="o">==</span> <span class="n">target</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">True</span>
<span class="k">return</span> <span class="kc">False</span>
</pre></div>
</div>
<p>我们为 <code class="docutils literal notranslate"><span class="pre">seq</span></code> 中的每个项目重新迭代 <code class="docutils literal notranslate"><span class="pre">seq</span></code> 一次,因此在最坏情况下我们需要迭代 <span class="math notranslate nohighlight">\(n\)</span> 个成员 <span class="math notranslate nohighlight">\(n\)</span> 次——总共在算法中走了 <span class="math notranslate nohighlight">\(n^{2}\)</span> “步”。因此,我们说 <code class="docutils literal notranslate"><span class="pre">is_in_slow</span></code> 是一个 <span class="math notranslate nohighlight">\(\mathcal{O}(n^{2})\)</span> 的算法:当我们将 <code class="docutils literal notranslate"><span class="pre">seq</span></code> 的大小翻倍时,我们的 <span class="math notranslate nohighlight">\(\mathcal{O}(n)\)</span> 算法会花两倍多的时间(线性增加),而我们的 <span class="math notranslate nohighlight">\(\mathcal{O}(n^{2})\)</span> 算法会花四倍多的时间(平方增加)。</p>
<p>以下是大O符号更加正式的定义:</p>
<div class="alert alert-block alert-info"><p><strong>“大O”符号</strong>(big-O notation):假设 <span class="math notranslate nohighlight">\(n\)</span> 代表着算法输入的“大小”,且数学表达式 <span class="math notranslate nohighlight">\(f(n)\)</span> 描述该算法在<em>最坏情况</em>下需要花多少计算步骤处理大小为 <span class="math notranslate nohighlight">\(n\)</span> 的输入,那么算法的运行时间复杂度用<strong>“大O”符号</strong>表达为 <span class="math notranslate nohighlight">\(\mathcal{O}(f(n))\)</span>。</p>
</div><p>几个重要的点:</p>
<ul class="simple">
<li><p>我们仅仅在乎 <span class="math notranslate nohighlight">\(f(n)\)</span> 最高次数的项。也就是说,<span class="math notranslate nohighlight">\(\mathcal{O}(n + n^{2})\)</span> 应被写为 <span class="math notranslate nohighlight">\(\mathcal{O}(n^{2})\)</span>。</p></li>
<li><p>我们永远不在乎关系中的常数。也就是说,就算一个算法迭代某序列两次,它的大O复杂度应该被写为 <span class="math notranslate nohighlight">\(\mathcal{O}(n)\)</span>,而不是 <span class="math notranslate nohighlight">\(\mathcal{O}(2n)\)</span>。</p></li>
<li><p>一个运行时间<em>不根据输入大小而改变</em>的算法是一个 <span class="math notranslate nohighlight">\(\mathcal{O}(1)\)</span> 算法。</p>
<ul>
<li><p>例:一个返回列表中第二个成员的函数。</p></li>
</ul>
</li>
<li><p>我们有其它更加微妙,不仅仅考虑最坏情况的分析算法复杂度的方法,因为最坏情况分析可能太过悲观。“大O”符号“也可以用来代表平均表现,<a class="reference external" href="https://en.wikipedia.org/wiki/Amortized_analysis">摊销</a>(amortized)表现,和其它分析类型。在此,我们仅仅使用最坏情况分析。</p></li>
</ul>
<div class="admonition note">
<p class="admonition-title fa fa-exclamation-circle"><strong>经验</strong>:</p>
<p>我们将会使用“大O”符号 <span class="math notranslate nohighlight">\(\mathcal{O}(f(n))\)</span> 来总结Python数据结构使用的算法的表现。</p>
</div>
</div>
<div class="section" id="序列性数据结构:列表和元组">
<h2>序列性数据结构:列表和元组<a class="headerlink" href="#序列性数据结构:列表和元组" title="永久链接至标题">¶</a></h2>
<p>“序列类型”小节已经介绍了列表和元组。请回忆这两个数据结构都提供了相同的交互界面来访问和总结其不同类型对象的成员序列。但是,列表是可以被改变的——你可以对其更新,删除,并添加项目——而元组不可变。因此列表<em>可变</em>,而元组<em>不可变</em>。在本节你将会找到很多操作序列数据结构的内置函数的算法复杂度。</p>
<p>你可以在<a class="reference external" href="https://docs.python.org/3/tutorial/datastructures.html#more-on-lists">这里</a>找到所有的Python列表函数。</p>
<div class="section" id="列表/元组复杂度">
<h3>列表/元组复杂度<a class="headerlink" href="#列表/元组复杂度" title="永久链接至标题">¶</a></h3>
<p>设 <code class="docutils literal notranslate"><span class="pre">seq</span></code> 为长度为 <span class="math notranslate nohighlight">\(n\)</span> 的<strong>列表或元组</strong>;<span class="math notranslate nohighlight">\(i\)</span> 和 <span class="math notranslate nohighlight">\(j\)</span> 为从区间 <span class="math notranslate nohighlight">\([0, n-1]\)</span> 中随意抽取的整数;<span class="math notranslate nohighlight">\(k\)</span> 为任何在此操作中使用的子序列的长度。以下为各种常见的列表和元组操作(根据CPython的实现)的复杂度:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 34%" />
<col style="width: 12%" />
<col style="width: 54%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>操作</p></th>
<th class="head"><p>复杂度</p></th>
<th class="head"><p>解释</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">len(seq)</span></code></p></td>
<td><p>O(1)</p></td>
<td><p>返回序列中成员的数量</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">seq[i]</span></code></p></td>
<td><p>O(1)</p></td>
<td><p>从序列中获取单个成员</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">seq[i:j]</span></code></p></td>
<td><p>O(k)</p></td>
<td><p>从序列中获取长度k的切片</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">for</span> <span class="pre">item</span> <span class="pre">in</span> <span class="pre">seq..</span></code></p></td>
<td><p>O(n)</p></td>
<td><p>迭代序列</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">obj</span> <span class="pre">in</span> <span class="pre">seq</span></code></p></td>
<td><p>O(n)</p></td>
<td><p>检查 <code class="docutils literal notranslate"><span class="pre">obj</span></code> 是否为 <code class="docutils literal notranslate"><span class="pre">seq</span></code> 的成员</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">seq.count(obj)</span></code></p></td>
<td><p>O(n)</p></td>
<td><p>返回 <code class="docutils literal notranslate"><span class="pre">obj</span></code> 在 <code class="docutils literal notranslate"><span class="pre">seq</span></code> 出现的次数</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">seq.index(obj)</span></code></p></td>
<td><p>O(n)</p></td>
<td><p>返回 <code class="docutils literal notranslate"><span class="pre">obj</span></code> 在 <code class="docutils literal notranslate"><span class="pre">seq</span></code> 中的索引</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="列表复杂度">
<h3>列表复杂度<a class="headerlink" href="#列表复杂度" title="永久链接至标题">¶</a></h3>
<p>我们在此考虑一些列表可用而元组不可用的改变序列的操作,如 <code class="docutils literal notranslate"><span class="pre">append</span></code>。请注意列表的实现导致:</p>
<ul class="simple">
<li><p>在列表<em>结尾</em>添加或删除成员的操作为 <span class="math notranslate nohighlight">\(\mathcal{O}(1)\)</span></p></li>
<li><p>在列表<em>开头</em>添加或删除成员的操作为 <span class="math notranslate nohighlight">\(\mathcal{O}(n)\)</span></p></li>
</ul>
<p>设 <code class="docutils literal notranslate"><span class="pre">my_list</span></code> 为长度为 <span class="math notranslate nohighlight">\(n\)</span> 的列表,而 <code class="docutils literal notranslate"><span class="pre">i</span></code> 为从区间 <span class="math notranslate nohighlight">\([0, n-1]\)</span> 随意抽取的整数。以下为列表各种操作(在CPython实现中)的复杂度:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 37%" />
<col style="width: 18%" />
<col style="width: 46%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>操作</p></th>
<th class="head"><p>复杂度</p></th>
<th class="head"><p>解释</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">my_list[i]</span> <span class="pre">=</span> <span class="pre">obj</span></code></p></td>
<td><p>O(1)</p></td>
<td><p>将列表第i位的项目设为新对象。</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">my_list.append(obj)</span></code></p></td>
<td><p>O(1)</p></td>
<td><p>在列表结尾添加新对象。</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">my_list.pop()</span></code></p></td>
<td><p>O(1)</p></td>
<td><p>删除在列表<em>结尾</em>的对象。</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">my_list.pop(0)</span></code></p></td>
<td><p>O(n)</p></td>
<td><p>删除在列表<em>开头</em>的对象。</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">my_list.sort()</span></code></p></td>
<td><p>O(nlog(n))</p></td>
<td><p>返回同列表的顺序版本。</p></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="DataStructures_II_Dictionaries.html" class="btn btn-neutral float-right" title="数据结构(第二部分):词典" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="Scope.html" class="btn btn-neutral float-left" title="作用域" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2019, Ryan Soklaski
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>