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>

相关文章
相关标签/搜索