Python 關於列表字典的鍵值修改

<h2 id="toc_0">list (修改列表的索引值)</h2>python

<h3 id="toc_1">循環一個列表時,最好不要對原列表有改變大小的操做,這樣會影響你的最終結果。</h3>json

<pre class="line-numbers"><code class="language-python">#使用負索引進行修改列表 print(&#39;First&#39;) lis = [11, 22, 33, 44, 55] print(lis) for num in range(len(lis)-1,-1,-1): if num % 2 != 0: lis.pop(num) else: print(lis) </code></pre>bash

<pre class="line-numbers"><code class="language-python">#使用步長進行修改列表 print(&#39;Second&#39;) lis = [11, 22, 33, 44, 55] print(lis) del lis[1::2] print(lis) </code></pre>app

<pre class="line-numbers"><code class="language-python">#添加新的列表進行修改 print(&#39;Third&#39;) lis = [11, 22, 33, 44, 55] print(lis) new_lis = list() for num in range(len(lis)): if num % 2 == 0: new_lis.append(lis[num]) else: lis = new_lis print(lis) </code></pre>python2.7

<h2 id="toc_2">dict</h2>測試

<h3 id="toc_3">在循環中不能夠改變字典的鍵值對(增長、刪除)</h3>編碼

<h4 id="toc_4">RuntimeError: dictionary changed size during iteration</h4>操作系統

<pre class="line-numbers"><code class="language-python">dict.fromkeys() </code></pre>code

