-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
80 lines (45 loc) · 68 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Evanq</title>
<link href="http://example.com/atom.xml" rel="self"/>
<link href="http://example.com/"/>
<updated>2022-09-07T15:17:29.095Z</updated>
<id>http://example.com/</id>
<author>
<name>Evanq</name>
</author>
<generator uri="https://hexo.io/">Hexo</generator>
<entry>
<title>cpp拾遗</title>
<link href="http://example.com/2022/09/07/cpp%E6%8B%BE%E9%81%97/"/>
<id>http://example.com/2022/09/07/cpp%E6%8B%BE%E9%81%97/</id>
<published>2022-09-07T15:03:41.000Z</published>
<updated>2022-09-07T15:17:29.095Z</updated>
<content type="html"><![CDATA[<p>暑假 robocom比赛前稍微整理了一下c++常用的一些运算符重载和STL知识点,</p><p><del>丢在这充个数</del></p><span id="more"></span><h3 id="结构体-Node-a-b"><a href="#结构体-Node-a-b" class="headerlink" title="结构体 (Node){a,b};"></a><strong>结构体</strong> (Node){a,b};</h3><h3 id="重载运算符"><a href="#重载运算符" class="headerlink" title="重载运算符"></a><strong>重载运算符</strong></h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>:</span><br><span class="line"></span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span> < (<span class="type">const</span> Node &a, <span class="type">const</span> Node &b ) {</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span>( a.x == b.x ) <span class="keyword">return</span> a.y> b.y;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> a.x> b.x; </span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="number">2</span>:</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Node</span>{</span><br><span class="line"></span><br><span class="line"> <span class="type">int</span> a,b;</span><br><span class="line"></span><br><span class="line"> <span class="type">bool</span> <span class="keyword">operator</span> <(<span class="type">const</span> Node & tmp) <span class="type">const</span> { <span class="comment">//最后的const不能省</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> (tmp.a!=a) <span class="keyword">return</span> a<tmp.a;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> b<tmp.b;</span><br><span class="line"></span><br><span class="line"> } </span><br><span class="line"></span><br><span class="line">};</span><br></pre></td></tr></table></figure><h3 id="Pair"><a href="#Pair" class="headerlink" title="Pair"></a><strong>Pair</strong></h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">**pair嵌套** pair<<span class="type">int</span>,pair<<span class="type">int</span>,<span class="type">char</span>> > p;</span><br><span class="line"></span><br><span class="line"><span class="function">pair<<span class="type">int</span>,<span class="type">char</span>> <span class="title">p</span><span class="params">(<span class="number">0</span>,’z’)</span></span>; </span><br><span class="line"></span><br><span class="line">p = <span class="built_in">make_pair</span>(<span class="number">1</span>,’A’); <span class="comment">// p = pair<int,char>(1,’A’);</span></span><br><span class="line"></span><br><span class="line">cout<<p.first << p.second;</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> pair<<span class="type">int</span>,<span class="type">int</span>> pp;</span><br></pre></td></tr></table></figure><h3 id="重载运算符-1"><a href="#重载运算符-1" class="headerlink" title="重载运算符"></a><strong>重载运算符</strong></h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">template</span><<span class="keyword">class</span> <span class="title class_">Ty1</span>,<span class="keyword">class</span> <span class="title class_">Ty2</span>> </span><br><span class="line"></span><br><span class="line"><span class="keyword">inline</span> <span class="type">const</span> pair<Ty1,Ty2> <span class="keyword">operator</span>+(<span class="type">const</span> pair<Ty1, Ty2>&p1, <span class="type">const</span> pair<Ty1, Ty2>&p2) {</span><br><span class="line"></span><br><span class="line"> pair<Ty1, Ty2> ret;</span><br><span class="line"></span><br><span class="line"> ret.first = p1.first + p2.first;</span><br><span class="line"></span><br><span class="line"> ret.second = p1.second + p2.second;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> ret;</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>排序遵从字典序</p><h3 id="Vector-x2F-list"><a href="#Vector-x2F-list" class="headerlink" title="Vector/list"></a><strong>Vector/list</strong></h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Vector<<span class="type">int</span>> u;</span><br><span class="line"></span><br><span class="line">常用函数:u.<span class="built_in">front</span>(), u.<span class="built_in">back</span>()(访问首尾)u.<span class="built_in">empty</span>(), u.<span class="built_in">clear</span>(), u.<span class="built_in">begin</span>(), u.<span class="built_in">end</span>(),(首尾地址) u.<span class="built_in">push_back</span>(), u.<span class="built_in">pop_back</span>(),(末尾添加和删除) u.<span class="built_in">size</span>(), u.<span class="built_in">erase</span>(),(删除指定地址或范围)u.<span class="built_in">resize</span>(), u.<span class="built_in">insert</span>()</span><br></pre></td></tr></table></figure><h4 id="遍历:"><a href="#遍历:" class="headerlink" title="遍历:"></a>遍历:</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">vector<<span class="type">int</span>>::iterator iter = u.<span class="built_in">begin</span>();</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (; it != u.<span class="built_in">end</span>(); it ++);</span><br><span class="line"></span><br><span class="line"><span class="comment">//或者</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">auto</span> it = u.<span class="built_in">begin</span>(); it != u.<span class="built_in">end</span>(); it++);</span><br><span class="line"></span><br><span class="line"><span class="comment">//逆遍历</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">For</span> (<span class="keyword">auto</span> it = u.<span class="built_in">rbegin</span>(); it != u.<span class="built_in">rend</span>(); it--);</span><br></pre></td></tr></table></figure><p>函数:resize、insert</p><h3 id="stack"><a href="#stack" class="headerlink" title="stack"></a><strong>stack</strong></h3><p>stack<int> st;</p><p>top(取栈顶值),push(栈顶加入),pop(栈顶删除),size,empty,不能clear,遍历</p><h3 id="Queue"><a href="#Queue" class="headerlink" title="Queue"></a><strong>Queue</strong></h3><p>queue<int> q;</p><p>push(队尾添加), pop(删除队首), size, empty, front,back(访问首尾),不能clear,不能遍历</p><h3 id="Deque"><a href="#Deque" class="headerlink" title="Deque"></a><strong>Deque</strong></h3><p>begin,end,front,back,push_back,push_front,pop_back,pop_front,clear,insert,erase,empty,size</p><h3 id="Priority-queue"><a href="#Priority-queue" class="headerlink" title="Priority_queue"></a><strong>Priority_queue</strong></h3><p>priority_queue<int> q; (默认大根堆)</p><p>priority_queue<int, vector<int>, greater<int>> q; (小根堆)////取负数</p><p>size, empty, pop(删除堆顶), top(取堆顶), push,不能clear</p><h3 id="Map"><a href="#Map" class="headerlink" title="Map"></a><strong>Map</strong></h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//建立非连续映射</span></span><br><span class="line"></span><br><span class="line">map<<span class="type">char</span>,<span class="type">int</span>> mp;</span><br><span class="line"></span><br><span class="line"><span class="comment">//插入:</span></span><br><span class="line"></span><br><span class="line">mp.<span class="built_in">insert</span>(<span class="built_in">pair</span><<span class="type">char</span>,<span class="type">int</span>>(‘A’,<span class="number">1</span>)); :key已存在无法插入</span><br><span class="line"></span><br><span class="line">mp.<span class="built_in">insert</span>(<span class="built_in">make_pair</span>(‘A’,<span class="number">1</span>)); <span class="comment">//同上</span></span><br><span class="line"></span><br><span class="line">mp[‘A’] = <span class="number">1</span>; :直接覆盖原有值</span><br><span class="line"> </span><br><span class="line"></span><br><span class="line"><span class="comment">//迭代器遍历,查询key:it->first,查询value:it->second(已按key升序排序)</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">//寻找键值:</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//不能直接访问不存在的key,先检mp.count(key)或者mp.find(key)</span></span><br><span class="line"></span><br><span class="line">mp.<span class="built_in">find</span>(key) == mp.<span class="built_in">end</span>()表示键值不存在</span><br><span class="line"></span><br><span class="line">mp.<span class="built_in">count</span>(key) == <span class="number">0</span> 表示键值不存在 <span class="comment">//不能定位</span></span><br><span class="line"> </span><br><span class="line"></span><br><span class="line"><span class="comment">//函数:size(查询键值个数)、clear、erase 指定地址:</span></span><br><span class="line"></span><br><span class="line">map<<span class="type">int</span>,string>::iterator iter;</span><br><span class="line"></span><br><span class="line">iter = mp.<span class="built_in">find</span>(<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (iter!=mp.<span class="built_in">end</span>()) mp.<span class="built_in">erase</span>(iter);</span><br><span class="line"></span><br><span class="line"><span class="comment">//erase 指定键值</span></span><br><span class="line"></span><br><span class="line"> <span class="built_in">erase</span>(‘A’);</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>map中元素自动按key升序排序(若键值不可排序可重载运算符)</p><h3 id="set"><a href="#set" class="headerlink" title="set"></a><strong>set</strong></h3><p>set<int> st;不可有重复数值,可以看成key与value相同的map</p><p>操作与map类似</p><h3 id="Multiset"><a href="#Multiset" class="headerlink" title="Multiset"></a><strong>Multiset</strong></h3><p>可以插入重复元素,count可以大于1,size函数返回元素个数而不是不同值的个数,erase会将所有对应的key全部删掉</p><p>删除一个:s.erase(s.find(key))</p><p>Set缺点:不能直接改变元素的值,优点:搜索删除具有对数复杂度</p><h3 id="Bitset"><a href="#Bitset" class="headerlink" title="Bitset"></a><strong>Bitset</strong></h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">bitset<位数> bitset1;</span><br><span class="line"></span><br><span class="line"><span class="function">bitset<8> <span class="title">bitset1</span><span class="params">(<span class="number">12</span>)</span><span class="comment">//bitset1(string(“100101”));</span></span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function">bitset<4> <span class="title">b1</span><span class="params">(“<span class="number">1001</span>”)</span>,<span class="title">b2</span><span class="params">(“<span class="number">0101</span>”)</span></span>;</span><br><span class="line"></span><br><span class="line">b1&=b2; cout<<b1&b2;</span><br><span class="line"></span><br><span class="line">b1[<span class="number">0</span>]为<span class="number">1</span>,b1[<span class="number">1</span>]为<span class="number">0</span></span><br><span class="line"></span><br><span class="line">b1.<span class="built_in">count</span>() == <span class="number">2</span>; 返回<span class="number">1</span>的个数</span><br><span class="line"></span><br><span class="line">b1.<span class="built_in">test</span>(<span class="number">0</span>) == <span class="literal">true</span>; 检查下标元素是<span class="number">0</span>/<span class="number">1</span></span><br><span class="line"></span><br><span class="line">b1.<span class="built_in">any</span>() == <span class="literal">true</span>;(是否有<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">b1.<span class="built_in">none</span>() == <span class="literal">false</span> (是否无<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">b1.<span class="built_in">all</span>() == <span class="literal">false</span>(是否全为<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">b1.<span class="built_in">flip</span>()有参数反转下标,否则反转全部</span><br><span class="line"></span><br><span class="line">b1.<span class="built_in">set</span>() 所有位置为<span class="number">1</span>,有参数下标处置<span class="number">1</span></span><br><span class="line"></span><br><span class="line">b1.<span class="built_in">reset</span>() 置<span class="number">0</span>,同上</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html"><p>暑假 robocom比赛前稍微整理了一下c++常用的一些运算符重载和STL知识点,</p>
<p><del>丢在这充个数</del></p></summary>
<category term="study" scheme="http://example.com/tags/study/"/>
<category term="c++" scheme="http://example.com/tags/c/"/>
</entry>
<entry>
<title>Python学习笔记</title>
<link href="http://example.com/2022/09/07/Python%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"/>
<id>http://example.com/2022/09/07/Python%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/</id>
<published>2022-09-07T11:40:49.000Z</published>
<updated>2022-09-07T15:21:38.077Z</updated>
<content type="html"><![CDATA[<p>按老师要求总算在开学前给python最基础的部分过了一遍,</p><p>把一些刚学可能遗忘的知识点记录<del>粘贴</del>于此,便于日后回忆<del>ctrl+f</del></p><span id="more"></span><h4 id="Python数据类型:"><a href="#Python数据类型:" class="headerlink" title="Python数据类型:"></a>Python数据类型:</h4><p>Number、String、Tuple:不可变数据</p><p>List、Set、Dictionary:可变数据</p><p>序列:string、list、tuple</p><h4 id="基本语法:"><a href="#基本语法:" class="headerlink" title="基本语法:"></a>基本语法:</h4><p>多语句一行用 ; 连接</p><p>跨越多行:\ 续行符(在行尾时),’’’或者”””</p><h5 id="选择、循环语句:"><a href="#选择、循环语句:" class="headerlink" title="选择、循环语句:"></a>选择、循环语句:</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#选择语句:</span></span><br><span class="line"><span class="keyword">if</span> 表达式<span class="number">1</span>:</span><br><span class="line"> <span class="comment">#...</span></span><br><span class="line"><span class="keyword">elif</span> 表达式<span class="number">2</span>:</span><br><span class="line"> <span class="comment">#...</span></span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"> <span class="comment">#...</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#循环语句:(没有do while)</span></span><br><span class="line"><span class="keyword">while</span> 表达式<span class="number">1</span>:</span><br><span class="line"> <span class="comment">#...</span></span><br><span class="line"><span class="keyword">else</span>: <span class="comment">#当表达式1为false时执行</span></span><br><span class="line"> <span class="comment">#...</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> var <span class="keyword">in</span> seq:</span><br><span class="line"> <span class="comment">#...</span></span><br><span class="line"><span class="keyword">else</span>: <span class="comment">#循环正常结束时执行</span></span><br><span class="line"> <span class="comment">#...</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">####若break跳出循环则不执行else语句</span></span><br></pre></td></tr></table></figure><h4 id="Python推导式:"><a href="#Python推导式:" class="headerlink" title="Python推导式:"></a>Python推导式:</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#先for后if,if过滤</span></span><br><span class="line">a = {x <span class="keyword">for</span> x <span class="keyword">in</span> <span class="string">'abracadabra'</span> <span class="keyword">if</span> x <span class="keyword">not</span> <span class="keyword">in</span> <span class="string">'abc'</span>}</span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#先if后for,对于元素的不同情况取不同值</span></span><br><span class="line">list1 = [<span class="string">'python'</span>, <span class="string">'test1'</span>, <span class="string">'test2'</span>]</span><br><span class="line">list2 = [word.title() <span class="keyword">if</span> word.startswith(<span class="string">'p'</span>) <span class="keyword">else</span> word.upper() <span class="keyword">for</span> word <span class="keyword">in</span> list1]</span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">vec1 = [<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>]</span><br><span class="line">vec2 = [<span class="number">4</span>, <span class="number">3</span>, -<span class="number">9</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment">#双重循环</span></span><br><span class="line">vec3 = [x*y <span class="keyword">for</span> x <span class="keyword">in</span> vec1 <span class="keyword">for</span> y <span class="keyword">in</span> vec2]</span><br><span class="line"><span class="comment">#vec3 = [8, 6, -18, 16, 12, -36, 24, 18, -54] </span></span><br><span class="line"></span><br><span class="line"><span class="comment">#遍历</span></span><br><span class="line">vec3 = [vec1[i]*vec2[i] <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(vec1))]</span><br><span class="line"><span class="comment">#vec3 = [8, 12, -54]</span></span><br></pre></td></tr></table></figure><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#嵌套列表</span></span><br><span class="line">matrix = [</span><br><span class="line"> [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>],</span><br><span class="line"> [<span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>],</span><br><span class="line"> [<span class="number">9</span>, <span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>],</span><br><span class="line">]</span><br><span class="line"><span class="comment">#### method1</span></span><br><span class="line">[[row[i] <span class="keyword">for</span> row <span class="keyword">in</span> matrix] <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">4</span>)]</span><br><span class="line"><span class="comment">#[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#### method2</span></span><br><span class="line">res = []</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">4</span>):</span><br><span class="line"> res.append([row[i] <span class="keyword">for</span> row <span class="keyword">in</span> matrix])</span><br><span class="line"></span><br><span class="line"><span class="comment">#### method3</span></span><br><span class="line">res = []</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">4</span>):</span><br><span class="line"> nw = []</span><br><span class="line"> <span class="keyword">for</span> row <span class="keyword">in</span> matrix:</span><br><span class="line"> nw.append(row[i])</span><br><span class="line"> res.append(nw)</span><br></pre></td></tr></table></figure><h4 id="运算符:"><a href="#运算符:" class="headerlink" title="运算符:"></a>运算符:</h4><p>**表示乘方,//表示整除</p><p>:=用于同时赋值且表达式有值的情况</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (n := <span class="built_in">len</span>(a)) > <span class="number">10</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"length is longer than 10"</span>)</span><br></pre></td></tr></table></figure><p>逻辑运算符:not、and、or</p><p>成员运算符:in、not in</p><p>身份运算符:is、is not判断引用对象是否为同一个</p><h3 id="基本数据类型"><a href="#基本数据类型" class="headerlink" title="基本数据类型"></a>基本数据类型</h3><h4 id="Number类型:"><a href="#Number类型:" class="headerlink" title="Number类型:"></a>Number类型:</h4><p>删除引用:del var1</p><p>科学计数法1e5类型为float</p><p>math模块:三角函数、sqrt、pow、ceil、floor、log等函数</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> math</span><br><span class="line"><span class="built_in">print</span>(math.<span class="built_in">pow</span>(x,y))</span><br></pre></td></tr></table></figure><p>random模块:seed、random函数</p><p>max、min多参数</p><h4 id="Str类型:"><a href="#Str类型:" class="headerlink" title="Str类型:"></a>Str类型:</h4><p>+(连接)、*(重复)、[]切片、in、r原义等</p><h5 id="格式化输出output:"><a href="#格式化输出output:" class="headerlink" title="格式化输出output:"></a>格式化输出output:</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"我叫 %s 今年 %d 岁!"</span> % (<span class="string">'小明'</span>, <span class="number">10</span>)</span><br><span class="line"><span class="comment">#%5.3f表示占5格保留3位小数</span></span><br><span class="line"></span><br><span class="line">name = <span class="string">'cz'</span> ; age = <span class="number">19</span></span><br><span class="line"><span class="string">f'我叫 <span class="subst">{name}</span> 今年 <span class="subst">{age}</span> 岁!'</span></span><br><span class="line"></span><br><span class="line"><span class="string">'{0}网址: "{site}!"'</span>.<span class="built_in">format</span>(<span class="string">'菜鸟教程'</span>, site = <span class="string">'www.runoob.com'</span>))</span><br><span class="line"><span class="comment">#{}里加:5d表示占5格,:5f表示保留五位小数</span></span><br><span class="line"><span class="comment">#:10.5表示占10格5位有效数字,:10.5f占10格5位小数</span></span><br></pre></td></tr></table></figure><table><thead><tr><th align="center">capitalize()</th><th align="left">将字符串的第一个字符转换为大写</th></tr></thead><tbody><tr><td align="center">count(str, beg= 0,end=len(string))</td><td align="left">返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数</td></tr><tr><td align="center">endswith(suffix, beg=0, end=len(string))</td><td align="left">检查字符串是否以 obj 结束,如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束,如果是,返回 True,否则返回 False.</td></tr><tr><td align="center">find(str, beg=0, end=len(string))</td><td align="left">检测 str 是否包含在字符串中,如果指定范围 beg 和 end ,则检查是否包含在指定范围内,如果包含返回开始的索引值,否则返回-1</td></tr><tr><td align="center">index(str, beg=0, end=len(string))</td><td align="left">跟find()方法一样,只不过如果str不在字符串中会报一个异常。</td></tr><tr><td align="center">isalnum()</td><td align="left">如果字符串至少有一个字符并且所有字符都是字母或数字则返 回 True,否则返回 False</td></tr><tr><td align="center">isalpha()</td><td align="left">如果字符串至少有一个字符并且所有字符都是字母或中文字则返回 True, 否则返回 False</td></tr><tr><td align="center">isdigit()</td><td align="left">如果字符串只包含数字则返回 True 否则返回 False..</td></tr><tr><td align="center">islower()</td><td align="left">如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False</td></tr><tr><td align="center">isnumeric()</td><td align="left">如果字符串中只包含数字字符,则返回 True,否则返回 False</td></tr><tr><td align="center">isspace()</td><td align="left">如果字符串中只包含空白,则返回 True,否则返回 False.</td></tr><tr><td align="center">istitle()</td><td align="left">如果字符串是标题化的(见 title())则返回 True,否则返回 False</td></tr><tr><td align="center">isupper()</td><td align="left">如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False</td></tr><tr><td align="center">join(seq)</td><td align="left">以指定字符串作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串</td></tr><tr><td align="center">len(string)</td><td align="left">返回字符串长度</td></tr><tr><td align="center">lower()</td><td align="left">转换字符串中所有大写字符为小写.</td></tr><tr><td align="center">max(str)</td><td align="left">返回字符串 str 中最大的字母。</td></tr><tr><td align="center">min(str)</td><td align="left">返回字符串 str 中最小的字母。</td></tr><tr><td align="center">replace(old, new <a href="https://www.runoob.com/python3/python3-string-replace.html">, max)</a></td><td align="left">把 将字符串中的 old 替换成 new,如果 max 指定,则替换不超过 max 次。</td></tr><tr><td align="center">rstrip()</td><td align="left">删除字符串末尾的空格或指定字符。</td></tr><tr><td align="center">split(sep=””, maxsplit=string.count(str))</td><td align="left">以 sep 为分隔符截取字符串,如果 maxsplit 有指定值(默认为-1),则仅截取其+1 个子字符串,分隔后类型为list</td></tr><tr><td align="center">splitlines(keepends)</td><td align="left">按照行(‘\r’, ‘\r\n’, \n’)分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。</td></tr><tr><td align="center">startswith(substr, beg=0,end=len(string))</td><td align="left">检查字符串是否是以指定子字符串 substr 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查。</td></tr><tr><td align="center">strip(chars)</td><td align="left">在字符串上执行 lstrip()和 rstrip()</td></tr><tr><td align="center">swapcase()</td><td align="left">将字符串中大写转换为小写,小写转换为大写</td></tr><tr><td align="center">title()</td><td align="left">返回”标题化”的字符串,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())</td></tr><tr><td align="center">upper()</td><td align="left">转换字符串中的小写字母为大写</td></tr><tr><td align="center">str.rjust(width[, fillchar])</td><td align="left">右对齐,设置宽度</td></tr></tbody></table><h4 id="List类型:"><a href="#List类型:" class="headerlink" title="List类型:"></a>List类型:</h4><p>删除列表元素 del list[1]</p><p>list / str都可以切片:lis[0 : -1 : 2],第一位起点,第二位终点 (不包含),-1、-2…表示从后往前索引,第三位表示step</p><p>lis[-1::-1] 可将原列表倒序</p><h5 id="List遍历:"><a href="#List遍历:" class="headerlink" title="List遍历:"></a>List遍历:</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">####enumerate()函数同时得到索引位置和对应值</span></span><br><span class="line"><span class="keyword">for</span> i, v <span class="keyword">in</span> <span class="built_in">enumerate</span>([<span class="string">'tic'</span>, <span class="string">'tac'</span>]):</span><br><span class="line"><span class="built_in">print</span>(i, v)</span><br><span class="line"><span class="comment">#0 tic</span></span><br><span class="line"><span class="comment">#1 tac</span></span><br><span class="line"></span><br><span class="line"><span class="comment">####同时遍历两个或更多的序列,可以使用 zip() 组合:</span></span><br><span class="line">questions = [<span class="string">'name'</span>, <span class="string">'favorite color'</span>]</span><br><span class="line">answers = [<span class="string">'lancelot'</span>, <span class="string">'blue'</span>]</span><br><span class="line"><span class="keyword">for</span> q, a <span class="keyword">in</span> <span class="built_in">zip</span>(questions, answers):</span><br><span class="line"><span class="built_in">print</span>(<span class="string">'What is your {0}? It is {1}.'</span>.<span class="built_in">format</span>(q, a))</span><br><span class="line"><span class="comment">#What is your name? It is lancelot.</span></span><br><span class="line"><span class="comment">#What is your favorite color? It is blue.</span></span><br><span class="line"></span><br><span class="line"><span class="comment">####反向遍历序列</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">reversed</span>(lis)</span><br><span class="line"></span><br><span class="line"><span class="comment">####用for循环遍历可切片序列时利用切片确定遍历范围</span></span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> data[<span class="number">1</span>:]:</span><br><span class="line"> <span class="comment">#...</span></span><br></pre></td></tr></table></figure><table><thead><tr><th align="center">list.append(obj)</th><th align="left">在列表末尾添加新的对象</th></tr></thead><tbody><tr><td align="center">list.count(obj)</td><td align="left">统计某个元素在列表中出现的次数</td></tr><tr><td align="center">list.extend(seq)</td><td align="left">在列表末尾一次性追加另一个序列中的多个值,可不同类型</td></tr><tr><td align="center">list.index(obj)</td><td align="left">从列表中找出某个值第一个匹配项的索引位置</td></tr><tr><td align="center">list.insert(index, obj)</td><td align="left">将对象插入列表</td></tr><tr><td align="center">list.pop(index=-1)</td><td align="left">移除列表中的一个元素(默认最后一个元素),并且返回该元素的值</td></tr><tr><td align="center">list.remove(obj)</td><td align="left">移除列表中某个值的第一个匹配项</td></tr><tr><td align="center">list.reverse()</td><td align="left">反向列表中元素</td></tr><tr><td align="center">list.sort( key=None, reverse=False)</td><td align="left">对原列表进行排序</td></tr><tr><td align="center">list.clear()</td><td align="left">清空列表</td></tr><tr><td align="center">list.copy()</td><td align="left">复制列表,类似lis[:]</td></tr></tbody></table><h4 id="Tuple类型:"><a href="#Tuple类型:" class="headerlink" title="Tuple类型:"></a>Tuple类型:</h4><p>元组可以输出部分和连接 (+)、切片,但不能修改元素值</p><p>字符串可以看成特殊的元组</p><p>一个元素的元组要添加逗号</p><p>多个变量用逗号隔开默认为元组类型</p><h4 id="Dict类型:"><a href="#Dict类型:" class="headerlink" title="Dict类型:"></a>Dict类型:</h4><p>用 {} 、dict() 创建空字典</p><p>del 键值,删除字典元素</p><p>字典的键值不能是可变数据</p><h5 id="字典dict的创建方法:"><a href="#字典dict的创建方法:" class="headerlink" title="字典dict的创建方法:"></a>字典dict的创建方法:</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#字典推导式创建字典:</span></span><br><span class="line">{x: x**<span class="number">2</span> <span class="keyword">for</span> x <span class="keyword">in</span> (<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>)}</span><br><span class="line"><span class="comment">#{2: 4, 4: 16, 6: 36}</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#直接键赋值增加条目</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#关键字参数指定键值创建字典(关键字只为字符串时):</span></span><br><span class="line"><span class="built_in">dict</span>(sape=<span class="number">4139</span>, guido=<span class="number">4127</span>, jack=<span class="number">4098</span>)</span><br><span class="line"><span class="comment">#{'sape': 4139, 'jack': 4098, 'guido': 4127}</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#键值对元组列表:</span></span><br><span class="line"><span class="built_in">dict</span>([(<span class="string">'sape'</span>, <span class="number">4139</span>), (<span class="string">'guido'</span>, <span class="number">4127</span>), (<span class="string">'jack'</span>, <span class="number">4098</span>)])</span><br><span class="line"><span class="comment">#{'sape': 4139, 'jack': 4098, 'guido': 4127}</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#zip函数</span></span><br><span class="line"><span class="built_in">dict</span>(<span class="built_in">zip</span>([<span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'three'</span>], [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]))</span><br><span class="line"><span class="comment">#{'three': 3, 'two': 2, 'one': 1} </span></span><br></pre></td></tr></table></figure><h5 id="字典dict的遍历:"><a href="#字典dict的遍历:" class="headerlink" title="字典dict的遍历:"></a>字典dict的遍历:</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">knights = {<span class="string">'gallahad'</span>: <span class="string">'the pure'</span>, <span class="string">'robin'</span>: <span class="string">'the brave'</span>}</span><br><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> knights.items():</span><br><span class="line"><span class="built_in">print</span>(k, v)</span><br><span class="line"><span class="comment">#gallahad the pure</span></span><br><span class="line"><span class="comment">#robin the brave</span></span><br></pre></td></tr></table></figure><table><thead><tr><th align="center">dict.clear()</th><th align="left">删除字典内所有元素</th></tr></thead><tbody><tr><td align="center">dict.copy()</td><td align="left">返回一个字典的浅复制</td></tr><tr><td align="center">dict.fromkeys(seq, val)</td><td align="left">创建一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值</td></tr><tr><td align="center">dict.get(key, default=None)</td><td align="left">返回指定键的值,如果键不在字典中返回 default 设置的默认值</td></tr><tr><td align="center">key in dict</td><td align="left">如果键在字典dict里返回true,否则返回false</td></tr><tr><td align="center">dict.items()</td><td align="left">返回一个视图对象</td></tr><tr><td align="center">dict.keys()</td><td align="left">返回一个视图对象</td></tr><tr><td align="center">dict.setdefault(key, default=None)</td><td align="left">和get()类似, 但如果键不存在于字典中,将会添加键并将值设为default,并且返回default</td></tr><tr><td align="center">dict.update(dict2)</td><td align="left">把字典dict2的键/值对更新到dict里,无返回值</td></tr><tr><td align="center">dict.values()</td><td align="left">返回一个视图对象</td></tr><tr><td align="center">pop(key,default)</td><td align="left">删除字典中键 key所对应的值,返回被删除的值,当键 key 不存在时返回default 的值</td></tr><tr><td align="center">popitem()</td><td align="left">返回并删除字典中的最后一对键和值,无返回值</td></tr></tbody></table><p>dict.items() 单独取出一个时类型为tuple</p><h4 id="Set类型:"><a href="#Set类型:" class="headerlink" title="Set类型:"></a>Set类型:</h4><p>创建空集合要用 set() 不能用 {}</p><h5 id="集合set运算符:"><a href="#集合set运算符:" class="headerlink" title="集合set运算符:"></a>集合set运算符:</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">a - b <span class="comment"># 集合a中包含而集合b中不包含的元素</span></span><br><span class="line"> <span class="comment"># {"apple", "banana", "cherry"} - {"google", "apple"} = {'banana', 'cherry'}</span></span><br><span class="line">a | b <span class="comment"># 集合a或b中包含的所有元素</span></span><br><span class="line">a & b <span class="comment"># 集合a和b中都包含了的元素</span></span><br><span class="line">a ^ b <span class="comment"># 不同时包含于a和b的元素</span></span><br></pre></td></tr></table></figure><table><thead><tr><th align="center">方法</th><th align="left">描述</th></tr></thead><tbody><tr><td align="center">add()</td><td align="left">为集合添加元素</td></tr><tr><td align="center">clear()</td><td align="left">移除集合中的所有元素</td></tr><tr><td align="center">copy()</td><td align="left">拷贝一个集合</td></tr><tr><td align="center">discard()</td><td align="left">删除集合中指定的元素,不存在不会报错</td></tr><tr><td align="center">isdisjoint()</td><td align="left">判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False。</td></tr><tr><td align="center">x.issubset(y)</td><td align="left">判断指定 x 集合是否为参数集合 y 的子集。</td></tr><tr><td align="center">x.issuperset(y)</td><td align="left">判断参数集合 y 是否为集合 x 的子集</td></tr><tr><td align="center">pop()</td><td align="left">随机移除元素</td></tr><tr><td align="center">remove()</td><td align="left">移除指定元素,元素不存在会报错</td></tr><tr><td align="center">symmetric_difference()</td><td align="left">返回两个集合组成的新集合,但会移除两个集合的重复元素:</td></tr><tr><td align="center">update(seq)</td><td align="left">给集合添加元素,参数可以是list、tuple等,可以多个</td></tr></tbody></table><h4 id="迭代器、生成器:"><a href="#迭代器、生成器:" class="headerlink" title="迭代器、生成器:"></a>迭代器、生成器:</h4><p>字符串,列表或元组对象都可用于创建迭代器:使用 next() 和 iter() 函数</p><p>迭代器对象可以用常规 for 语句遍历</p><h4 id="函数Func:"><a href="#函数Func:" class="headerlink" title="函数Func:"></a>函数Func:</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">func</span>(<span class="params">x</span>):</span><br><span class="line"> <span class="keyword">return</span> x**<span class="number">2</span></span><br></pre></td></tr></table></figure><p>函数参数为可变对象和不可变对象时实现不同:</p><p>1、变量无类型,变量只是对象的引用(指针),指向的具体对象有类型</p><p>2、不可变对象:对象在分配了地址后不可在地址上对对象的具体值进行修改</p><p>3、可变对象:可以在地址上修改对象的具体值</p><p>4、函数参数默认为形参,但是形参与实参相当于指向同一地址的指针</p><p>5、若参数为可变对象,且函数中修改了形参所指向地址的值,则也会对实参造成影响,其他情况下函数对实参没有影响</p><p>无返回值的函数或只写了return的函数返回值默认为 None</p><p>函数内可以访问全局变量,但不能修改其值!用 global 引用以更新变量值</p><h5 id="关键字参数:"><a href="#关键字参数:" class="headerlink" title="关键字参数:"></a>关键字参数:</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#使用关键字参数允许函数调用时参数的顺序与声明时不一致,因为 Python 解释器能够用参数名匹配参数值。</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">printi</span>(<span class="params">name, age</span>):</span><br><span class="line"> <span class="built_in">print</span> (<span class="string">"名字: "</span>, name)</span><br><span class="line"> <span class="built_in">print</span> (<span class="string">"年龄: "</span>, age)</span><br><span class="line">printi(age = <span class="number">50</span>, name = <span class="string">"runoob"</span>)</span><br></pre></td></tr></table></figure><h5 id="默认参数:"><a href="#默认参数:" class="headerlink" title="默认参数:"></a>默认参数:</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">printi</span>(<span class="params">name, age = <span class="number">35</span></span>): <span class="comment">#若age没有传递参数则会使用默认参数</span></span><br><span class="line"> <span class="built_in">print</span> (<span class="string">"名字: "</span>, name)</span><br><span class="line"> <span class="built_in">print</span> (<span class="string">"年龄: "</span>, age)</span><br><span class="line">printi(age = <span class="number">50</span>, name = <span class="string">"runoob"</span>)</span><br><span class="line"><span class="built_in">print</span> (<span class="string">"------------------------"</span>)</span><br><span class="line">printi(name = <span class="string">"runoob"</span>)</span><br></pre></td></tr></table></figure><p><strong>!!!默认参数只能放在参数最后面</strong></p><h5 id="不定长参数:"><a href="#不定长参数:" class="headerlink" title="不定长参数:"></a>不定长参数:</h5><p>加了星号 ***** 的参数会以元组(tuple)的形式导入,存放所有未命名的变量参数。</p><p>如果在函数调用时加 ***** 参数没有指定,它就是一个空元组。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">fp</span>(<span class="params">arg1, *var</span>): <span class="comment"># **var则以字典形式导入</span></span><br><span class="line"> <span class="built_in">print</span> (arg1)</span><br><span class="line"> <span class="built_in">print</span> (var)</span><br><span class="line">fp(<span class="number">70</span>, <span class="number">60</span>, <span class="number">50</span>)</span><br><span class="line"><span class="comment">#输出:-----------------------</span></span><br><span class="line"><span class="number">70</span></span><br><span class="line">(<span class="number">60</span>, <span class="number">50</span>)</span><br></pre></td></tr></table></figure><p>加了两个星号 *<strong>*</strong> 的参数会以字典的形式导入。</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fp(<span class="number">1</span>, a=<span class="number">2</span>, b=<span class="number">3</span>) <span class="comment">#字典形式导入的函数,调用以'键=值' 的形式</span></span><br></pre></td></tr></table></figure><p>声明函数时,如果单独出现 *****,则 ***** 后的参数必须用关键字传入:</p><p>声明函数时,如果单独出现 **/**,则 <strong>/</strong> 前的参数必须指定位置传入:</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">f</span>(<span class="params">a, b, /, c, d, *, e, f</span>):</span><br><span class="line"> <span class="comment">#...</span></span><br><span class="line">f(<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>, d=<span class="number">40</span>, f=<span class="number">60</span>, e=<span class="number">50</span>)</span><br></pre></td></tr></table></figure><h5 id="匿名函数lambda:"><a href="#匿名函数lambda:" class="headerlink" title="匿名函数lambda:"></a>匿名函数lambda:</h5><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sum</span> = <span class="keyword">lambda</span> arg1, arg2 : arg1 + arg2</span><br><span class="line"><span class="string">'''相当于:</span></span><br><span class="line"><span class="string">def f(arg1, arg2):</span></span><br><span class="line"><span class="string"> return arg1 + arg2</span></span><br><span class="line"><span class="string">sum = f</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="comment">#调用:</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">sum</span>(<span class="number">10</span>, <span class="number">20</span>))</span><br></pre></td></tr></table></figure><p>将匿名函数封装在另一个函数中,通过传入不同参数得到不同的匿名函数:</p><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">def</span> <span class="title function_">myfunc</span>(<span class="params">n</span>):</span><br><span class="line"><span class="keyword">return</span> <span class="keyword">lambda</span> a : a * n</span><br><span class="line">mydoubler = myfunc(<span class="number">2</span>)</span><br><span class="line">mytripler = myfunc(<span class="number">3</span>)</span><br><span class="line"> </span><br><span class="line"><span class="built_in">print</span>(mydoubler(<span class="number">11</span>))</span><br><span class="line"><span class="built_in">print</span>(mytripler(<span class="number">11</span>))</span><br><span class="line"><span class="comment">#输出:---------------</span></span><br><span class="line"><span class="number">22</span></span><br><span class="line"><span class="number">33</span></span><br><span class="line"><span class="string">'''</span></span><br><span class="line"><span class="string">相当于:</span></span><br><span class="line"><span class="string">mydoubler = lambda a: a * 2</span></span><br><span class="line"><span class="string">mytripler = lambda a: a * 3</span></span><br><span class="line"><span class="string">'''</span></span><br></pre></td></tr></table></figure><h4 id="内置函数:"><a href="#内置函数:" class="headerlink" title="内置函数:"></a>内置函数:</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">eval</span>(<span class="built_in">str</span>) <span class="comment">#函数用来执行一个字符串表达式,并返回表达式的值。</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">enumerate</span>(seq) <span class="comment">#函数用于seq,列出数据和数据下标</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">filter</span>(function, seq) <span class="comment">#返回迭代器对象,序列的每个元素作为参数传递给函数进行判断,将返回 True 的元素放到可迭代对象</span></span><br><span class="line"></span><br><span class="line"><span class="string">'{0} {1}'</span>.<span class="built_in">format</span>() <span class="comment">#格式化函数</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">all</span>(seq) <span class="built_in">any</span>(seq) <span class="comment">#all() 函数判断给定的iter中的所有元素是否都为TRUE,any() 函数判断是否至少有一个TRUE</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">input</span>() <span class="comment">#函数输入默认为str类型</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">len</span>() <span class="comment">#返回对象长度</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">max</span>() <span class="built_in">min</span>() <span class="built_in">sum</span>() <span class="comment">#序列为参数</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">map</span>(function, seq) <span class="comment">#根据提供的函数对指定序列做映射,以参数序列中每一个元素调用 function 函数,返回包含每次 function 函数返回值的可迭代对象</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(*objects, sep=<span class="string">' '</span>, end=<span class="string">'\n'</span>, file=sys.stdout, flush=<span class="literal">False</span>)</span><br><span class="line"><span class="comment">#sep间隔,默认空格; end默认换行;</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">range</span>(start, stop, step) <span class="comment">#左闭右开,返回可迭代对象</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">zip</span>() <span class="comment">#将序列中对应位置的元素打包成元组,返回迭代器对象,zip(*可迭代元组对象可以解压)</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">round</span>(num[, pos]) <span class="comment">#将num保留至第pos位小数(就近)</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">reversed</span>(seq) <span class="comment">#返回反转的迭代器</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">sorted</span>(seq, key = <span class="literal">None</span>, reverse = <span class="literal">False</span>) <span class="comment">#接收任何iter,reverse选择是否降序,key是一个函数(匿名函数),一般对原元素进行某种操作</span></span><br></pre></td></tr></table></figure><h4 id="模块:"><a href="#模块:" class="headerlink" title="模块:"></a>模块:</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> random</span><br><span class="line">random.seed() <span class="comment">#初始化随机数生成器</span></span><br><span class="line">random.random() <span class="comment">#[0, 1.0)</span></span><br><span class="line">random.randrange(start, stop, step)</span><br><span class="line">random.randint(a, b) <span class="comment">#a<=N<=b</span></span><br></pre></td></tr></table></figure>]]></content>
<summary type="html"><p>按老师要求总算在开学前给python最基础的部分过了一遍,</p>
<p>把一些刚学可能遗忘的知识点记录<del>粘贴</del>于此,便于日后回忆<del>ctrl+f</del></p></summary>
<category term="Python" scheme="http://example.com/tags/Python/"/>
<category term="study" scheme="http://example.com/tags/study/"/>
</entry>
<entry>
<title>Hello World</title>
<link href="http://example.com/2022/08/27/hello-world/"/>
<id>http://example.com/2022/08/27/hello-world/</id>
<published>2022-08-27T09:33:03.129Z</published>
<updated>2022-09-07T14:29:22.579Z</updated>
<content type="html"><![CDATA[<p>Welcome to <a href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p><span id="more"></span><h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></table></figure><p>More info: <a href="https://hexo.io/docs/writing.html">Writing</a></p><h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></table></figure><p>More info: <a href="https://hexo.io/docs/server.html">Server</a></p><h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></table></figure><p>More info: <a href="https://hexo.io/docs/generating.html">Generating</a></p><h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></table></figure><p>More info: <a href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>]]></content>
<summary type="html"><p>Welcome to <a href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p></summary>
</entry>
</feed>