Skip to content

Commit

Permalink
Site updated: 2023-10-18 16:56:47
Browse files Browse the repository at this point in the history
  • Loading branch information
zade23 committed Oct 18, 2023
1 parent b8c9d59 commit 94293cc
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 11 deletions.
59 changes: 56 additions & 3 deletions 2023/10/18/Python元组集合字典/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@


<title>Python元组集合字典 | ANdRoid&#39;s BLOG</title>
<meta property="og:type" content="article">
<meta name="description" content="1. 元组 1.1 元组的初始化 1.2 元组的解包 1.3 元组的其他操作 2. 集合 2.1 集合的初始化 2.2 集合的常用操作 2.3 使用for循环遍历集合 3. 字典 3.1 字典的初始化 3.2 字典的常用操作 3.3 使用for循环遍历字典 1. 元组元组跟 列表 类似,只是不支持动态添加、删除元素,以及不能修改元素。 1.1 元组的初始化元组需要用小括号括起来,">
<meta property="og:type" content="article">
<meta property="og:title" content="Python元组集合字典">
<meta property="og:url" content="https://zade23.github.io/2023/10/18/Python%E5%85%83%E7%BB%84%E9%9B%86%E5%90%88%E5%AD%97%E5%85%B8/index.html">
<meta property="og:site_name" content="ANdRoid&#39;s BLOG">
<meta property="og:description" content="1. 元组 1.1 元组的初始化 1.2 元组的解包 1.3 元组的其他操作 2. 集合 2.1 集合的初始化 2.2 集合的常用操作 2.3 使用for循环遍历集合 3. 字典 3.1 字典的初始化 3.2 字典的常用操作 3.3 使用for循环遍历字典 1. 元组元组跟 列表 类似,只是不支持动态添加、删除元素,以及不能修改元素。 1.1 元组的初始化元组需要用小括号括起来,">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-10-18T08:20:18.000Z">
<meta property="article:modified_time" content="2023-10-18T08:20:18.738Z">
<meta property="article:modified_time" content="2023-10-18T08:56:33.797Z">
<meta property="article:author" content="Android">
<meta name="twitter:card" content="summary">
<!-- Canonical links -->
Expand Down Expand Up @@ -356,7 +358,58 @@ <h1 class="article-title" itemprop="name">
</div>
<div class="article-entry marked-body" itemprop="articleBody">


<ul>
<li><a href="#1-%E5%85%83%E7%BB%84">1. 元组</a><ul>
<li><a href="#11-%E5%85%83%E7%BB%84%E7%9A%84%E5%88%9D%E5%A7%8B%E5%8C%96">1.1 元组的初始化</a></li>
<li><a href="#12-%E5%85%83%E7%BB%84%E7%9A%84%E8%A7%A3%E5%8C%85">1.2 元组的解包</a></li>
<li><a href="#13-%E5%85%83%E7%BB%84%E7%9A%84%E5%85%B6%E4%BB%96%E6%93%8D%E4%BD%9C">1.3 元组的其他操作</a></li>
</ul>
</li>
<li><a href="#2-%E9%9B%86%E5%90%88">2. 集合</a><ul>
<li><a href="#21-%E9%9B%86%E5%90%88%E7%9A%84%E5%88%9D%E5%A7%8B%E5%8C%96">2.1 集合的初始化</a></li>
<li><a href="#22-%E9%9B%86%E5%90%88%E7%9A%84%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C">2.2 集合的常用操作</a></li>
<li><a href="#23-%E4%BD%BF%E7%94%A8for%E5%BE%AA%E7%8E%AF%E9%81%8D%E5%8E%86%E9%9B%86%E5%90%88">2.3 使用<code>for</code>循环遍历集合</a></li>
</ul>
</li>
<li><a href="#3-%E5%AD%97%E5%85%B8">3. 字典</a><ul>
<li><a href="#31-%E5%AD%97%E5%85%B8%E7%9A%84%E5%88%9D%E5%A7%8B%E5%8C%96">3.1 字典的初始化</a></li>
<li><a href="#32-%E5%AD%97%E5%85%B8%E7%9A%84%E5%B8%B8%E7%94%A8%E6%93%8D%E4%BD%9C">3.2 字典的常用操作</a></li>
<li><a href="#33-%E4%BD%BF%E7%94%A8for%E5%BE%AA%E7%8E%AF%E9%81%8D%E5%8E%86%E5%AD%97%E5%85%B8">3.3 使用<code>for</code>循环遍历字典</a></li>
</ul>
</li>
</ul>
<hr>
<h3 id="1-元组"><a href="#1-元组" class="headerlink" title="1. 元组"></a>1. 元组</h3><p>元组跟 <u>列表</u> 类似,只是不支持动态添加、删除元素,以及不能修改元素。</p>
<h4 id="1-1-元组的初始化"><a href="#1-1-元组的初始化" class="headerlink" title="1.1 元组的初始化"></a>1.1 元组的初始化</h4><p>元组需要用小括号括起来,中间的元素用逗号隔开。</p>
<p><strong>注意</strong>:如果初始化只包含一个元素的元组,需要在该元素后添加逗号。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">a = () <span class="comment"># 初始化一个空元组</span></span><br><span class="line">b = (<span class="number">1</span>, <span class="number">2</span>) <span class="comment"># 含有2个整数的元组</span></span><br><span class="line">c = <span class="number">6</span>, <span class="string">&quot;Python&quot;</span>, <span class="number">3.14</span> <span class="comment"># 小括号可以省略,等价于(6, &quot;Python&quot;, 3.14)</span></span><br><span class="line">d = (<span class="number">5</span>,) <span class="comment"># 注意不能写成(5),(5)表示整数5</span></span><br><span class="line">e = <span class="number">5</span>, <span class="comment"># 等价于(5,)</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(a, b, c, d, e)</span><br></pre></td></tr></table></figure>