<h4 id="toc_5">dic = {&#39;k1&#39;: &#39;v1&#39;, &#39;k2&#39;: &#39;v2&#39;, &#39;name&#39;: &#39;alex&#39;}</h4>對象

<h5 id="toc_6">錯誤的示範</h5>

<pre class="line-numbers"><code class="language-python">for key in dic: if &#39;k&#39; in key: dic.pop(key) print(dic) </code></pre>

<h5 id="toc_7">修改字典的內容須要把修改的鍵加入到空列表而後遍歷修改字典的值</h5>

<pre class="line-numbers"><code class="language-python">l1 = list() for key in dic: if &#39;k&#39; in key: l1.append(key) print(l1) for key in l1: dic.pop(key) print(dic) </code></pre>

<hr/>

<h3 id="toc_8">ValueError: unknown locale: UTF-8</h3>

<pre class="line-numbers"><code class="language-python">File &quot;/Users/wyl/Documents/effectmatrix/program/minetest/MCEdit-Unified-master/ENV/lib/python2.7/locale.py&quot;, line 545, in getdefaultlocale     return _parse_localename(localename)   File &quot;/Users/wyl/Documents/effectmatrix/program/minetest/MCEdit-Unified-master/ENV/lib/python2.7/locale.py&quot;, line 477, in _parse_localename     raise ValueError, &#39;unknown locale: %s&#39; % localename ValueError: unknown locale: UTF-8 </code></pre>

<h3 id="toc_9">解決方法:</h3>

<pre class="line-numbers"><code class="language-bash">1.在.bash_profile文件中加入  export LANG=&quot;en_US.UTF-8&quot; export LC_COLLATE=&quot;en_US.UTF-8&quot; export LC_CTYPE=&quot;en_US.UTF-8&quot; export LC_MESSAGES=&quot;en_US.UTF-8&quot; export LC_MONETARY=&quot;en_US.UTF-8&quot; export LC_NUMERIC=&quot;en_US.UTF-8&quot; export LC_TIME=&quot;en_US.UTF-8&quot; 2.source 使用更新後的內容 source .bash_profile 測試 python -c &#39;import locale; print(locale.getdefaultlocale());&#39; </code></pre>

<hr/>

<h2 id="toc_10">異常處理</h2>

<table> <thead> <tr> <th>異常名稱</th> <th>描述</th> </tr> </thead>

<tbody> <tr> <td>BaseException</td> <td>全部異常的基類</td> </tr> <tr> <td>SystemExit</td> <td>解釋器請求退出</td> </tr> <tr> <td>KeyboardInterrupt</td> <td>用戶中斷執行(一般是輸入^C)</td> </tr> <tr> <td>Exception</td> <td>常規錯誤的基類</td> </tr> <tr> <td>StopIteration</td> <td>迭代器沒有更多的值</td> </tr> <tr> <td>GeneratorExit</td> <td>生成器(generator)發生異常來通知退出</td> </tr> <tr> <td>StandardError</td> <td>全部的內建標準異常的基類</td> </tr> <tr> <td>ArithmeticError</td> <td>全部數值計算錯誤的基類</td> </tr> <tr> <td>FloatingPointError</td> <td>浮點計算錯誤</td> </tr> <tr> <td>OverflowError</td> <td>數值運算超出最大限制</td> </tr> <tr> <td>ZeroDivisionError</td> <td>除(或取模)零 (全部數據類型)</td> </tr> <tr> <td>AssertionError</td> <td>斷言語句失敗</td> </tr> <tr> <td>AttributeError</td> <td>對象沒有這個屬性</td> </tr> <tr> <td>EOFError</td> <td>沒有內建輸入,到達EOF 標記</td> </tr> <tr> <td>EnvironmentError</td> <td>操做系統錯誤的基類</td> </tr> <tr> <td>IOError</td> <td>輸入/輸出操做失敗</td> </tr> <tr> <td>OSError</td> <td>操做系統錯誤</td> </tr> <tr> <td>WindowsError</td> <td>系統調用失敗</td> </tr> <tr> <td>ImportError</td> <td>導入模塊/對象失敗</td> </tr> <tr> <td>LookupError</td> <td>無效數據查詢的基類</td> </tr> <tr> <td>IndexError</td> <td>序列中沒有此索引(index)</td> </tr> <tr> <td>KeyError</td> <td>映射中沒有這個鍵</td> </tr> <tr> <td>MemoryError</td> <td>內存溢出錯誤(對於Python 解釋器不是致命的)</td> </tr> <tr> <td>NameError</td> <td>未聲明/初始化對象 (沒有屬性)</td> </tr> <tr> <td>UnboundLocalError</td> <td>訪問未初始化的本地變量</td> </tr> <tr> <td>ReferenceError</td> <td>弱引用(Weak reference)試圖訪問已經垃圾回收了的對象</td> </tr> <tr> <td>RuntimeError</td> <td>通常的運行時錯誤</td> </tr> <tr> <td>NotImplementedError</td> <td>還沒有實現的方法</td> </tr> <tr> <td>SyntaxError</td> <td>Python 語法錯誤</td> </tr> <tr> <td>IndentationError</td> <td>縮進錯誤</td> </tr> <tr> <td>TabError</td> <td>Tab 和空格混用</td> </tr> <tr> <td>SystemError</td> <td>通常的解釋器系統錯誤</td> </tr> <tr> <td>TypeError</td> <td>對類型無效的操做</td> </tr> <tr> <td>ValueError</td> <td>傳入無效的參數</td> </tr> <tr> <td>UnicodeError</td> <td>Unicode 相關的錯誤</td> </tr> <tr> <td>UnicodeDecodeError</td> <td>Unicode 解碼時的錯誤</td> </tr> <tr> <td>UnicodeEncodeError</td> <td>Unicode 編碼時錯誤</td> </tr> <tr> <td>UnicodeTranslateError</td> <td>Unicode 轉換時錯誤</td> </tr> <tr> <td>Warning</td> <td>警告的基類</td> </tr> <tr> <td>DeprecationWarning</td> <td>關於被棄用的特徵的警告</td> </tr> <tr> <td>FutureWarning</td> <td>關於構造未來語義會有改變的警告</td> </tr> <tr> <td>OverflowWarning</td> <td>舊的關於自動提高爲長整型(long)的警告</td> </tr> <tr> <td>PendingDeprecationWarning</td> <td>關於特性將會被廢棄的警告</td> </tr> <tr> <td>RuntimeWarning</td> <td>可疑的運行時行爲(runtime behavior)的警告</td> </tr> <tr> <td>SyntaxWarning</td> <td>可疑的語法的警告</td> </tr> <tr> <td>UserWarning</td> <td>用戶代碼生成的警告</td> </tr> </tbody> </table>

<h2 id="toc_11">序列化注意事項:</h2>

<ul> <li>json: 傳入傳出爲字符串</li> <li>文件打開使用&#39;r&#39;模式</li> <li>只能寫一行</li> <li>只支持字典</li> </ul>

<pre class="line-numbers"><code class="language-python">json.dumps(data,sort_keys=True,indent=2,separators=(&#39;,&#39;,&#39;:&#39;),ensure_ascii=False) #sort_keys 排序 #indent 縮進 #separators 分隔符 #ensure_ascii 支持中文 </code></pre>

<ul> <li>pickle:傳入傳出爲bytes類型</li> <li>文件打開使用&#39;rb&#39;模式</li> <li>支持任意類型</li> </ul>

<pre class="line-numbers"><code class="language-python">print(pickle.loads(b&#39;\x80\x03}q\x00X\x01\x00\x00\x001q\x01K\x04s.&#39;)) #字符串的格式爲&#39;bytes&#39;,不是須要&#39;encode()&#39; </code></pre>

<ul> <li>shelve:打開文件的參數增長&#39;writeback=True&#39;防止寫入失敗</li> <li>支持字典</li> </ul>

<pre class="line-numbers"><code class="language-python">import shelve f = shelve.open(&#39;c&#39;,writeback=True) #建立文件 #writeback = True 回寫 </code></pre>

相關文章
相關標籤/搜索