Python learning 2: detailed dictionary basics

<div class="iteye-blog-content-contain" style="font-size: 14px"><p><div id="cnblogs_post_body"></p>
<p> <p>作者:NiceCui</p> </p>
<p> <ul> </p>
<p>  <li>本文谢绝转载,如需转载需征得作者本人同意,谢谢。</li> </p>
<p>  <li>本文链接:http://www.cnblogs.com/NiceCui/p/7862377.html</li> </p>
<p>  <li>邮箱:[email protected]</li> </p>
<p>  <li>日期:2017-11-19</li> </p>
<p> </ul> </p>
<p> <p style="text-align: center;"><span style="font-size: 18pt;"><strong> PYTHON 学习二:词典基础详解</strong></span></p> </p>
<p> <p style="text-align: center;"><span style="font-size: 12px;">(<span style="font-size: 12px;">学习不要求多,小计划能学会体验到乐趣便可</span>)</span></p> </p>
<p> <p style="text-align: left;"><span style="font-size: 18pt;"><span style="font-size: 14pt;"><strong>一:介绍</strong></span></span></p> </p>
<p> <p style="text-align: left;"><span style="font-size: 16px;"> PYTHON 词典其实和 JAVA The HashMap is very similar, both are implemented by <span style="color: #ff0000;">Hash table</span>, all are <span style="color: #ff0000;"> The key-value pair </span> is stored, that is, the <span style="font-size: 16px;">dictionary</span> is an array, and the index of the array is the hash of the key obtained after the function is processed. In fact, you will encounter many similar storage methods in the learning of programming languages. For example, the cache system Memcached stores data in key-value pairs, and Redis also has them. Therefore, the key-value pair storage method is widely used in programming languages. </span></p> </p>
<p> <p style="text-align: left;"><span style="font-size: 16px;"> ;A dictionary is a container that can hold multiple elements, but the <span style="font-size: 16px;">word</span>dictionary is not position-indexed, <span style="font- size: 16px;">Dictionary</span> allows a custom way to index data. </span></p> </p>

<p> <p style="text-align: left;"><span style="font-size: 16px;">Dictionaries can contain multiple elements, each of which is easy to divide; </span></p> </p>
<p> <p style="text-align: left;"><span style="font-size: 16px;"> ;The element of the dictionary consists of two parts: <span style="color: #ff0000;">key</span>(Key) and <span style="color: #ff0000;">value</ span>(Value), the key is the index of the data, the value is the data itself, and the key and value are in one-to-one correspondence. </span></p> </p>
<p> <p style="text-align: left;">&nbsp;</p> </p >
<p> <div class="cnblogs_code"> </p>

<p><span style="color: #008080;"> 2</span> <span style="color: #008000;">#</span><span style="color: #008000;">coding:utf-8</span></p>
<p><span style="color: #008080;"> 3</span> <span style="color: #800000;">'''</span></p>
<p><span style="color: #008080;"> 4</span> <span style="color: #800000;">Created on 2017-11-19</p>
<p></span><span style="color: #008080;"> 5</span> <span style="color: #800000;">@author: NiceCui</p>
<p></span><span style="color: #008080;"> 6</span> <span style="color: #800000;">'''</span></p>
<p><span style="color: #008080;"> 7</span> </p>
<p><span style="color: #008080;"> 8</span> example_dict = {<span style="color: #800000;">&quot;</span><span style="color: #800000;">a</span><span style="color: #800000;">&quot;</span>:1,<span style="color: #800000;">&quot;</span><span style="color: #800000;">b</span><span style="color: #800000;">&quot;</span>:2,<span style="color: #800000;">&quot;</span><span style="color: #800000;">c</span><span style="color: #800000;">&quot;</span>:3<span style="color: #000000;">}</p>
<p></span><span style="color: #008080;"> 9</span> </p>
<p><span style="color: #008080;">10</span> <span style="color: #0000ff;">print</span>(type(example_dict)) <span style="color: #008000;">#</span><span style="color: #008000;">输出 &lt;type 'dict'&gt;</span></p>
<p><span style="color: #008080;">11</span> </p>
<p><span style="color: #008080;">12</span> <span style="color: #0000ff;">print</span>(example_dict[<span style="color: #800000;">&quot;</span><span style="color: #800000;">a</span><span style="color: #800000;">&quot;</span>])  <span style="color: #008000;">#</span><span style="color: #008000;">输出 1</span></p>
<p><span style="color: #008080;">13</span> </p>
<p><span style="color: #008080;">14</span> <span style="color: #0000ff;">print</span>(example_dict[<span style="color: #800000;">&quot;</span><span style="color: #800000;">b</span><span style="color: #800000;">&quot;</span>])  <span style="color: #008000;">#</span><span style="color: #008000;">输出 2</span></p>
<p><span style="color: #008080;">15</span> </p>
<p><span style="color: #008080;">16</span> <span style="color: #0000ff;">print</span>(example_dict[<span style="color: #800000;">&quot;</span><span style="color: #800000;">c</span><span style="color: #800000;">&quot;</span>])  <span style="color: #008000;">#</span><span style="color: #008000;">输出 3</span></pre> </p>
<p> </div> </p>
<p> <p style="text-align: left;">&nbsp;</p> </p> <p> <div class="cnblogs_code"> </p>
<p> <p><strong><span style="font-size: 14pt;">Third: Modify or increase the value of an element in the dictionary</span></strong> </p> </p>