<h4 id="1-2-元组的解包"><a href="#1-2-元组的解包" class="headerlink" title="1.2 元组的解包"></a>1.2 元组的解包</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">t = <span class="number">12345</span>, <span class="number">54321</span>, <span class="string">&quot;Hello!&quot;</span> <span class="comment"># 初始化一个元组</span></span><br><span class="line">x, y, z = t <span class="comment"># 将元组解包,将元组内的三个值按顺序赋值给x、y、z</span></span><br><span class="line"><span class="built_in">print</span>(x, y, z)</span><br></pre></td></tr></table></figure>

<p>所以,判断语句中的交换操作,本质上是元组的解包:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">a, b = <span class="number">3</span>, <span class="number">4</span> <span class="comment"># 将元组(3, 4)解包,分别赋值给a、b</span></span><br><span class="line">a, b = b, a <span class="comment"># 将元组(b, a)解包,分别赋值给a、b</span></span><br></pre></td></tr></table></figure>

<p>同样地,函数中函数返回多个值,本质上也是返回了一个元组:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">calc</span>(<span class="params">x, y</span>):</span><br><span class="line"> <span class="keyword">return</span> x + y, x * y <span class="comment"># 等价于 return (x + y, x * y)</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">x, y = <span class="number">3</span>, <span class="number">4</span></span><br><span class="line">s, p = calc(x, y) <span class="comment"># 将(x + y, x * y)解包,分别赋值给s、p</span></span><br><span class="line"><span class="built_in">print</span>(s, p)</span><br></pre></td></tr></table></figure>

<h4 id="1-3-元组的其他操作"><a href="#1-3-元组的其他操作" class="headerlink" title="1.3 元组的其他操作"></a>1.3 元组的其他操作</h4><p>元组的下标访问元素、循环遍历、切片、加法和乘法运算等操作,都与列表相同。</p>
<h3 id="2-集合"><a href="#2-集合" class="headerlink" title="2. 集合"></a>2. 集合</h3><p>集合是Python中最常用的数据结构之一,用来存储不同元素。<br>注意,集合中的元素是无序的。</p>
<h4 id="2-1-集合的初始化"><a href="#2-1-集合的初始化" class="headerlink" title="2.1 集合的初始化"></a>2.1 集合的初始化</h4><p>创建集合用花括号或<code>set()</code>函数。注意:创建空集合只能用<code>set()</code>,不能用<code>&#123;&#125;</code>,因为<code>&#123;&#125;</code>创建的是空字典,会在下一小节里介绍字典。</p>
<p>集合常见的初始化方式:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">basket = &#123;<span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;orange&#x27;</span>, <span class="string">&#x27;apple&#x27;</span>, <span class="string">&#x27;pear&#x27;</span>, <span class="string">&#x27;orange&#x27;</span>, <span class="string">&#x27;banana&#x27;</span>&#125; <span class="comment"># 会自动去除重复元素</span></span><br><span class="line"><span class="built_in">print</span>(basket) <span class="comment"># 重复的元素已经去除了</span></span><br><span class="line"></span><br><span class="line">s = <span class="built_in">set</span>() <span class="comment"># 初始化一个空列表</span></span><br><span class="line"><span class="built_in">print</span>(s)</span><br><span class="line"></span><br><span class="line">a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">3</span>, <span class="number">1</span>]</span><br><span class="line">b = <span class="built_in">set</span>(a) <span class="comment"># 将列表转化成集合,一般是为了去重。</span></span><br><span class="line">c = <span class="built_in">list</span>(b) <span class="comment"># 将集合转化回列表</span></span><br><span class="line"><span class="built_in">print</span>(b, c)</span><br><span class="line"></span><br><span class="line">x = <span class="string">&quot;abracadabra&quot;</span></span><br><span class="line">a = <span class="built_in">set</span>(x) <span class="comment"># 将字符串中的每个字符存到集合中</span></span><br><span class="line">b = <span class="built_in">str</span>(a) <span class="comment"># 注意,这里并不能将集合转化回原字符串,而是用格式化表示集合中的内容</span></span><br><span class="line"><span class="built_in">print</span>(a, b)</span><br></pre></td></tr></table></figure>

