1048 lines
549 KiB
HTML
1048 lines
549 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
<html><head><title>数据结构</title><meta charSet="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="数据结构"/><meta property="og:description" content="Python 数据结构."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../../static/icon.png"/><meta name="description" content="Python 数据结构."/><meta name="generator" content="Quartz"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><script async src="https://umami.7wate.com/script.js" data-website-id="c061efdc-95dd-4d21-9d04-a1ffda0a85b9"></script><script>
|
|||
|
var _hmt = _hmt || [];
|
|||
|
(function() {
|
|||
|
var hm = document.createElement("script");
|
|||
|
hm.src = "https://hm.baidu.com/hm.js?94d8ccb156eb7c65abf317e6e01cdba9";
|
|||
|
var s = document.getElementsByTagName("script")[0];
|
|||
|
s.parentNode.insertBefore(hm, s);
|
|||
|
})();
|
|||
|
</script><script async src="https://www.googletagmanager.com/gtag/js?id=G-MHMEL0F832"></script><script>
|
|||
|
(function() {
|
|||
|
window.dataLayer = window.dataLayer || [];
|
|||
|
function gtag() {
|
|||
|
window.dataLayer.push(arguments);
|
|||
|
}
|
|||
|
gtag('js', new Date());
|
|||
|
gtag('config', 'G-MHMEL0F832');
|
|||
|
})();
|
|||
|
</script><link href="../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&family=Schibsted Grotesk:wght@400;700&family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&display=swap" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch(`../../../../static/contentIndex.json`).then(data => data.json())</script></head><body data-slug="Tech/programming-language/Python/入门/数据结构"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h1 class="page-title "><a href="../../../..">📚 X·Eden</a></h1><div class="spacer mobile-only"></div><div class="search "><div id="search-icon"><p>Search</p><div></div><svg tabIndex="0" aria-labelledby="title desc" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title id="title">Search</title><desc id="desc">Search</desc><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></div><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="Search for something" placeholder="Search for something"/><div id="results-container"></div></div></div></div><div class="darkmode "><input class="toggle" id="darkmode-toggle" type="checkbox" tabIndex="-1"/><label id="toggle-label-light" for="darkmode-toggle" tabIndex="-1"><svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35;" xmlSpace="preserve"><title>Light mode</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561,26.439C28.289,26.168,27.914,26,27.5,26c-0.828,0-1.5,0.672-1.5,1.5c0,0.414,0.168,0.789,0.439,1.06l2,2 C28.711,30.832,29.086,31,29.5,31c0.828,0,1.5-0.672,1.5-1.5c0-0.414-0.168-0.789-0.439-1.061L28.561,26.439z M17.5,29 c-0.829,0-1.5,0.672-1.5,1.5v3c0,0.828,0.671,1.5,1.5,1.5s1.5-0.672,1.5-1.5v-3C19,29.672,18.329,29,17.5,29z M17.5,7 C11.71,7,7,11.71,7,17.5S11.71,28,17.5,28S28,23.29,28,17.5S23.29,7,17.5,7z M17.5,25c-4.136,0-7.5-3.364-7.5-7.5 c0-4.136,3.364-7.5,7.5-7.5c4.136,0,7.5,3.364,7.5,7.5C25,21.636,21.636,25,17.5,25z"></path></svg></label><label id="toggle-label-dark" for="darkmode-toggle" tabIndex="-1"><svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" version="1.1" id="nightIcon" x="0px" y="0px" viewBox="0 0 100 100" style="enable-background='new 0 0 100 100'" xmlSpace="preserve"><title>Dark mode</title><path d="M96.76,66.458c-0.853-0.852-2.15-1.064-3.23-0.534c-6.063,2.991-12.858,4.571-19.655,4.571 C62.022,70.495,50.88,65.88,42.5,57.5C29.043,44.043,25.658,23.536,34.076,6.47c0.532-1.08,0.318-2.379-0.534-3.23 c-0.851-0.852-2.15-1.064-3.23-0.534c-4.918,2.427-9.375,5.619-13.246,9.491c-9.447,9.447-14.65,22.008-14.65
|
|||
|
<p>Python 的数值数据类型是用来存储数值的。一旦创建,<strong>数值对象的身份和类型是不可改变的</strong>。如果你改变了数值对象的值,Python 将创建一个新的数值对象。Python 支持三种不同的数值类型:</p>
|
|||
|
<ul>
|
|||
|
<li><strong>整型 (int)</strong>:被用来表示整数,包括正整数和负整数,不带小数点。在 Python 3 中,整数的长度是无限的,可以视为 Python 2 中的 Long 类型。此外,布尔类型(bool)实际上是整型的一种特殊形式,其中 True 和 False 分别被表示为 1 和 0。</li>
|
|||
|
<li><strong>浮点型 (float)</strong>:用来表示实数,由整数部分和小数部分组成。浮点型还可以使用科学计数法表示。例如,2.5e2 表示的是 2.5 乘以 10 的平方,等于 250。</li>
|
|||
|
<li><strong>复数 (complex)</strong>:复数由实部和虚部构成,表示形式可以是 a + bj,或者 complex(a,b)。在这里,实部 a 和虚部 b 都是浮点型。</li>
|
|||
|
</ul>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">num1 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">-</span><span style="color:var(--shiki-token-constant);">100</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 整数</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">num2 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">200.01</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 浮点数</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">num3 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">0x</span><span style="color:var(--shiki-token-constant);">A0F</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 十六进制</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">num4 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">0o</span><span style="color:var(--shiki-token-constant);">37</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 八进制</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 引入库</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">import</span><span style="color:var(--shiki-color-text);"> math</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">import</span><span style="color:var(--shiki-color-text);"> random</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 数学常量 PI 和 e</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">pi </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> math</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-color-text);">pi</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">e </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> math</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-color-text);">e</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 绝对值</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">abs_val </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">abs</span><span style="color:var(--shiki-token-punctuation);">(num1)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 向上取整</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">ceil_val </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> math</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">ceil</span><span style="color:var(--shiki-token-punctuation);">(num2)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 返回最大数</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">max_val </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">max</span><span style="color:var(--shiki-token-punctuation);">(num1, num2)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># x**y 运算后的值</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">pow_val </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">pow</span><span style="color:var(--shiki-token-punctuation);">(num1, num2)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 随机数</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">random_val </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> random</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">random</span><span style="color:var(--shiki-token-punctuation);">()</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># x弧度的正弦值。</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">sin_val </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> math</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">sin</span><span style="color:var(--shiki-token-punctuation);">(pi</span><span style="color:var(--shiki-token-keyword);">/</span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-token-punctuation);">)</span></span></code></pre></div>
|
|||
|
<h2 id="字符串">字符串<a aria-hidden="true" tabindex="-1" href="#字符串" class="internal"> §</a></h2>
|
|||
|
<p>字符串在计算机科学和编程中非常重要。在 Python 程序中,如果我们把单个或多个字符用单引号或者双引号包围起来,我们就创建了一个字符串。</p>
|
|||
|
<h3 id="定义">定义<a aria-hidden="true" tabindex="-1" href="#定义" class="internal"> §</a></h3>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 在 Python 中,可以使用单引号(')和双引号(")来定义字符串,它们的使用方式完全相同。</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">s1 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'hello, world!'</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">s2 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"hello, world!"</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用三个单引号(''')或者三个双引号(""")可以创建一个多行字符串。</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">s3 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"""</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-string-expression);">hello, </span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-string-expression);">world!</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-string-expression);">"""</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 反斜杠(\)可以用来在字符串中插入特殊字符,这被称为转义字符。</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">s4 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'\n\t\141\u9a86\u660a'</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 在字符串前加上字符 r 可以阻止反斜杠的转义效果。</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">s5 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">r</span><span style="color:var(--shiki-token-string-expression);">'\n\\hello, world!\\\n'</span><span style="color:var(--shiki-color-text);"> </span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 通过将字符串级联来创建新的字符串。</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">s6 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"this "</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"is "</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"string"</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 字符串是不可变的,这意味着你不能改变一个字符串的内容。</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">s6</span><span style="color:var(--shiki-token-punctuation);">[</span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-token-punctuation);">]</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"c"</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 这将引发一个错误</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 字符串可以通过索引进行访问,其中 0 是第一个字符的索引,-1 是最后一个字符的索引。</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">s1 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'hello, world!'</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(s1[</span><span style="color:var(--shiki-token-constant);">0</span><span style="color:var(--shiki-token-punctuation);">])</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:h</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(s1[</span><span style="color:var(--shiki-token-keyword);">-</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">])</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:!</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># Python也支持字符串的切片操作,你可以使用这种方式来获取字符串的一部分。</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">s1 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'hello, world!'</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(s1[</span><span style="color:var(--shiki-token-constant);">0</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-token-constant);">5</span><span style="color:var(--shiki-token-punctuation);">])</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:hello</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># ===== 字符串切片示意图 =====</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> L </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> e </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> t </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> t </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> e </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> r </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> s </span><span style="color:var(--shiki-token-keyword);">|</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);">--</span><span style="color:var(--shiki-token-keyword);">-|</span><span style="color:var(--shiki-color-text);">--</span><span style="color:var(--shiki-token-keyword);">-|</span><span style="color:var(--shiki-color-text);">--</span><span style="color:var(--shiki-token-keyword);">-|</span><span style="color:var(--shiki-color-text);">--</span><span style="color:var(--shiki-token-keyword);">-|</span><span style="color:var(--shiki-color-text);">--</span><span style="color:var(--shiki-token-keyword);">-|</span><span style="color:var(--shiki-color-text);">--</span><span style="color:var(--shiki-token-keyword);">-|</span><span style="color:var(--shiki-color-text);">--</span><span style="color:var(--shiki-token-keyword);">-|</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">0</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">5</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">6</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">|-</span><span style="color:var(--shiki-token-constant);">7</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|-</span><span style="color:var(--shiki-token-constant);">6</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|-</span><span style="color:var(--shiki-token-constant);">5</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|-</span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|-</span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|-</span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|-</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|</span></span></code></pre></div>
|
|||
|
<h3 id="转义">转义<a aria-hidden="true" tabindex="-1" href="#转义" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th align="left">转义字符</th><th align="left">描述</th><th align="left">示例</th></tr></thead><tbody><tr><td align="left"><code>\\</code></td><td align="left">反斜杠符号</td><td align="left"><code>print("\\")</code> 输出:<code>\</code></td></tr><tr><td align="left"><code>\'</code></td><td align="left">单引号</td><td align="left"><code>print('\'')</code> 输出:<code>'</code></td></tr><tr><td align="left"><code>\"</code></td><td align="left">双引号</td><td align="left"><code>print("\"")</code> 输出:<code>"</code></td></tr><tr><td align="left"><code>\n</code></td><td align="left">换行</td><td align="left"><code>print("\n")</code> 输出一个新行</td></tr><tr><td align="left"><code>\r</code></td><td align="left">回车</td><td align="left"><code>print("Hello\rWorld!")</code> 输出:<code>World!</code></td></tr><tr><td align="left"><code>\t</code></td><td align="left">横向制表符(Tab)</td><td align="left"><code>print("Hello\tWorld!")</code> 输出:<code>Hello World!</code></td></tr><tr><td align="left"><code>\b</code></td><td align="left">退格 (Backspace)</td><td align="left"><code>print("Hello \bWorld!")</code> 输出:<code>Hello World!</code></td></tr><tr><td align="left"><code>\f</code></td><td align="left">换页</td><td align="left"><code>print("Hello \fWorld!")</code> 输出:<code>Hello World!</code></td></tr><tr><td align="left"><code>\a</code></td><td align="left">响铃</td><td align="left"><code>print("\a")</code> 执行后电脑有响声</td></tr><tr><td align="left"><code>\000</code></td><td align="left">空</td><td align="left"><code>print("\000")</code> 输出:“</td></tr><tr><td align="left"><code>\v</code></td><td align="left">纵向制表符</td><td align="left"><code>print("Hello \vWorld!")</code> 输出:<code>Hello World!</code></td></tr><tr><td align="left"><code>\other</code></td><td align="left">其他的字符以普通格式输出</td><td align="left"></td></tr><tr><td align="left"><code>\xhh</code></td><td align="left">二进制数,代表一个 ASCII 字符</td><td align="left"><code>print("\x48\x65\x6c\x6c\x6f")</code> 输出:<code>Hello</code></td></tr><tr><td align="left"><code>\ooo</code></td><td align="left">八进制数,代表一个 ASCII 字符</td><td align="left"><code>print("\141\142\143")</code> 输出:<code>abc</code></td></tr><tr><td align="left"><code>\uhhhh</code></td><td align="left">16 进制数,代表一个 Unicode 字符</td><td align="left"><code>print("\u6211\u4eec")</code> 输出:<code>我们</code></td></tr></tbody></table>
|
|||
|
<h3 id="运算">运算<a aria-hidden="true" tabindex="-1" href="#运算" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th align="left">运算符</th><th align="left">描述</th><th align="left">示例</th></tr></thead><tbody><tr><td align="left"><code>+</code></td><td align="left">连接字符串</td><td align="left"><code>"Hello " + "World!"</code> 输出:<code>Hello World!</code></td></tr><tr><td align="left"><code>*</code></td><td align="left">重复字符串</td><td align="left"><code>"Hello " * 3</code> 输出:<code>Hello Hello Hello</code></td></tr><tr><td align="left"><code>[]</code></td><td align="left">索引字符串</td><td align="left"><code>"Hello"[1]</code> 输出:<code>e</code></td></tr><tr><td align="left"><code>[:]</code></td><td align="left">切片字符串</td><td align="left"><code>"Hello"[1:4]</code> 输出:<code>ell</code></td></tr><tr><td align="left"><code>in</code></td><td align="left">成员运算符 - 如果字符串中包含给定的字符返回 True</td><td align="left"><code>'H' in 'Hello'</code> 输出:<code>True</code></td></tr><tr><td align="left"><code>not in</code></td><td align="left">成员运算符 - 如果字符串中不包含给定的字符返回 True</td><td align="left"><code>'H' not in 'Hello'</code> 输出:<code>False</code></td></tr><tr><td align="left"><code>%</code></td><td align="left">格式化字符串</td><td align="left"><code>"Hello, %s" % 'World!'</code> 输出:<code>Hello, World!</code></td></tr><tr><td align="left"><code>f-string</code></td><td align="left">格式化字符串的新方式(Python 3.6+)</td><td align="left"><code>f"Hello, {'World!'}</code> 输出:<code>Hello, World!</code></td></tr></tbody></table>
|
|||
|
<h3 id="f-strings"><code>f-strings</code><a aria-hidden="true" tabindex="-1" href="#f-strings" class="internal"> §</a></h3>
|
|||
|
<p>在 Python 3.6 及以后的版本中,格式化字符串引入了更为简洁的方式,即使用 <code>f</code> 字符作为字符串的前缀,创建所谓的格式化字符串字面值(f-string)。通过 f-string,可以在字符串中直接插入变量值或表达式,并可以通过冒号和数字来指定输出宽度等格式化选项。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># {a:10} 表示将变量 a 插入到字符串中,并占用 10 个字符的宽度。不足 10 个字符,将使用空格进行填充。</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">a</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> b </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">5</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">10</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-keyword);">f</span><span style="color:var(--shiki-token-string-expression);">'</span><span style="color:var(--shiki-token-constant);">{</span><span style="color:var(--shiki-token-punctuation);">a</span><span style="color:var(--shiki-token-keyword);">:10</span><span style="color:var(--shiki-token-constant);">}</span><span style="color:var(--shiki-token-string-expression);"> * </span><span style="color:var(--shiki-token-constant);">{</span><span style="color:var(--shiki-token-punctuation);">b</span><span style="color:var(--shiki-token-keyword);">:10</span><span style="color:var(--shiki-token-constant);">}</span><span style="color:var(--shiki-token-string-expression);"> = </span><span style="color:var(--shiki-token-constant);">{</span><span style="color:var(--shiki-token-punctuation);">a </span><span style="color:var(--shiki-token-keyword);">*</span><span style="color:var(--shiki-token-punctuation);"> b</span><span style="color:var(--shiki-token-constant);">}</span><span style="color:var(--shiki-token-string-expression);">'</span><span style="color:var(--shiki-token-punctuation);">)</span></span></code></pre></div>
|
|||
|
<h4 id="pep-701-的更新">PEP 701 的更新<a aria-hidden="true" tabindex="-1" href="#pep-701-的更新" class="internal"> §</a></h4>
|
|||
|
<p>在 Python 3.11 版本中,f-strings 还存在一些限制,导致在某些情况下使用起来不够灵活。PEP 701 对 f-strings 进行了一些更新,解除了一些限制,使得 f-strings 可以更灵活地使用,包括以下方面的更新:</p>
|
|||
|
<p>在 Python 3.12 版本中,对 f-strings 进行了更新,解除了之前的一些限制,使得 f-strings 更加灵活和强大。更新主要包括以下方面:</p>
|
|||
|
<h5 id="引号重用">引号重用<a aria-hidden="true" tabindex="-1" href="#引号重用" class="internal"> §</a></h5>
|
|||
|
<p>在 Python 3.11 中,f-string 的表达式组件使用与包含 f-string 相同的引号会导致 SyntaxError。而在 Python 3.12 中,可以重用引号,不再限制引号的使用。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">songs </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> [</span><span style="color:var(--shiki-token-string-expression);">'Take me back to Eden'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'Alkaline'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'Ascensionism'</span><span style="color:var(--shiki-color-text);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">result1 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">f</span><span style="color:var(--shiki-token-string-expression);">"This is the playlist: </span><span style="color:var(--shiki-token-constant);">{</span><span style="color:var(--shiki-token-string-expression);">', '</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">join</span><span style="color:var(--shiki-token-punctuation);">(songs)</span><span style="color:var(--shiki-token-constant);">}</span><span style="color:var(--shiki-token-string-expression);">"</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(result1)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 输出:'This is the playlist: Take me back to Eden, Alkaline, Ascensionism'</span></span></code></pre></div>
|
|||
|
<h5 id="多行表达式和注释">多行表达式和注释<a aria-hidden="true" tabindex="-1" href="#多行表达式和注释" class="internal"> §</a></h5>
|
|||
|
<p>在 Python 3.11 中,f-string 的表达式必须在一行内定义,即使外部表达式可以跨多行,这会使得代码可读性变差。而在 Python 3.12 中,可以定义跨多行的表达式,并且可以包含注释,提高了代码的可读性。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">result2 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">f</span><span style="color:var(--shiki-token-string-expression);">"This is the playlist: </span><span style="color:var(--shiki-token-constant);">{</span><span style="color:var(--shiki-token-string-expression);">', '</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">join</span><span style="color:var(--shiki-token-punctuation);">([</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-punctuation);"> </span><span style="color:var(--shiki-token-string-expression);">'Take me back to Eden'</span><span style="color:var(--shiki-token-punctuation);">, </span><span style="color:var(--shiki-token-comment);"># My, my, those eyes like fire</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-punctuation);"> </span><span style="color:var(--shiki-token-string-expression);">'Alkaline'</span><span style="color:var(--shiki-token-punctuation);">, </span><span style="color:var(--shiki-token-comment);"># Not acid nor alkaline</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-punctuation);"> </span><span style="color:var(--shiki-token-string-expression);">'Ascensionism'</span><span style="color:var(--shiki-token-punctuation);"> </span><span style="color:var(--shiki-token-comment);"># Take to the broken skies at last</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-punctuation);">])</span><span style="color:var(--shiki-token-constant);">}</span><span style="color:var(--shiki-token-string-expression);">"</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(result2)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 输出:'This is the playlist: Take me back to Eden, Alkaline, Ascensionism'</span></span></code></pre></div>
|
|||
|
<h5 id="反斜杠和-unicode-字符">反斜杠和 Unicode 字符<a aria-hidden="true" tabindex="-1" href="#反斜杠和-unicode-字符" class="internal"> §</a></h5>
|
|||
|
<p>在 Python 3.11 中,f-string 的表达式不能包含任何反斜杠字符,也不能包含 Unicode 转义序列。而在 Python 3.12 中,可以包含反斜杠和 Unicode 字符,使得表达式更加灵活。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">songs </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> [</span><span style="color:var(--shiki-token-string-expression);">'Take me back to Eden'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'Alkaline'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'Ascensionism'</span><span style="color:var(--shiki-color-text);">]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">result3 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">f</span><span style="color:var(--shiki-token-string-expression);">"This is the playlist: </span><span style="color:var(--shiki-token-constant);">{</span><span style="color:var(--shiki-token-string-expression);">'\n'</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">join</span><span style="color:var(--shiki-token-punctuation);">(songs)</span><span style="color:var(--shiki-token-constant);">}</span><span style="color:var(--shiki-token-string-expression);">"</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(result3)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 输出:</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># This is the playlist: Take me back to Eden</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># Alkaline</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># Ascensionism</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">result4 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">f</span><span style="color:var(--shiki-token-string-expression);">"This is the playlist: </span><span style="color:var(--shiki-token-constant);">{</span><span style="color:var(--shiki-token-string-expression);">'\N{BLACK HEART SUIT}'</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">join</span><span style="color:var(--shiki-token-punctuation);">(songs)</span><span style="color:var(--shiki-token-constant);">}</span><span style="color:var(--shiki-token-string-expression);">"</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(result4)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 输出:'This is the playlist: Take me back to Eden♥Alkaline♥Ascensionism'</span></span></code></pre></div>
|
|||
|
<p>以上代码展示了 Python 3.12 中 f-strings 的更新。可以看到,我们可以在 f-string 中使用引号重用,多行表达式和注释,以及包含反斜杠和 Unicode 字符的表达式,这些更新使得 f-strings 更加灵活和强大,更方便地进行字符串格式化。同时,通过使用 PEG 解析器实现的更新,使得错误消息更加准确,有助于代码调试。</p>
|
|||
|
<h3 id="常用方法">常用方法<a aria-hidden="true" tabindex="-1" href="#常用方法" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th align="left">函数</th><th align="left">描述</th><th align="left">示例</th></tr></thead><tbody><tr><td align="left">len(str)</td><td align="left">返回字符串长度</td><td align="left"><code>len('hello, world!')</code> 输出结果:13</td></tr><tr><td align="left">str.capitalize()</td><td align="left">返回首字母大写的字符串</td><td align="left"><code>'hello, world!'.capitalize()</code> 输出结果:Hello, world!</td></tr><tr><td align="left">str.title()</td><td align="left">返回每个单词首字母大写的字符串</td><td align="left"><code>'hello, world!'.title()</code> 输出结果:Hello, World!</td></tr><tr><td align="left">str.upper()</td><td align="left">返回字符串变大写后的拷贝</td><td align="left"><code>'hello, world!'.upper()</code> 输出结果:HELLO, WORLD!</td></tr><tr><td align="left">str.find(sub)</td><td align="left">查找子串在字符串中的起始位置,未找到返回 -1</td><td align="left"><code>'hello, world!'.find('or')</code> 输出结果:8</td></tr><tr><td align="left">str.startswith(prefix)</td><td align="left">检查字符串是否以指定的前缀开头</td><td align="left"><code>'hello, world!'.startswith('He')</code> 输出结果:False</td></tr><tr><td align="left">str.endswith(suffix)</td><td align="left">检查字符串是否以指定的后缀结尾</td><td align="left"><code>'hello, world!'.endswith('!')</code> 输出结果:True</td></tr><tr><td align="left">str.center(width, fillchar)</td><td align="left">将字符串以指定宽度居中并在两侧填充指定字符</td><td align="left"><code>'hello'.center(10, '*')</code> 输出结果:<em><strong>hello</strong></em></td></tr><tr><td align="left">str.rjust(width, fillchar)</td><td align="left">将字符串以指定宽度靠右并在左侧填充指定字符</td><td align="left"><code>'hello'.rjust(10, '*')</code> 输出结果:*****hello</td></tr><tr><td align="left">str.isdigit()</td><td align="left">检查字符串是否由数字构成</td><td align="left"><code>'abc123456'.isdigit()</code> 输出结果:False</td></tr><tr><td align="left">str.isalpha()</td><td align="left">检查字符串是否由字母构成</td><td align="left"><code>'abc123456'.isalpha()</code> 输出结果:False</td></tr><tr><td align="left">str.isalnum()</td><td align="left">检查字符串是否由字母和数字构成</td><td align="left"><code>'abc123456'.isalnum()</code> 输出结果:True</td></tr><tr><td align="left">str.strip()</td><td align="left">返回修剪左右两侧空格之后的字符串</td><td align="left"><code>' jackfrued@126.com '.strip()</code> 输出结果:’<a href="mailto:jackfrued@126.com" class="external">jackfrued@126.com</a>’</td></tr></tbody></table>
|
|||
|
<h2 id="列表">列表<a aria-hidden="true" tabindex="-1" href="#列表" class="internal"> §</a></h2>
|
|||
|
<p>列表(<code>list</code>)是一种结构化的、非标量类型,它是值的有序序列,每个值都可以通过索引进行标识,定义列表可以将列表的元素放在 <code>[]</code> 中,多个元素用 <code>,</code> 进行分隔,可以使用 <code>for</code> 循环对列表元素进行遍历,也可以使用 <code>[]</code> 或 <code>[:]</code> 运算符取出列表中的一个或多个元素。</p>
|
|||
|
<h3 id="定义-1">定义<a aria-hidden="true" tabindex="-1" href="#定义-1" class="internal"> §</a></h3>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 列表定义</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">list1 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> [ </span><span style="color:var(--shiki-token-string-expression);">'abcd'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">786</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2.23</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'runoob'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">70.2</span><span style="color:var(--shiki-color-text);"> ]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">tinylist </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> [</span><span style="color:var(--shiki-token-constant);">123</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'runoob'</span><span style="color:var(--shiki-color-text);">]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 输出完整列表</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list1)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出结果:['abcd', 786, 2.23, 'runoob', 70.2]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 输出列表第一个元素</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list1[</span><span style="color:var(--shiki-token-constant);">0</span><span style="color:var(--shiki-token-punctuation);">])</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出结果:'abcd'</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 从第二个开始输出到第三个元素</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list1[</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-token-punctuation);">])</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出结果:[786, 2.23]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 输出从第三个元素开始的所有元素</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list1[</span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-token-punctuation);">:])</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出结果:[2.23, 'runoob', 70.2]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 输出两次列表</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(tinylist </span><span style="color:var(--shiki-token-keyword);">*</span><span style="color:var(--shiki-token-punctuation);"> </span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出结果:[123, 'runoob', 123, 'runoob']</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 连接列表</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list1 </span><span style="color:var(--shiki-token-keyword);">+</span><span style="color:var(--shiki-token-punctuation);"> tinylist)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出结果:['abcd', 786, 2.23, 'runoob', 70.2, 123, 'runoob']</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 通过循环用下标遍历列表元素</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> index </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">range</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-function);">len</span><span style="color:var(--shiki-token-punctuation);">(list1)):</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list1[index])</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 通过for 循环遍历列表元素</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> elem </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> list1</span><span style="color:var(--shiki-token-punctuation);">:</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(elem)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 通过 enumerate 函数处理列表之后再遍历可以同时获得元素索引和值</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> index</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> elem </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">enumerate</span><span style="color:var(--shiki-token-punctuation);">(list1):</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(index, elem)</span></span></code></pre></div>
|
|||
|
<h3 id="运算-1">运算<a aria-hidden="true" tabindex="-1" href="#运算-1" class="internal"> §</a></h3>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 创建列表</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">list1 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> [</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-color-text);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">list2 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> [</span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">5</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">6</span><span style="color:var(--shiki-color-text);">]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 连接列表,返回一个新的列表</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">list3 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> list1 </span><span style="color:var(--shiki-token-keyword);">+</span><span style="color:var(--shiki-color-text);"> list2</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list3)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:[1, 2, 3, 4, 5, 6]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 复制列表,返回一个新的列表</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">list4 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> list1 </span><span style="color:var(--shiki-token-keyword);">*</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">3</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list4)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:[1, 2, 3, 1, 2, 3, 1, 2, 3]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 计算元素个数</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">length </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">len</span><span style="color:var(--shiki-token-punctuation);">(list1)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(length)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:3</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 判断元素是否在列表中</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">result </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> list1</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(result)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:False</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">result </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> list1</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(result)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:True</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 访问元素,可以使用索引,索引从0开始</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">elem </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> list1</span><span style="color:var(--shiki-token-punctuation);">[</span><span style="color:var(--shiki-token-constant);">0</span><span style="color:var(--shiki-token-punctuation);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(elem)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:1</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 切片操作</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">list5 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> list1</span><span style="color:var(--shiki-token-punctuation);">[</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-token-punctuation);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list5)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:[2, 3]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 切片时可以指定步长</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">list6 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> list1</span><span style="color:var(--shiki-token-punctuation);">[</span><span style="color:var(--shiki-token-constant);">0</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-token-punctuation);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list6)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:[1, 3]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 切片时可以省略开始或结束索引</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">list7 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> list1</span><span style="color:var(--shiki-token-punctuation);">[:</span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-token-punctuation);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">list8 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> list1</span><span style="color:var(--shiki-token-punctuation);">[</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">:]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list7)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:[1, 2]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list8)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:[2, 3]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 反向切片</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">list9 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> list1</span><span style="color:var(--shiki-token-punctuation);">[::</span><span style="color:var(--shiki-token-keyword);">-</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list9)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:[3, 2, 1]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 修改元素值</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">list1</span><span style="color:var(--shiki-token-punctuation);">[</span><span style="color:var(--shiki-token-constant);">0</span><span style="color:var(--shiki-token-punctuation);">]</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">100</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list1)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:[100, 2, 3]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 列表末尾添加元素</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">list1</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">append</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list1)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:[100, 2, 3, 4]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 列表末尾删除元素</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">list1</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">pop</span><span style="color:var(--shiki-token-punctuation);">()</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list1)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:[100, 2, 3]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 指定位置插入元素</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">list1</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">insert</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">, </span><span style="color:var(--shiki-token-constant);">200</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list1)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:[100, 200, 2, 3]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 删除指定位置的元素</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">del</span><span style="color:var(--shiki-color-text);"> list1</span><span style="color:var(--shiki-token-punctuation);">[</span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-token-punctuation);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list1)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:[100, 200, 3]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 清空列表</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">list1</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">clear</span><span style="color:var(--shiki-token-punctuation);">()</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(list1)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:[]</span></span></code></pre></div>
|
|||
|
<h3 id="常用方法-1">常用方法<a aria-hidden="true" tabindex="-1" href="#常用方法-1" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th align="left">函数</th><th align="left">描述</th></tr></thead><tbody><tr><td align="left"><code>len(list)</code></td><td align="left">返回列表元素的个数。</td></tr><tr><td align="left"><code>list.append(x)</code></td><td align="left">在列表末尾添加元素 <code>x</code>。</td></tr><tr><td align="left"><code>list.insert(i, x)</code></td><td align="left">在指定位置 <code>i</code> 前插入元素 <code>x</code>。</td></tr><tr><td align="left"><code>list.extend(iterable)</code></td><td align="left">将可迭代对象中的元素逐个添加到列表。</td></tr><tr><td align="left"><code>list.remove(x)</code></td><td align="left">删除列表中第一个值为 <code>x</code> 的元素。</td></tr><tr><td align="left"><code>list.pop([i])</code></td><td align="left">移除并返回列表中指定位置 <code>i</code> 处的元素。如果未指定索引,默认删除并返回最后一个元素。</td></tr><tr><td align="left"><code>list.clear()</code></td><td align="left">清空列表中的所有元素。</td></tr><tr><td align="left"><code>list.index(x[, start[, end]])</code></td><td align="left">返回列表中第一个值为 <code>x</code> 的元素的索引。如果未找到该元素,会引发 <code>ValueError</code> 异常。</td></tr><tr><td align="left"><code>list.count(x)</code></td><td align="left">返回元素 <code>x</code> 在列表中出现的次数。</td></tr><tr><td align="left"><code>list.sort(key=None, reverse=False)</code></td><td align="left">对列表进行排序,如果指定参数 <code>key</code> 则按照 <code>key</code> 进行排序,如果指定参数 <code>reverse</code> 则为降序。</td></tr><tr><td align="left"><code>list.reverse()</code></td><td align="left">将列表中的元素倒序排列。</td></tr><tr><td align="left"><code>list.copy()</code></td><td align="left">返回列表的浅拷贝,即创建一个新列表并复制原列表中的元素。</td></tr></tbody></table>
|
|||
|
<h2 id="元组">元组<a aria-hidden="true" tabindex="-1" href="#元组" class="internal"> §</a></h2>
|
|||
|
<p>Python 中的元组是一种容器数据类型,使用小括号 <code>()</code> 定义,类似于列表,但元组的元素不能修改,是不可变的。元组用于存储多个数据,可以包含不同类型的元素。与列表不同,元组在定义时可以省略括号,但在实际使用中建议加上括号,以增强代码的可读性。</p>
|
|||
|
<p><img src="https://static.7wate.com/img/2022/11/20/49dc8db289c04.png" alt="img"/></p>
|
|||
|
<h3 id="定义-2">定义<a aria-hidden="true" tabindex="-1" href="#定义-2" class="internal"> §</a></h3>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 定义元组</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">t </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> (</span><span style="color:var(--shiki-token-string-expression);">'骆昊'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">38</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">True</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'四川成都'</span><span style="color:var(--shiki-color-text);">)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 定义空元组</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">t1 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> ()</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 元组中只包含一个元素时,需要在元素后面添加逗号 `,`,否则括号会被当作运算符使用。</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">tup1 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> (</span><span style="color:var(--shiki-token-constant);">50</span><span style="color:var(--shiki-color-text);">) </span><span style="color:var(--shiki-token-comment);"># 不加逗号,类型为整型</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">tup1 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> (</span><span style="color:var(--shiki-token-constant);">50</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);">) </span><span style="color:var(--shiki-token-comment);"># 加上逗号,类型为元组</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 遍历元组中的值</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> member </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> t</span><span style="color:var(--shiki-token-punctuation);">:</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(member)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 重新给元组赋值</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># t[0] = '王大锤' # TypeError,元组不可修改</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 变量t重新引用了新的元组,原来的元组将被垃圾回收</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">t </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> (</span><span style="color:var(--shiki-token-string-expression);">'王大锤'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">20</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">True</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'云南昆明'</span><span style="color:var(--shiki-color-text);">)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 将元组转换成列表</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">person </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">list</span><span style="color:var(--shiki-token-punctuation);">(t)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 将列表转换成元组</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">fruits_list </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> [</span><span style="color:var(--shiki-token-string-expression);">'apple'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'banana'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'orange'</span><span style="color:var(--shiki-color-text);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">fruits_tuple </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">tuple</span><span style="color:var(--shiki-token-punctuation);">(fruits_list)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 创建一个新的元组</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">tup1 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> (</span><span style="color:var(--shiki-token-constant);">12</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">34.56</span><span style="color:var(--shiki-color-text);">)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">tup2 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> (</span><span style="color:var(--shiki-token-string-expression);">'abc'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'xyz'</span><span style="color:var(--shiki-color-text);">)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">tup3 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> tup1 </span><span style="color:var(--shiki-token-keyword);">+</span><span style="color:var(--shiki-color-text);"> tup2</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 删除元组</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">tup </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> (</span><span style="color:var(--shiki-token-string-expression);">'Google'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'Runoob'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">1997</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2000</span><span style="color:var(--shiki-color-text);">)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">del</span><span style="color:var(--shiki-color-text);"> tup</span></span></code></pre></div>
|
|||
|
<h3 id="运算-2">运算<a aria-hidden="true" tabindex="-1" href="#运算-2" class="internal"> §</a></h3>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 创建元组</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">tup1 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> (</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-color-text);">)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">tup2 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> (</span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">5</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">6</span><span style="color:var(--shiki-color-text);">)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 连接元组,返回一个新的元组</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">tup3 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> tup1 </span><span style="color:var(--shiki-token-keyword);">+</span><span style="color:var(--shiki-color-text);"> tup2</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(tup3)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:(1, 2, 3, 4, 5, 6)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 复制元组,返回一个新的元组</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">tup4 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> tup1 </span><span style="color:var(--shiki-token-keyword);">*</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">3</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(tup4)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:(1, 2, 3, 1, 2, 3, 1, 2, 3)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 计算元素个数</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">length </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">len</span><span style="color:var(--shiki-token-punctuation);">(tup1)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(length)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:3</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 判断元素是否在元组中</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">result </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> tup1</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(result)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:False</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">result </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> tup1</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(result)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:True</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 访问元素,可以使用索引,索引从0开始</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">elem </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> tup1</span><span style="color:var(--shiki-token-punctuation);">[</span><span style="color:var(--shiki-token-constant);">0</span><span style="color:var(--shiki-token-punctuation);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(elem)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:1</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 切片操作</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">tup5 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> tup1</span><span style="color:var(--shiki-token-punctuation);">[</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-token-punctuation);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(tup5)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:(2, 3)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 切片时可以指定步长</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">tup6 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> tup1</span><span style="color:var(--shiki-token-punctuation);">[</span><span style="color:var(--shiki-token-constant);">0</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-token-punctuation);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(tup6)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:(1, 3)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 切片时可以省略开始或结束索引</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">tup7 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> tup1</span><span style="color:var(--shiki-token-punctuation);">[:</span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-token-punctuation);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">tup8 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> tup1</span><span style="color:var(--shiki-token-punctuation);">[</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">:]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(tup7)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:(1, 2)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(tup8)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:(2, 3)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 反向切片</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">tup9 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> tup1</span><span style="color:var(--shiki-token-punctuation);">[::</span><span style="color:var(--shiki-token-keyword);">-</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(tup9)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:(3, 2, 1)</span></span></code></pre></div>
|
|||
|
<h3 id="常用方法-2">常用方法<a aria-hidden="true" tabindex="-1" href="#常用方法-2" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th align="left">函数</th><th align="left">描述</th></tr></thead><tbody><tr><td align="left"><code>len(tuple)</code></td><td align="left">返回元组中元素的个数。</td></tr><tr><td align="left"><code>tuple.count(x)</code></td><td align="left">返回元组中元素 <code>x</code> 出现的次数。</td></tr><tr><td align="left"><code>tuple.index(x[, start[, end]])</code></td><td align="left">返回元组中第一个值为 <code>x</code> 的元素的索引。如果未找到该元素,会引发 <code>ValueError</code> 异常。</td></tr><tr><td align="left"><code>tuple + tuple</code></td><td align="left">将两个元组连接成一个新的元组。</td></tr><tr><td align="left"><code>tuple * n</code></td><td align="left">将元组重复 <code>n</code> 次。</td></tr><tr><td align="left"><code>x in tuple</code></td><td align="left">判断元素 <code>x</code> 是否在元组中,如果在返回 <code>True</code>,否则返回 <code>False</code>。</td></tr><tr><td align="left"><code>x not in tuple</code></td><td align="left">判断元素 <code>x</code> 是否不在元组中,如果不在返回 <code>True</code>,否则返回 <code>False</code>。</td></tr></tbody></table>
|
|||
|
<h2 id="集合">集合<a aria-hidden="true" tabindex="-1" href="#集合" class="internal"> §</a></h2>
|
|||
|
<p>集合(set)是由不重复元素组成的无序容器。基本用法包括成员检测、消除重复元素。集合对象支持合集、交集、差集、对称差分等数学运算。</p>
|
|||
|
<h3 id="定义-3">定义<a aria-hidden="true" tabindex="-1" href="#定义-3" class="internal"> §</a></h3>
|
|||
|
<p>集合可以通过花括号 <code>{}</code> 或 <code>set()</code> 函数创建。注意,创建空集合只能用 <code>set()</code>,不能用 <code>{}</code>,因为 <code>{}</code> 创建的是空字典。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 创建集合</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set1 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">5</span><span style="color:var(--shiki-token-punctuation);">}</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set2 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">5</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">6</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">7</span><span style="color:var(--shiki-token-punctuation);">}</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set3 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span><span style="color:var(--shiki-token-constant);">5</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">6</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">7</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">8</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">9</span><span style="color:var(--shiki-token-punctuation);">}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 判断元素是否在集合内</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> set1 </span><span style="color:var(--shiki-token-comment);"># True</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-constant);">10</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> set1 </span><span style="color:var(--shiki-token-comment);"># False</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 添加元素</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set1</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">add</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-constant);">6</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># {1, 2, 3, 4, 5, 6}</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set1</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">add</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-constant);">7</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># {1, 2, 3, 4, 5, 6, 7}</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set1</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">add</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># {1, 2, 3, 4, 5, 6, 7},重复的元素不会被添加</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 删除元素</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set1</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">remove</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># {1, 2, 3, 5, 6, 7}</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set1</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">discard</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-constant);">6</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># {1, 2, 3, 5, 7}</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set1</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">discard</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-constant);">8</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># {1, 2, 3, 5, 7},如果元素不存在,discard 方法不会引发异常</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 元素个数</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">len</span><span style="color:var(--shiki-token-punctuation);">(set1)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 5</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 清空集合</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set1</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">clear</span><span style="color:var(--shiki-token-punctuation);">()</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># set()</span></span></code></pre></div>
|
|||
|
<h3 id="运算-3">运算<a aria-hidden="true" tabindex="-1" href="#运算-3" class="internal"> §</a></h3>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">set1 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">5</span><span style="color:var(--shiki-token-punctuation);">}</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set2 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">5</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">6</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">7</span><span style="color:var(--shiki-token-punctuation);">}</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set3 </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span><span style="color:var(--shiki-token-constant);">5</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">6</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">7</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">8</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">9</span><span style="color:var(--shiki-token-punctuation);">}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 交集</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set1 </span><span style="color:var(--shiki-token-keyword);">&</span><span style="color:var(--shiki-color-text);"> set2 </span><span style="color:var(--shiki-token-comment);"># {3, 4, 5}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 并集</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set1 </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> set2 </span><span style="color:var(--shiki-token-comment);"># {1, 2, 3, 4, 5, 6, 7}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 差集</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set1 </span><span style="color:var(--shiki-token-keyword);">-</span><span style="color:var(--shiki-color-text);"> set2 </span><span style="color:var(--shiki-token-comment);"># {1, 2}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 对称差运算(在 set1 或 set2 中,但不会同时出现在二者中)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set1 </span><span style="color:var(--shiki-token-keyword);">^</span><span style="color:var(--shiki-color-text);"> set2 </span><span style="color:var(--shiki-token-comment);"># {1, 2, 6, 7}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 判断子集和超集</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set2 </span><span style="color:var(--shiki-token-keyword);"><=</span><span style="color:var(--shiki-color-text);"> set1 </span><span style="color:var(--shiki-token-comment);"># False,set2 是 set1 的子集吗?</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set3 </span><span style="color:var(--shiki-token-keyword);"><=</span><span style="color:var(--shiki-color-text);"> set1 </span><span style="color:var(--shiki-token-comment);"># False,set3 是 set1 的子集吗?</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set1 </span><span style="color:var(--shiki-token-keyword);">>=</span><span style="color:var(--shiki-color-text);"> set2 </span><span style="color:var(--shiki-token-comment);"># False,set1 是 set2 的超集吗?</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">set1 </span><span style="color:var(--shiki-token-keyword);">>=</span><span style="color:var(--shiki-color-text);"> set3 </span><span style="color:var(--shiki-token-comment);"># False,set1 是 set3 的超集吗?</span></span></code></pre></div>
|
|||
|
<h3 id="常用方法-3">常用方法<a aria-hidden="true" tabindex="-1" href="#常用方法-3" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th align="left">函数</th><th align="left">描述</th></tr></thead><tbody><tr><td align="left">set.add(x)</td><td align="left">将元素 x 添加到集合中</td></tr><tr><td align="left">set.remove(x)</td><td align="left">删除集合中的元素 x,如果 x 不在集合中会引发 KeyError</td></tr><tr><td align="left">set.discard(x)</td><td align="left">删除集合中的元素 x,如果 x 不在集合中不会引发异常</td></tr><tr><td align="left">set.pop()</td><td align="left">随机删除集合中的一个元素,并返回删除的元素</td></tr><tr><td align="left">set.clear()</td><td align="left">清空集合中的所有元素</td></tr><tr><td align="left">len(set)</td><td align="left">返回集合中元素的个数</td></tr><tr><td align="left">x in set</td><td align="left">判断元素 x 是否在集合中,如果在返回 True,否则返回 False</td></tr><tr><td align="left">set1.isdisjoint(set2)</td><td align="left">判断两个集合是否没有共同的元素,如果没有返回 True,否则返回 False</td></tr><tr><td align="left">set1.issubset(set2)</td><td align="left">判断集合 set1 是否是集合 set2 的子集,如果是返回 True,否则返回 False</td></tr><tr><td align="left">set1.issuperset(set2)</td><td align="left">判断集合 set1 是否是集合 set2 的超集,如果是返回 True,否则返回 False</td></tr><tr><td align="left">set1.union(set2)</td><td align="left">返回集合 set1 和 set2 的并集</td></tr><tr><td align="left">set1.intersection(set2)</td><td align="left">返回集合 set1 和 set2 的交集</td></tr><tr><td align="left">set1.difference(set2)</td><td align="left">返回集合 set1 和 set2 的差集,即在 set1 中但不在 set2 中的元素</td></tr><tr><td align="left">set1.symmetric_difference(set2)</td><td align="left">返回集合 set1 和 set2 的对称差集,即在 set1 或 set2 中,但不会同时出现在二者中的元素</td></tr><tr><td align="left">set1.update(set2)</td><td align="left">将集合 set2 中的元素添加到 set1 中</td></tr><tr><td align="left">set1.intersection_update(set2)</td><td align="left">将集合 set1 中不在 set2 中的元素删除</td></tr><tr><td align="left">set1.difference_update(set2)</td><td align="left">将集合 set1 和 set2 的差集中的元素从 set1 中删除</td></tr><tr><td align="left">set1.symmetric_difference_update(set2)</td><td align="left">将集合 set1 和 set2 的对称差集中的元素添加到 set1 中</td></tr><tr><td align="left">set.copy()</td><td align="left">返回集合的浅拷贝</td></tr><tr><td align="left">set.pop()</td><td align="left">随机删除并返回集合中的一个元素</td></tr></tbody></table>
|
|||
|
<h2 id="字典">字典<a aria-hidden="true" tabindex="-1" href="#字典" class="internal"> §</a></h2>
|
|||
|
<p><img src="https://static.7wate.com/img/2022/11/20/6f5d818eb8092.png" alt="img"/></p>
|
|||
|
<p>字典是另一种可变容器模型,且<strong>可存储任意类型对象</strong>。字典与列表、集合不同的是,它是一种键 - 值(key-value)对应的数据结构。每个元素都是<strong>由一个键和一个值组成的键值对</strong>,键和值通过冒号分开。键必须是唯一的,但值则不必。</p>
|
|||
|
<p><img src="https://static.7wate.com/img/2022/11/20/0e8264c5acc5a.png" alt="img"/></p>
|
|||
|
<h3 id="定义-4">定义<a aria-hidden="true" tabindex="-1" href="#定义-4" class="internal"> §</a></h3>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 使用大括号创建字典</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">person </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span><span style="color:var(--shiki-token-string-expression);">'name'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'John'</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'age'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">30</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'city'</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'New York'</span><span style="color:var(--shiki-token-punctuation);">}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 使用 dict() 创建字典</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">empty_dict </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">dict</span><span style="color:var(--shiki-token-punctuation);">()</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 访问字典中的元素</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(person[</span><span style="color:var(--shiki-token-string-expression);">'name'</span><span style="color:var(--shiki-token-punctuation);">])</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:John</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(person[</span><span style="color:var(--shiki-token-string-expression);">'age'</span><span style="color:var(--shiki-token-punctuation);">])</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:30</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(person[</span><span style="color:var(--shiki-token-string-expression);">'city'</span><span style="color:var(--shiki-token-punctuation);">])</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:New York</span></span></code></pre></div>
|
|||
|
<h3 id="运算-4">运算<a aria-hidden="true" tabindex="-1" href="#运算-4" class="internal"> §</a></h3>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 修改字典中的元素</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">person</span><span style="color:var(--shiki-token-punctuation);">[</span><span style="color:var(--shiki-token-string-expression);">'age'</span><span style="color:var(--shiki-token-punctuation);">]</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">31</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(person)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:{'name': 'John', 'age': 31, 'city': 'New York'}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 添加新的键值对</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">person</span><span style="color:var(--shiki-token-punctuation);">[</span><span style="color:var(--shiki-token-string-expression);">'job'</span><span style="color:var(--shiki-token-punctuation);">]</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'Engineer'</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(person)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:{'name': 'John', 'age': 31, 'city': 'New York', 'job': 'Engineer'}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 删除字典中的元素</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">del</span><span style="color:var(--shiki-color-text);"> person</span><span style="color:var(--shiki-token-punctuation);">[</span><span style="color:var(--shiki-token-string-expression);">'city'</span><span style="color:var(--shiki-token-punctuation);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(person)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:{'name': 'John', 'age': 31, 'job': 'Engineer'}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 清空字典</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">person</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">clear</span><span style="color:var(--shiki-token-punctuation);">()</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(person)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:{}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 遍历字典的键</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> key </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> person</span><span style="color:var(--shiki-token-punctuation);">:</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(key)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 遍历字典的值</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> value </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> person</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">values</span><span style="color:var(--shiki-token-punctuation);">():</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(value)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 遍历字典的键值对</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> key</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> value </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> person</span><span style="color:var(--shiki-token-punctuation);">.</span><span style="color:var(--shiki-token-function);">items</span><span style="color:var(--shiki-token-punctuation);">():</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(key, value)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 判断键是否存在</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">if</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'name'</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> person</span><span style="color:var(--shiki-token-punctuation);">:</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">"Name exists."</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
|
|||
|
<span data-line> </span></code></pre></div>
|
|||
|
<h3 id="常用方法-4">常用方法<a aria-hidden="true" tabindex="-1" href="#常用方法-4" class="internal"> §</a></h3>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th align="left">方法</th><th align="left">描述</th></tr></thead><tbody><tr><td align="left"><code>keys()</code></td><td align="left">返回字典所有键的列表</td></tr><tr><td align="left"><code>values()</code></td><td align="left">返回字典所有值的列表</td></tr><tr><td align="left"><code>items()</code></td><td align="left">返回字典所有键值对的元组列表</td></tr><tr><td align="left"><code>get(key, default=None)</code></td><td align="left">返回指定键的值,如果键不存在,返回默认值</td></tr><tr><td align="left"><code>pop(key)</code></td><td align="left">删除指定键的值,并返回该键对应的值</td></tr><tr><td align="left"><code>popitem()</code></td><td align="left">随机删除一个键值对,并返回该键值对</td></tr><tr><td align="left"><code>clear()</code></td><td align="left">删除字典中的所有键值对</td></tr><tr><td align="left"><code>update(other_dict)</code></td><td align="left">将其他字典的键值对更新到当前字典</td></tr><tr><td align="left"><code>copy()</code></td><td align="left">返回字典的浅拷贝(复制字典中的键值对)</td></tr></tbody></table>
|
|||
|
<h2 id="推导式">推导式<a aria-hidden="true" tabindex="-1" href="#推导式" class="internal"> §</a></h2>
|
|||
|
<p>推导式是一种强大的 Python 特性,可以快速、简洁地创建新的数据序列。它可以从一个数据序列构建另一个新的数据序列的结构体。</p>
|
|||
|
<h3 id="列表-1">列表<a aria-hidden="true" tabindex="-1" href="#列表-1" class="internal"> §</a></h3>
|
|||
|
<p>列表推导式是最常用的推导式,它通过在一个可迭代对象上应用表达式来创建一个新的列表。</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 语法</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">new_list </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> [out_exp_res </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> out_exp </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> input_list]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">new_list </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> [out_exp_res </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> out_exp </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> input_list </span><span style="color:var(--shiki-token-keyword);">if</span><span style="color:var(--shiki-color-text);"> condition]</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 示例</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">multiples </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> [i </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> i </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">range</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-constant);">30</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">if</span><span style="color:var(--shiki-color-text);"> i </span><span style="color:var(--shiki-token-keyword);">%</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">==</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">0</span><span style="color:var(--shiki-color-text);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(multiples)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:[0, 3, 6, 9, 12, 15, 18, 21, 24, 27]</span></span></code></pre></div>
|
|||
|
<h3 id="字典-1">字典<a aria-hidden="true" tabindex="-1" href="#字典-1" class="internal"> §</a></h3>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 语法</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">new_dict </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span><span style="color:var(--shiki-color-text);">key_expr</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> value_expr </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> value </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> collection</span><span style="color:var(--shiki-token-punctuation);">}</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">new_dict </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span><span style="color:var(--shiki-color-text);">key_expr</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> value_expr </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> value </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> collection </span><span style="color:var(--shiki-token-keyword);">if</span><span style="color:var(--shiki-color-text);"> condition</span><span style="color:var(--shiki-token-punctuation);">}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 示例</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">dic </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span><span style="color:var(--shiki-color-text);">x</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> x</span><span style="color:var(--shiki-token-keyword);">**</span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> x </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> (</span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">6</span><span style="color:var(--shiki-color-text);">)</span><span style="color:var(--shiki-token-punctuation);">}</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(dic)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:{2: 4, 4: 16, 6: 36}</span></span></code></pre></div>
|
|||
|
<h3 id="集合-1">集合<a aria-hidden="true" tabindex="-1" href="#集合-1" class="internal"> §</a></h3>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 语法</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">new_set </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span><span style="color:var(--shiki-color-text);">expression </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> item </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> sequence</span><span style="color:var(--shiki-token-punctuation);">}</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">new_set </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span><span style="color:var(--shiki-color-text);">expression </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> item </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> sequence </span><span style="color:var(--shiki-token-keyword);">if</span><span style="color:var(--shiki-color-text);"> conditional</span><span style="color:var(--shiki-token-punctuation);">}</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 示例</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">a </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-punctuation);">{</span><span style="color:var(--shiki-color-text);">x </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> x </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'abracadabra'</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">if</span><span style="color:var(--shiki-color-text);"> x </span><span style="color:var(--shiki-token-keyword);">not</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'abc'</span><span style="color:var(--shiki-token-punctuation);">}</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(a)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:{'d', 'r'}</span></span></code></pre></div>
|
|||
|
<h3 id="元组-1">元组<a aria-hidden="true" tabindex="-1" href="#元组-1" class="internal"> §</a></h3>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 语法</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">new_tuple </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> (expression </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> item </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> sequence)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">new_tuple </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> (expression </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> item </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> sequence </span><span style="color:var(--shiki-token-keyword);">if</span><span style="color:var(--shiki-color-text);"> conditional)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 示例</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">a </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> (x </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> x </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">range</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">, </span><span style="color:var(--shiki-token-constant);">10</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);">)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(a)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:<generator object <genexpr> at 0x7f900dfdd200></span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-constant);">tuple</span><span style="color:var(--shiki-token-punctuation);">(a))</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:(1, 2, 3, 4, 5, 6, 7, 8, 9)</span></span></code></pre></div>
|
|||
|
<h3 id="其他">其他<a aria-hidden="true" tabindex="-1" href="#其他" class="internal"> §</a></h3>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">f </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> [x </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> x </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">range</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">, </span><span style="color:var(--shiki-token-constant);">10</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(f)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">f </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> [x </span><span style="color:var(--shiki-token-keyword);">+</span><span style="color:var(--shiki-color-text);"> y </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> x </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'ABCDE'</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> y </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'1234567'</span><span style="color:var(--shiki-color-text);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(f)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">f </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> [x </span><span style="color:var(--shiki-token-keyword);">**</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> x </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">range</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">, </span><span style="color:var(--shiki-token-constant);">1000</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(sys.</span><span style="color:var(--shiki-token-function);">getsizeof</span><span style="color:var(--shiki-token-punctuation);">(f))</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 查看对象占用内存的字节数</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(f)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 生成器表达式,不占用额外内存</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">f </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> (x </span><span style="color:var(--shiki-token-keyword);">**</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> x </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">range</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">, </span><span style="color:var(--shiki-token-constant);">1000</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);">)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(sys.</span><span style="color:var(--shiki-token-function);">getsizeof</span><span style="color:var(--shiki-token-punctuation);">(f))</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 相比列表生成式,生成器不占用存储数据的空间</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(f)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> val </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> f</span><span style="color:var(--shiki-token-punctuation);">:</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(val)</span></span>
|
|||
|
<span data-line> </span></code></pre></div>
|
|||
|
<h2 id="迭代器">迭代器<a aria-hidden="true" tabindex="-1" href="#迭代器" class="internal"> §</a></h2>
|
|||
|
<p>可迭代对象是 Python 中的一种数据结构,例如列表、元组、字典、字符串等都是可迭代对象。迭代器是一种特殊的可迭代对象,它可以通过调用 <code>iter()</code> 函数获取,然后使用 <code>next()</code> 函数逐个访问元素。迭代器在其生命周期中,会有四个状态:<code>GEN_CREATED</code>、<code>GEN_RUNNING</code>、<code>GEN_SUSPENDED</code> 和 <code>GEN_CLOSED</code>。</p>
|
|||
|
<pre><code class="mermaid">graph LR
|
|||
|
A[生成器已创建] --> B[解释器执行中]
|
|||
|
B --> C[在 yield 表达式处暂停]
|
|||
|
C --> D[解释器执行中]
|
|||
|
D --> C
|
|||
|
D --> E[生成器执行结束]
|
|||
|
</code></pre>
|
|||
|
<ul>
|
|||
|
<li><strong>iter()</strong>:创建迭代器。</li>
|
|||
|
<li><strong>next()</strong>:输出迭代器的下一个元素。</li>
|
|||
|
</ul>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">s </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'abc'</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">it </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">iter</span><span style="color:var(--shiki-token-punctuation);">(s)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(it)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:<str_iterator object at 0x10c90e650></span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-function);">next</span><span style="color:var(--shiki-token-punctuation);">(it))</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:'a'</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-function);">next</span><span style="color:var(--shiki-token-punctuation);">(it))</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:'b'</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-function);">next</span><span style="color:var(--shiki-token-punctuation);">(it))</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:'c'</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 抛出异常</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 迭代器用尽后会引发 StopIteration 异常</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">try</span><span style="color:var(--shiki-token-punctuation);">:</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-function);">next</span><span style="color:var(--shiki-token-punctuation);">(it))</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">except</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">StopIteration</span><span style="color:var(--shiki-token-punctuation);">:</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-string-expression);">"迭代器用尽了。"</span><span style="color:var(--shiki-token-punctuation);">)</span></span>
|
|||
|
<span data-line> </span></code></pre></div>
|
|||
|
<h2 id="生成器">生成器<a aria-hidden="true" tabindex="-1" href="#生成器" class="internal"> §</a></h2>
|
|||
|
<p>生成器是一种特殊的迭代器,它是一个可以像迭代器一样使用 for 循环来获取元素的函数。生成器函数使用 <code>yield</code> 关键字将一个普通函数改造成生成器函数,<code>yield</code> 相当于 <code>return</code> 但有所不同,<code>yield</code> 虽然返回了值但函数并没有结束,而是暂停在 <code>yield</code> 处,下次调用 <code>next()</code> 或循环迭代时会从 <code>yield</code> 处继续执行。生成器在其生命周期中会有四个状态:<code>GEN_CREATED</code>、<code>GEN_RUNNING</code>、<code>GEN_SUSPENDED</code> 和 <code>GEN_CLOSED</code>。</p>
|
|||
|
<pre><code class="mermaid">graph LR
|
|||
|
A[生成器函数被调用] --> B[生成器已创建]
|
|||
|
B --> C[解释器执行中]
|
|||
|
C --> D[在 yield 表达式处暂停]
|
|||
|
D --> C
|
|||
|
C --> E[生成器执行结束]
|
|||
|
|
|||
|
F[生成器已创建] --> G[GEN_CREATED]
|
|||
|
G --> H[生成器正在执行]
|
|||
|
H --> I[GEN_RUNNING]
|
|||
|
I --> J[在 yield 表达式处暂停]
|
|||
|
J --> H
|
|||
|
I --> K[生成器执行结束]
|
|||
|
K --> L[GEN_CLOSED]
|
|||
|
</code></pre>
|
|||
|
<p>生成器在其生命周期中,会有如下四个状态:</p>
|
|||
|
<ul>
|
|||
|
<li>GEN_CREATED:生成器已创建,还未被激活。</li>
|
|||
|
<li>GEN_RUNNING:解释器正在执行(只有在多线程应用中才能看到这个状态)。</li>
|
|||
|
<li>GEN_SUSPENDED:在 yield 表达式处暂停。</li>
|
|||
|
<li>GEN_CLOSED:生成器执行结束。</li>
|
|||
|
</ul>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-keyword);">def</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">fibonacci</span><span style="color:var(--shiki-color-text);">(</span><span style="color:var(--shiki-token-parameter);">n</span><span style="color:var(--shiki-color-text);">):</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> a</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> b</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> counter </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">0</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">0</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">while</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">True</span><span style="color:var(--shiki-token-punctuation);">:</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">if</span><span style="color:var(--shiki-color-text);"> counter </span><span style="color:var(--shiki-token-keyword);">></span><span style="color:var(--shiki-color-text);"> n</span><span style="color:var(--shiki-token-punctuation);">:</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">return</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">yield</span><span style="color:var(--shiki-color-text);"> a</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> a</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> b </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> b</span><span style="color:var(--shiki-token-punctuation);">,</span><span style="color:var(--shiki-color-text);"> a </span><span style="color:var(--shiki-token-keyword);">+</span><span style="color:var(--shiki-color-text);"> b</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> counter </span><span style="color:var(--shiki-token-keyword);">+=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">1</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> i </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">fibonacci</span><span style="color:var(--shiki-token-punctuation);">(</span><span style="color:var(--shiki-token-constant);">10</span><span style="color:var(--shiki-token-punctuation);">):</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">print</span><span style="color:var(--shiki-token-punctuation);">(i, end</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string-expression);">" "</span><span style="color:var(--shiki-token-punctuation);">)</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># 输出:0 1 1 2 3 5 8 13 21 34 55</span></span></code></pre></div></article></div><div class="right sidebar"><div class="graph "><h3>Graph View</h3><div class="graph-outer"><div id="graph-container" data-cfg="{"drag":true,"zoom":true,"depth":1,"scale":1.1,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[]}"></div><svg version="1.1" id="global-graph-icon" xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xmlSpace="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
|
|||
|
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
|
|||
|
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
|
|||
|
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
|
|||
|
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
|
|||
|
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
|
|||
|
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
|
|||
|
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
|
|||
|
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
|
|||
|
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
|
|||
|
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{"drag":true,"zoom":true,"depth":-1,"scale":0.9,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[]}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class><h3>Table of Contents</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content"><ul class="overflow"><li class="depth-0"><a href="#数值" data-for="数值">数值</a></li><li class="depth-0"><a href="#字符串" data-for="字符串">字符串</a></li><li class="depth-1"><a href="#定义" data-for="定义">定义</a></li><li class="depth-1"><a href="#转义" data-for="转义">转义</a></li><li class="depth-1"><a href="#运算" data-for="运算">运算</a></li><li class="depth-1"><a href="#f-strings" data-for="f-strings">f-strings</a></li><li class="depth-1"><a href="#常用方法" data-for="常用方法">常用方法</a></li><li class="depth-0"><a href="#列表" data-for="列表">列表</a></li><li class="depth-1"><a href="#定义-1" data-for="定义-1">定义</a></li><li class="depth-1"><a href="#运算-1" data-for="运算-1">运算</a></li><li class="depth-1"><a href="#常用方法-1" data-for="常用方法-1">常用方法</a></li><li class="depth-0"><a href="#元组" data-for="元组">元组</a></li><li class="depth-1"><a href="#定义-2" data-for="定义-2">定义</a></li><li class="depth-1"><a href="#运算-2" data-for="运算-2">运算</a></li><li class="depth-1"><a href="#常用方法-2" data-for="常用方法-2">常用方法</a></li><li class="depth-0"><a href="#集合" data-for="集合">集合</a></li><li class="depth-1"><a href="#定义-3" data-for="定义-3">定义</a></li><li class="depth-1"><a href="#运算-3" data-for="运算-3">运算</a></li><li class="depth-1"><a href="#常用方法-3" data-for="常用方法-3">常用方法</a></li><li class="depth-0"><a href="#字典" data-for="字典">字典</a></li><li class="depth-1"><a href="#定义-4" data-for="定义-4">定义</a></li><li class="depth-1"><a href="#运算-4" data-for="运算-4">运算</a></li><li class="depth-1"><a href="#常用方法-4" data-for="常用方法-4">常用方法</a></li><li class="depth-0"><a href="#推导式" data-for="推导式">推导式</a></li><li class="depth-1"><a href="#列表-1" data-for="列表-1">列表</a></li><li class="depth-1"><a href="#字典-1" data-for="字典-1">字典</a></li><li class="depth-1"><a href="#集合-1" data-for="集合-1">集合</a></li><li class="depth-1"><a href="#元组-1" data-for="元组-1">元组</a></li><li class="depth-1"><a href="#其他" data-for="其他">其他</a></li><li class="depth-0"><a href="#迭代器" data-for="迭代器">迭代器</a></li><li class="depth-0"><a href="#生成器" data-for="生成器">生成器</a></li></ul></div></div><div class="backlinks "><h3>Backlinks</h3><ul class="overflow"><li>No backlinks found</li></ul></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{"path":"Basics","collapsed":true},{"path":"Basics/english","collapsed":true},{"path":"Blog","collapsed":true},{"path":"Book","collapsed":true},{"path":"Book/个人成长","collapsed":true},{"path":"Book/医学健康","collapsed":true},{"path":"Book/历史","collapsed":true},{"path":"Book/哲学宗教&quo
|
|||
|
function toggleCallout() {
|
|||
|
const outerBlock = this.parentElement;
|
|||
|
outerBlock.classList.toggle(`is-collapsed`);
|
|||
|
const collapsed = outerBlock.classList.contains(`is-collapsed`);
|
|||
|
const height = collapsed ? this.scrollHeight : outerBlock.scrollHeight;
|
|||
|
outerBlock.style.maxHeight = height + `px`;
|
|||
|
let current = outerBlock;
|
|||
|
let parent = outerBlock.parentElement;
|
|||
|
while (parent) {
|
|||
|
if (!parent.classList.contains(`callout`)) {
|
|||
|
return;
|
|||
|
}
|
|||
|
const collapsed2 = parent.classList.contains(`is-collapsed`);
|
|||
|
const height2 = collapsed2 ? parent.scrollHeight : parent.scrollHeight + current.scrollHeight;
|
|||
|
parent.style.maxHeight = height2 + `px`;
|
|||
|
current = parent;
|
|||
|
parent = parent.parentElement;
|
|||
|
}
|
|||
|
}
|
|||
|
function setupCallout() {
|
|||
|
const collapsible = document.getElementsByClassName(
|
|||
|
`callout is-collapsible`
|
|||
|
);
|
|||
|
for (const div of collapsible) {
|
|||
|
const title = div.firstElementChild;
|
|||
|
if (title) {
|
|||
|
title.removeEventListener(`click`, toggleCallout);
|
|||
|
title.addEventListener(`click`, toggleCallout);
|
|||
|
const collapsed = div.classList.contains(`is-collapsed`);
|
|||
|
const height = collapsed ? title.scrollHeight : div.scrollHeight;
|
|||
|
div.style.maxHeight = height + `px`;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
document.addEventListener(`nav`, setupCallout);
|
|||
|
window.addEventListener(`resize`, setupCallout);
|
|||
|
</script><script type="module">
|
|||
|
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
|
|||
|
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
|
|||
|
mermaid.initialize({
|
|||
|
startOnLoad: false,
|
|||
|
securityLevel: 'loose',
|
|||
|
theme: darkMode ? 'dark' : 'default'
|
|||
|
});
|
|||
|
document.addEventListener('nav', async () => {
|
|||
|
await mermaid.run({
|
|||
|
querySelector: '.mermaid'
|
|||
|
})
|
|||
|
});
|
|||
|
</script><script src="https://cdn.jsdelivr.net/npm/katex@0.16.7/dist/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../postscript.js" type="module"></script></html>
|