<p>  <pre><span style="color: #008080;"> 1</span> <span style="color: #008000;">#</span><span style="color: #008000;">!/usr/bin/ PYTHON </span></p>
<p><span style="color: #008080;"> 2</span> <span style="color: #008000;">#</span><span style="color: #008000;">coding:utf-8</span></p>
<p><span style="color: #008080;"> 3</span> <span style="color: #800000;">'''</span></p>
<p><span style="color: #008080;"> 4</span> <span style="color: #800000;">Created on 2017-11-19</p>
<p></span><span style="color: #008080;"> 5</span> <span style="color: #800000;">@author: NiceCui</p>
<p></span><span style="color: #008080;"> 6</span> <span style="color: #800000;">'''</span></p>
<p><span style="color: #008080;"> 7</span> </p>
<p><span style="color: #008080;"> 8</span> example_dict = {<span style="color: #800000;">&quot;</span><span style="color: #800000;">a</span><span style="color: #800000;">&quot;</span>:1,<span style="color: #800000;">&quot;</span><span style="color: #800000;">b</span><span style="color: #800000;">&quot;</span>:2,<span style="color: #800000;">&quot;</span><span style="color: #800000;">c</span><span style="color: #800000;">&quot;</span>:3<span style="color: #000000;">}</p>
<p></span><span style="color: #008080;"> 9</span> </p>
<p><span style="color: #008080;">10</span> example_dict[<span style="color: #800000;">&quot;</span><span style="color: #800000;">a</span><span style="color: #800000;">&quot;</span>] = 10 <span style="color: #008000;">#</span><span style="color: #008000;">修改 键 &quot;a&quot; 的值为 10</span></p>
<p><span style="color: #008080;">11</span> </p>
<p><span style="color: #008080;">12</span> example_dict[<span style="color: #800000;">&quot;</span><span style="color: #800000;">b</span><span style="color: #800000;">&quot;</span>] = 20 <span style="color: #008000;">#</span><span style="color: #008000;">修改 键 &quot;b&quot; 的值为 20</span></p>
<p><span style="color: #008080;">13</span> </p>
<p><span style="color: #008080;">14</span> example_dict[<span style="color: #800000;">&quot;</span><span style="color: #800000;">d</span><span style="color: #800000;">&quot;</span>] = 4  <span style="color: #008000;">#</span><span style="color: #008000;">增加 键是 &quot;d&quot; 值是 4</span></p>
<p><span style="color: #008080;">15</span> </p>
<p><span style="color: #008080;">16</span> <span style="color: #0000ff;">print</span>(example_dict)    <span style="color: #008000;">#</span><span style="color: #008000;">输出 :{'a': 10, 'c': 3, 'b': 20, 'd': 4}</span></pre> </p>
<p> </div> </p>
<p> <p><span style="font-size: 16px;">构建一个新的空词典</span></p> </p>
<p> <div class="cnblogs_code"> </p>
<p>  <pre><span style="color: #008000;">#</span><span style="color: #008000;">!/usr/bin/ PYTHON </span><span style="color: #008000;"></p>
<p>#</span><span style="color: #008000;">coding:utf-8</span></p>
<p><span style="color: #800000;">'''</span><span style="color:#800000;"></p>
<p>Created on 2017-11-19</p>
<p>@author: NiceCui</p>
<p></span><span style="color: #800000;">'''</span><span style="color: #000000;"></p>
<p> </p>







<p>  <pre><span style="color: #008080;">1</span> <span style="color: #008000;">#</span><span style="color: #008000;">!/usr/bin/ PYTHON </span></p>
<p><span style="color: #008080;">2</span> <span style="color: #008000;">#</span><span style="color: #008000;">coding:utf-8</span></p>
<p><span style="color: #008080;">3</span> <span style="color: #800000;">'''</span></p>
<p><span style="color: #008080;">4</span> <span style="color: #800000;">Created on 2017-11-19</p>
<p></span><span style="color: #008080;">5</span> <span style="color: #800000;">@author: NiceCui</p>
<p></span><span style="color: #008080;">6</span> <span style="color: #800000;">'''</span></p>
<p><span style="color: #008080;">7</span> </p>
<p><span style="color: #008080;">8</span> </p>
<p><span style="color: #008080;">9</span> example_dict ={<span style="color: #800000;">&quot;</span><span style="color: #800000;">premium</span><span style="color: #800000;">&quot;</span>:0.2,<span style="color:#800000;">&quot;</span><span style="color: #800000;">tax</span><span style="color: #800000;">&quot;</span>:0.15}</pre> </p>
<p> </div> </p>
In the dictionary example, and in most application scenarios, we use strings as dictionary keys. But other types of data, such as arrays and boolean values, can also be used as keys for dictionaries, easy to learn, play to learn, and learn to be happy. </span></p> </p> <p> <p style="text-align: left;">&nbsp;</p></p>



<p></div></p></div>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326389041&siteId=291194637