<h4 id="2-2-集合的常用操作"><a href="#2-2-集合的常用操作" class="headerlink" title="2.2 集合的常用操作"></a>2.2 集合的常用操作</h4><p>假设<code>a</code>表示一个集合。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">len</span>(a) 返回集合中包含的元素数量。</span><br><span class="line">a.add(x) 在集合中添加一个元素。</span><br><span class="line">a.remove(x) 删除集合中的x,如果x不存在,则报异常。</span><br><span class="line">a.discard(x) 删除集合中的x,如果x不存在,则不进行任何操作。</span><br><span class="line">x <span class="keyword">in</span> a 判断x是否在a中。</span><br></pre></td></tr></table></figure>

<p>例如:</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">a = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">len</span>(a)) <span class="comment"># 输出3</span></span><br><span class="line"></span><br><span class="line">a.add(<span class="number">4</span>)</span><br><span class="line"><span class="built_in">print</span>(a) <span class="comment"># 输出 &#123;1, 2, 3, 4&#125;,注意集合中的元素是无序的。</span></span><br><span class="line"></span><br><span class="line">a.remove(<span class="number">2</span>)</span><br><span class="line"><span class="built_in">print</span>(a) <span class="comment"># 输出 &#123;1, 3, 4&#125;</span></span><br><span class="line"></span><br><span class="line">a.remove(<span class="number">5</span>) <span class="comment"># 因为5不存在,所以会报异常</span></span><br><span class="line"></span><br><span class="line">a.discard(<span class="number">5</span>) <span class="comment"># 因为5不存在,所以不进行任何操作</span></span><br><span class="line"><span class="built_in">print</span>(a) <span class="comment"># &#123;1, 3, 4&#125;</span></span><br></pre></td></tr></table></figure>

<h4 id="2-3-使用for循环遍历集合"><a href="#2-3-使用for循环遍历集合" class="headerlink" title="2.3 使用for循环遍历集合"></a>2.3 使用<code>for</code>循环遍历集合</h4><p>类似于列表,集合也可以用<code>for ... in ...</code>的形式遍历。例如:</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">a = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> x <span class="keyword">in</span> a: <span class="comment"># 循环遍历整个集合</span></span><br><span class="line"> <span class="built_in">print</span>(x, end=<span class="string">&#x27; &#x27;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="3-字典"><a href="#3-字典" class="headerlink" title="3. 字典"></a>3. 字典</h3><p>字典是Python中最常用的数据结构之一,用来存储映射关系。<br>注意,字典中的元素是无序的。</p>
<p>不同于列表,字典是以<code>key</code>进行索引的,可以将每个<code>key</code>映射到某个<code>value</code><code>key</code>可以是任何不可变类型,常用可以作为<code>key</code>的类型有数字和字符串。列表因为是可变的,所以不能作为<code>key</code><code>value</code>可以是任意类型。</p>
<h4 id="3-1-字典的初始化"><a href="#3-1-字典的初始化" class="headerlink" title="3.1 字典的初始化"></a>3.1 字典的初始化</h4><h4 id="3-2-字典的常用操作"><a href="#3-2-字典的常用操作" class="headerlink" title="3.2 字典的常用操作"></a>3.2 字典的常用操作</h4><h4 id="3-3-使用for循环遍历字典"><a href="#3-3-使用for循环遍历字典" class="headerlink" title="3.3 使用for循环遍历字典"></a>3.3 使用<code>for</code>循环遍历字典</h4>

</div>
<div class="article-footer">
Expand Down
Loading

0 comments on commit 94293cc

Please sign in to comment.