-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPython-字典.html
154 lines (143 loc) · 6.53 KB
/
Python-字典.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>商伟的技术博客</title>
<meta name="description" content="">
<meta name="author" content="商伟">
<!-- HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]>
<script src="/theme/html5.js"></script>
<![endif]-->
<!-- Styles -->
<link href="/theme/bootstrap.min.css" rel="stylesheet">
<link href="/theme/local.css" rel="stylesheet">
<link href="/theme/pygments.css" rel="stylesheet">
<!-- Feeds -->
</head>
<body>
<div class="topbar">
<div class="topbar-inner">
<div class="container-fluid">
<a class="brand" href="/">商伟的技术博客</a>
<ul class="nav">
<li ><a href="/category/django.html">Django</a></li>
<li ><a href="/category/docker.html">Docker</a></li>
<li ><a href="/category/git.html">GIT</a></li>
<li ><a href="/category/javascript.html">JavaScript</a></li>
<li ><a href="/category/mongodb.html">Mongodb</a></li>
<li ><a href="/category/mysql.html">MySQL</a></li>
<li ><a href="/category/pa-chong.html">爬虫</a></li>
<li class="active"><a href="/category/python.html">Python</a></li>
<li ><a href="/category/rabbitmq.html">RabbitMQ</a></li>
<li ><a href="/category/redis.html">redis</a></li>
<li ><a href="/category/shu-ju-jie-gou.html">数据结构</a></li>
<li ><a href="/category/sui-shou-bi-ji.html">随手笔记</a></li>
<li ><a href="/category/supervisor.html">Supervisor</a></li>
<li ><a href="/category/vue.html">VUE</a></li>
<li ><a href="/category/wang-luo.html">网络</a></li>
<li ><a href="/category/web.html">web</a></li>
<li ><a href="/category/xiao-cheng-xu.html">小程序</a></li>
<li ><a href="/category/xu-ni-huan-jing.html">虚拟环境</a></li>
</ul>
<p class="pull-right"><a href="/archives.html">[archives]</a> <a href="/tags.html">[tags]</a></p>
</div>
</div>
</div>
<div class="container-fluid">
<div class="sidebar">
<div class="well">
<h3>Blogroll</h3>
<ul>
<li><a href="http://getpelican.com/">Pelican</a></li>
<li><a href="http://python.org/">Python.org</a></li>
<li><a href="http://jinja.pocoo.org/">Jinja2</a></li>
</ul>
<div class="social">
<h3>Social</h3>
<ul>
<li><a href="https://lienze.tech/">老渔夫吃虾米</a></li>
</ul>
</div>
</div>
</div>
<div class="content">
<div class='article'>
<div class="page-header"><h1>Python-字典</h1></div>
<div class="well small">Permalink: <a class="more" href="/Python-字典.html">1000-01-09 18:44:00+00:09</a>
by <a class="url fn" href="/author/shang-wei.html">商伟 </a>
in <a href="/category/python.html">Python</a>
tags: <a href="/tag/python.html">Python</a> </div>
<div><p>[TOC]</p>
<h4>字典的介绍</h4>
<div class="highlight"><pre><span></span><span class="n">info</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span><span class="s1">'班长'</span><span class="p">,</span> <span class="s1">'id'</span><span class="p">:</span><span class="mi">100</span><span class="p">,</span> <span class="s1">'sex'</span><span class="p">:</span><span class="s1">'f'</span><span class="p">,</span> <span class="s1">'address'</span><span class="p">:</span><span class="s1">'地球亚洲中国北京'</span><span class="p">}</span>
</pre></div>
<ul>
<li>字典和列表一样,也能够存储多个数据</li>
<li>列表中找某个元素时,是根据下标进行的</li>
<li>字典中找某个元素时,是根据'名字'(就是冒号:前面的那个值,例如上面代码中的'name'、'id'、'sex')</li>
<li>字典的每个元素由2部分组成,键:值。例如 'name':'班长' ,'name'为键,'班长'为值</li>
</ul>
<h5>字典的添加操作</h5>
<ul>
<li>info['id'] = ‘shangwei’</li>
<li>如果在使用 <strong>变量名['键'] = 数据</strong> 时,这个“键”在字典中,不存在,那么就会新增这个元素</li>
</ul>
<h5>字典的删除操作</h5>
<ul>
<li>del</li>
<li>del删除指定的元素</li>
<li>clear()</li>
<li>:clear清空整个字典</li>
</ul>
<h5>字典的修改操作</h5>
<ul>
<li>字典的修改操作就是 通过key来进行修改</li>
<li>info['id'] = 123</li>
</ul>
<h5>字典的查找操作</h5>
<ul>
<li>直接查找key</li>
<li>info['age']</li>
<li>如果字典存在key 返回他的 value 不存在则报错</li>
<li>在我们不确定字典中是否存在某个键而又想获取其值时,可以使用get方法,还可以设置默认值</li>
<li>age = info.get('age') #'age'键不存在,所以age为None</li>
<li>age = info.get('age', 18) # 若info中不存在'age'这个键,就返回默认值18</li>
</ul>
<h5>字典的常见操作</h5>
<ul>
<li>keys</li>
<li>返回一个包含字典所有KEY的列表</li>
<li>values</li>
<li>返回一个包含字典所有VALUE的列表</li>
<li>items</li>
<li>返回一个包含所有(键,值)元祖的列表</li>
<li>has_key (Python3 已取消)</li>
<li>dict.has_key(key)如果key在字典中,返回True,否则返回False</li>
</ul>
<h5>遍历</h5>
<ul>
<li>我们可以用 for 循环来遍历字典</li>
</ul>
<h5>enumerate()</h5>
<blockquote>
<p>enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中。</p>
</blockquote>
<div class="highlight"><pre><span></span><span class="n">chars</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'a'</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">,</span> <span class="s1">'d'</span><span class="p">]</span>
<span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="nb">chr</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">chars</span><span class="p">):</span>
<span class="k">print</span> <span class="n">i</span><span class="p">,</span> <span class="nb">chr</span>
<span class="o">...</span>
<span class="mi">0</span> <span class="n">a</span>
<span class="mi">1</span> <span class="n">b</span>
<span class="mi">2</span> <span class="n">c</span>
<span class="mi">3</span> <span class="n">d</span>
</pre></div></div>
</div>
<footer>
<p>Powered by <a href="http://getpelican.com/">Pelican</a>. Theme based on <a href="http://twitter.github.com/bootstrap/">Twitter Bootstrap</a>.</p>
<p>© 商伟</p>
</footer>
</div>
</div>
</body>
</html>