-
Notifications
You must be signed in to change notification settings - Fork 2
/
list_tuple_compare.html
159 lines (143 loc) · 12.5 KB
/
list_tuple_compare.html
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
<!DOCTYPE html>
<html lang="cn">
<head>
<meta charset="utf-8" />
<title>python经典面试题:列表和元组有什么异同?</title>
<link rel="stylesheet" href="/theme/css/main.css" />
</head>
<body id="index" class="home">
<header id="banner" class="body">
<h1><a href="/">python自动化测试人工智能 </a></h1>
<nav><ul>
<li><a href="/category/ba-zi.html">八字</a></li>
<li><a href="/category/ce-shi.html">测试</a></li>
<li><a href="/category/ce-shi-kuang-jia.html">测试框架</a></li>
<li><a href="/category/common.html">common</a></li>
<li><a href="/category/da-shu-ju.html">大数据</a></li>
<li><a href="/category/feng-shui.html">风水</a></li>
<li><a href="/category/ji-qi-xue-xi.html">机器学习</a></li>
<li><a href="/category/jie-meng.html">解梦</a></li>
<li><a href="/category/linux.html">linux</a></li>
<li class="active"><a href="/category/python.html">python</a></li>
<li><a href="/category/shu-ji.html">书籍</a></li>
<li><a href="/category/shu-ju-fen-xi.html">数据分析</a></li>
<li><a href="/category/zhong-cao-yao.html">中草药</a></li>
<li><a href="/category/zhong-yi.html">中医</a></li>
</ul></nav>
</header><!-- /#banner -->
<section id="content" class="body">
<article>
<header>
<h1 class="entry-title">
<a href="/list_tuple_compare.html" rel="bookmark"
title="Permalink to python经典面试题:列表和元组有什么异同?">python经典面试题:列表和元组有什么异同?</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2018-11-27T05:20:00+08:00">
Published: 二 27 十一月 2018
</abbr>
<address class="vcard author">
By <a class="url fn" href="/author/andrew.html">andrew</a>
</address>
<p>In <a href="/category/python.html">python</a>.</p>
</footer><!-- /.post-info --> <ul>
<li><a href="https://china-testing.github.io/practices.html">python测试开发项目实战-目录</a></li>
<li><a href="https://china-testing.github.io/python_books.html">python工具书籍下载-持续更新</a></li>
<li><a href="https://china-testing.github.io/python3_quick.html">python 3.7极速入门教程 - 目录</a></li>
</ul>
<p>列表和元组是Python中最常用的两种数据结构,字典是第三种。 相同点:</p>
<ul>
<li>都是序列</li>
<li>都可以存储任何数据类型</li>
<li>可以通过索引访问</li>
</ul>
<p><img alt="图片.png" src="https://upload-images.jianshu.io/upload_images/12713060-32a6c6361069d513.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"></p>
<h3 id="_1">语法差异</h3>
<p>使用方括号[]创建列表,而使用括号()创建元组。 请看以下示例:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">l</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"https://china-testing.github.io/"</span><span class="p">,</span> <span class="s2">"https://www.oscobo.com/"</span><span class="p">]</span>
<span class="o">>>></span> <span class="n">t</span> <span class="o">=</span> <span class="p">(</span><span class="s2">"https://china-testing.github.io/"</span><span class="p">,</span> <span class="s2">"https://www.oscobo.com/"</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
<span class="p">[</span><span class="s1">'https://china-testing.github.io/'</span><span class="p">,</span> <span class="s1">'https://www.oscobo.com/'</span><span class="p">]</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
<span class="p">(</span><span class="s1">'https://china-testing.github.io/'</span><span class="p">,</span> <span class="s1">'https://www.oscobo.com/'</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">l</span><span class="p">))</span>
<span class="o"><</span><span class="k">class</span> <span class="err">'</span><span class="nc">list</span><span class="s1">'></span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">t</span><span class="p">))</span>
<span class="o"><</span><span class="k">class</span> <span class="err">'</span><span class="nc">tuple</span><span class="s1">'></span>
</pre></div>
<h3 id="_2">是否可变</h3>
<p>列表是可变的,而元组是不可变的,这标志着两者之间的关键差异。 </p>
<p>我们可以修改列表的值,但是不修改元组的值。</p>
<p>由于列表是可变的,我们不能将列表用作字典中的key。 但可以使用元组作为字典key。</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">l</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"https://www.jianshu.com/u/69f40328d4f0"</span>
<span class="o">>>></span> <span class="n">l</span>
<span class="p">[</span><span class="s1">'https://china-testing.github.io/'</span><span class="p">,</span> <span class="s1">'https://www.jianshu.com/u/69f40328d4f0'</span><span class="p">]</span>
<span class="o">>>></span> <span class="n">t</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"https://www.jianshu.com/u/69f40328d4f0"</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
<span class="n">File</span> <span class="s2">"<stdin>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="p">,</span> <span class="ow">in</span> <span class="o"><</span><span class="n">module</span><span class="o">></span>
<span class="ne">TypeError</span><span class="p">:</span> <span class="s1">'tuple'</span> <span class="nb">object</span> <span class="n">does</span> <span class="ow">not</span> <span class="n">support</span> <span class="n">item</span> <span class="n">assignment</span>
</pre></div>
<h3 id="_3">重用与拷贝</h3>
<p>元组无法复制。 原因是元组是不可变的。 如果运行tuple(tuple_name)将返回自己。 </p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">copy_t</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">t</span> <span class="ow">is</span> <span class="n">copy_t</span><span class="p">)</span>
<span class="bp">True</span>
<span class="o">>>></span> <span class="n">copy_l</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">l</span> <span class="ow">is</span> <span class="n">copy_l</span><span class="p">)</span>
<span class="bp">False</span>
</pre></div>
<h3 id="_4">参考资料</h3>
<ul>
<li>讨论 qq群144081101 567351477</li>
<li><a href="https://china-testing.github.io/list_tuple_compare.html">本文最新版本地址</a></li>
<li><a href="https://github.com/china-testing/python-api-tesing">本文涉及的python测试开发库</a> 谢谢点赞!</li>
<li><a href="https://github.com/china-testing/python-api-tesing/blob/master/books.md">本文相关海量书籍下载</a> </li>
<li><a href="https://china-testing.github.io/python_books.html">python工具书籍下载-持续更新</a></li>
</ul>
<h3 id="_5">大小差异</h3>
<p>Python将低开销的较大的块分配给元组,因为它们是不可变的。 对于列表则分配小内存块。 与列表相比,元组的内存更小。 当你拥有大量元素时,元组比列表快。列表的长度是可变的。</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">l</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"https://china-testing.github.io/"</span><span class="p">,</span> <span class="s2">"https://www.oscobo.com/"</span><span class="p">]</span>
<span class="o">>>></span> <span class="n">t</span> <span class="o">=</span> <span class="p">(</span><span class="s2">"https://china-testing.github.io/"</span><span class="p">,</span> <span class="s2">"https://www.oscobo.com/"</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">l</span><span class="o">.</span><span class="n">__sizeof__</span><span class="p">())</span>
<span class="mi">56</span>
<span class="o">>>></span> <span class="k">print</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">__sizeof__</span><span class="p">())</span>
<span class="mi">40</span>
</pre></div>
<h3 id="_6">同构与异构</h3>
<p>习惯上元组多用于用于存储异构元素,异构元素即不同数据类型的元素,比如(ip,port)。 另一方面,列表用于存储异构元素,这些元素属于相同类型的元素,比如[int1,in2,in3]。</p>
<h3 id="_7">参考资料</h3>
<ul>
<li>讨论qq群630011153 144081101</li>
<li><a href="https://china-testing.github.io/list_tuple_compare.html">本文最新版本地址</a></li>
<li><a href="https://github.com/china-testing/python-api-tesing">本文涉及的python测试开发库</a> 谢谢点赞!</li>
<li><a href="https://github.com/china-testing/python-api-tesing/blob/master/books.md">本文相关海量书籍下载</a> </li>
<li>道家技术-手相手诊看相中医等钉钉群21734177 qq群:391441566 184175668 338228106 看手相、面相、舌相、抽签、体质识别。服务费50元每人次起。请联系钉钉或者微信pythontesting</li>
<li><a href="https://china-testing.github.io/testing_training.html">接口自动化性能测试线上培训大纲</a></li>
<li><a href="https://www.fullstackpython.com/monitoring.html">Monitoring</a></li>
<li><a href="https://www.jianshu.com/p/49202312f855">2018最佳人工智能机器学习工具书及下载(持续更新)</a></li>
<li>https://github.com/lorien/awesome-web-scraping/blob/master/python.md</li>
<li>最好用的Python爬虫推荐 https://www.jianshu.com/p/7da43c16dd87</li>
<li>https://www.zhihu.com/question/41277528</li>
</ul>
</div><!-- /.entry-content -->
</article>
</section>
<section id="extras" class="body">
<div class="blogroll">
<h2>links</h2>
<ul>
<li><a href="https://china-testing.github.io/testing_training.html">自动化性能接口测试线上及深圳培训与项目实战 qq群:144081101 591302926</a></li>
<li><a href="http://blog.sciencenet.cn/blog-2604609-1112306.html">pandas数据分析scrapy爬虫 521070358 Py人工智能pandas-opencv 6089740</a></li>
<li><a href="http://blog.sciencenet.cn/blog-2604609-1112306.html">中医解梦看相八字算命qq群 391441566 csdn书籍下载-python爬虫 437355848</a></li>
</ul>
</div><!-- /.blogroll -->
</section><!-- /#extras -->
<footer id="contentinfo" class="body">
<address id="about" class="vcard body">
Proudly powered by <a href="http://getpelican.com/">Pelican</a>, which takes great advantage of <a href="http://python.org">Python</a>.
</address><!-- /#about -->
<p>The theme is by <a href="http://coding.smashingmagazine.com/2009/08/04/designing-a-html-5-layout-from-scratch/">Smashing Magazine</a>, thanks!</p>
</footer><!-- /#contentinfo -->
</body>
</html>