Python的collections原来这么好用!

浪尽此生 提交于 2021-01-12 15:35:26

<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="font-size: 16px; color: black; padding: 0 10px; line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; word-break: break-word; word-wrap: break-word; text-align: left; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;"><p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">collections是实现了特定目标的容器,以提供Python标准内建容器 dict , list , set , 和 tuple 的替代选择。为了让大家更好的认识,本文详细总结collections的相关知识,一起来学习吧!</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">collections模块:实现了特定目标的容器,以提供Python标准内建容器 dict、list、set、tuple 的替代选择。</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: disc;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">Counter:字典的子类,提供了可哈希对象的计数功能。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">defaultdict:字典的子类,提供了一个工厂函数,为字典查询提供了默认值。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">OrderedDict:字典的子类,保留了他们被添加的顺序。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">namedtuple:创建命名元组子类的工厂函数。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">deque:类似列表容器,实现了在两端快速添加(append)和弹出(pop)。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">ChainMap:类似字典的容器类,将多个映射集合到一个视图里面。</p> </section></li></ul> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;"><strong style="font-weight: bold; color: black;">Counter</strong></p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">Counter是一个dict子类,主要是用来对你访问的对象的频率进行计数。</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;import&nbsp;collections<br>&gt;&gt;&gt;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;统计字符出现的次数</span><br>...&nbsp;collections.Counter(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello&nbsp;world'</span>)<br>Counter({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'l'</span>:&nbsp;3,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'o'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'h'</span>:&nbsp;1,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'e'</span>:&nbsp;1,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'&nbsp;'</span>:&nbsp;1,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'w'</span>:&nbsp;1,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'r'</span>:&nbsp;1,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'d'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;统计单词个数</span><br>...&nbsp;collections.Counter(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello&nbsp;world&nbsp;hello&nbsp;lucy'</span>.split())<br>Counter({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'world'</span>:&nbsp;1,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'lucy'</span>:&nbsp;1})<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">常用方法:</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: disc;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">elements():返回一个迭代器,每个元素重复计算的个数,如果一个元素的计数小于1,就会被忽略。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">most_common([n]):返回一个列表,提供n个访问频率最高的元素和计数</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">subtract([iterable-or-mapping]):从迭代对象中减去元素,输入输出可以是0或者负数</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">update([iterable-or-mapping]):从迭代对象计数元素或者从另一个 映射对象 (或计数器) 添加。</p> </section></li></ul> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;c&nbsp;=&nbsp;collections.Counter(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello&nbsp;world&nbsp;hello&nbsp;lucy'</span>.split())<br>&gt;&gt;&gt;&nbsp;c<br>Counter({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'world'</span>:&nbsp;1,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'lucy'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;获取指定对象的访问次数,也可以使用get方法</span><br>...&nbsp;c[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>]<br>2<br>&gt;&gt;&gt;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;查看元素</span><br>...&nbsp;list(c.elements())<br>[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'world'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'lucy'</span>]<br>&gt;&gt;&gt;&nbsp;c1&nbsp;=&nbsp;collections.Counter(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello&nbsp;world'</span>.split())<br>&gt;&gt;&gt;&nbsp;c2&nbsp;=&nbsp;collections.Counter(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello&nbsp;lucy'</span>.split())<br>&gt;&gt;&gt;&nbsp;c1<br>Counter({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>:&nbsp;1,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'world'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;c2<br>Counter({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>:&nbsp;1,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'lucy'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;追加对象,+或者c1.update(c2)</span><br>...&nbsp;c1+c2<br>Counter({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'world'</span>:&nbsp;1,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'lucy'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;减少对象,-或者c1.subtract(c2)</span><br>...&nbsp;c1-c2<br>Counter({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'world'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;清除</span><br>...&nbsp;c.clear()<br>&gt;&gt;&gt;&nbsp;c<br>Counter()<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;"><strong style="font-weight: bold; color: black;">defaultdict</strong></p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">返回一个新的类似字典的对象。defaultdict 是内置 dict 类的子类。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">class collections.defaultdict([default_factory[, ...]])</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;d&nbsp;=&nbsp;collections.defaultdict()<br>&gt;&gt;&gt;&nbsp;d<br>defaultdict(None,&nbsp;{})<br>&gt;&gt;&gt;&nbsp;e&nbsp;=&nbsp;collections.defaultdict(str)<br>&gt;&gt;&gt;&nbsp;e<br>defaultdict(&lt;class&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'str'</span>&gt;,&nbsp;{})<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">例子</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">defaultdict的一个典型用法是使用其中一种内置类型(如str、int、list或dict等)作为默认工厂,这些内置类型在没有参数调用时返回空类型。</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;e&nbsp;=&nbsp;collections.defaultdict(str)<br>&gt;&gt;&gt;&nbsp;e<br>defaultdict(&lt;class&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'str'</span>&gt;,&nbsp;{})<br>&gt;&gt;&gt;&nbsp;e[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>]<br><span class="hljs-string" style="color: #98c379; line-height: 26px;">''</span><br>&gt;&gt;&gt;&nbsp;e<br>defaultdict(&lt;class&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'str'</span>&gt;,&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>:&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">''</span>})<br>&gt;&gt;&gt;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;普通字典调用不存在的键时,报错</span><br>...&nbsp;e1&nbsp;=&nbsp;{}<br>&gt;&gt;&gt;&nbsp;e1[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>]<br>Traceback&nbsp;(most&nbsp;recent&nbsp;call&nbsp;last):<br>&nbsp;&nbsp;File&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">"&lt;stdin&gt;"</span>,&nbsp;line&nbsp;1,&nbsp;<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span>&nbsp;&lt;module&gt;<br>KeyError:&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span><br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">使用 int 作为 default_factory</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;fruit&nbsp;=&nbsp;collections.defaultdict(int)<br>&gt;&gt;&gt;&nbsp;fruit[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>]&nbsp;=&nbsp;2<br>&gt;&gt;&gt;&nbsp;fruit<br>defaultdict(&lt;class&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'int'</span>&gt;,&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>:&nbsp;2})<br>&gt;&gt;&gt;&nbsp;fruit[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'banana'</span>]&nbsp;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;没有对象时,返回0</span><br>0<br>&gt;&gt;&gt;&nbsp;fruit<br>defaultdict(&lt;class&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'int'</span>&gt;,&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'banana'</span>:&nbsp;0})<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">使用 list 作为 default_factory</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;s&nbsp;=&nbsp;[(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'yellow'</span>,&nbsp;1),&nbsp;(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'blue'</span>,&nbsp;2),&nbsp;(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'yellow'</span>,&nbsp;3),&nbsp;(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'blue'</span>,&nbsp;4),&nbsp;(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'red'</span>,&nbsp;1)]<br>&gt;&gt;&gt;&nbsp;d&nbsp;=&nbsp;collections.defaultdict(list)<br>&gt;&gt;&gt;&nbsp;<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span>&nbsp;k,v&nbsp;<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span>&nbsp;s:<br>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d[k].append(v)<br>...<br>&gt;&gt;&gt;&nbsp;d<br>defaultdict(&lt;class&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'list'</span>&gt;,&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'yellow'</span>:&nbsp;[1,&nbsp;3],&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'blue'</span>:&nbsp;[2,&nbsp;4],&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'red'</span>:&nbsp;[1]})<br>&gt;&gt;&gt;&nbsp;d.items()<br>dict_items([(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'yellow'</span>,&nbsp;[1,&nbsp;3]),&nbsp;(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'blue'</span>,&nbsp;[2,&nbsp;4]),&nbsp;(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'red'</span>,&nbsp;[1])])<br>&gt;&gt;&gt;&nbsp;sorted(d.items())<br>[(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'blue'</span>,&nbsp;[2,&nbsp;4]),&nbsp;(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'red'</span>,&nbsp;[1]),&nbsp;(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'yellow'</span>,&nbsp;[1,&nbsp;3])]<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">使用 dict 作为 default_factory</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;nums&nbsp;=&nbsp;collections.defaultdict(dict)<br>&gt;&gt;&gt;&nbsp;nums[1]&nbsp;=&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'one'</span>:1}<br>&gt;&gt;&gt;&nbsp;nums<br>defaultdict(&lt;class&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'dict'</span>&gt;,&nbsp;{1:&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'one'</span>:&nbsp;1}})<br>&gt;&gt;&gt;&nbsp;nums[2]<br>{}<br>&gt;&gt;&gt;&nbsp;nums<br>defaultdict(&lt;class&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'dict'</span>&gt;,&nbsp;{1:&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'one'</span>:&nbsp;1},&nbsp;2:&nbsp;{}})<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">使用 set 作为 default_factory</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;types&nbsp;=&nbsp;collections.defaultdict(<span class="hljs-built_in" style="color: #e6c07b; line-height: 26px;">set</span>)<br>&gt;&gt;&gt;&nbsp;types[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'手机'</span>].add(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'华为'</span>)<br>&gt;&gt;&gt;&nbsp;types[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'手机'</span>].add(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'小米'</span>)<br>&gt;&gt;&gt;&nbsp;types[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'显示器'</span>].add(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'AOC'</span>)<br>&gt;&gt;&gt;&nbsp;types<br>defaultdict(&lt;class&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'set'</span>&gt;,&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'手机'</span>:&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'华为'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'小米'</span>},&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'显示器'</span>:&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'AOC'</span>}})<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;"><strong style="font-weight: bold; color: black;">OrderedDict</strong></p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">Python字典中的键的顺序是任意的,它们不受添加的顺序的控制。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">collections.OrderedDict 类提供了保留他们添加顺序的字典对象。</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;o&nbsp;=&nbsp;collections.OrderedDict()<br>&gt;&gt;&gt;&nbsp;o[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k1'</span>]&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'v1'</span><br>&gt;&gt;&gt;&nbsp;o[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k3'</span>]&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'v3'</span><br>&gt;&gt;&gt;&nbsp;o[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k2'</span>]&nbsp;=&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'v2'</span><br>&gt;&gt;&gt;&nbsp;o<br>OrderedDict([(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k1'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'v1'</span>),&nbsp;(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k3'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'v3'</span>),&nbsp;(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k2'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'v2'</span>)])<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">如果在已经存在的 key 上添加新的值,将会保留原来的 key 的位置,然后覆盖 value 值。</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;o[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k1'</span>]&nbsp;=&nbsp;666<br>&gt;&gt;&gt;&nbsp;o<br>OrderedDict([(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k1'</span>,&nbsp;666),&nbsp;(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k3'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'v3'</span>),&nbsp;(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k2'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'v2'</span>)])<br>&gt;&gt;&gt;&nbsp;dict(o)<br>{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k1'</span>:&nbsp;666,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k3'</span>:&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'v3'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k2'</span>:&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'v2'</span>}<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;"><strong style="font-weight: bold; color: black;">namedtuple</strong></p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">三种定义命名元组的方法:第一个参数是命名元组的构造器(如下的:Person1,Person2,Person3)</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;P1&nbsp;=&nbsp;collections.namedtuple(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'Person1'</span>,[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'name'</span>,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'age'</span>,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'height'</span>])<br>&gt;&gt;&gt;&nbsp;P2&nbsp;=&nbsp;collections.namedtuple(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'Person2'</span>,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'name,age,height'</span>)<br>&gt;&gt;&gt;&nbsp;P3&nbsp;=&nbsp;collections.namedtuple(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'Person3'</span>,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'name&nbsp;age&nbsp;height'</span>)<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">实例化命名元组</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;lucy&nbsp;=&nbsp;P1(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'lucy'</span>,23,180)<br>&gt;&gt;&gt;&nbsp;lucy<br>Person1(name=<span class="hljs-string" style="color: #98c379; line-height: 26px;">'lucy'</span>,&nbsp;age=23,&nbsp;height=180)<br>&gt;&gt;&gt;&nbsp;jack&nbsp;=&nbsp;P2(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'jack'</span>,20,190)<br>&gt;&gt;&gt;&nbsp;jack<br>Person2(name=<span class="hljs-string" style="color: #98c379; line-height: 26px;">'jack'</span>,&nbsp;age=20,&nbsp;height=190)<br>&gt;&gt;&gt;&nbsp;lucy.name&nbsp;&nbsp;<span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;">#&nbsp;直接通过&nbsp;实例名.属性&nbsp;来调用</span><br><span class="hljs-string" style="color: #98c379; line-height: 26px;">'lucy'</span><br>&gt;&gt;&gt;&nbsp;lucy.age<br>23<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;"><strong style="font-weight: bold; color: black;">deque</strong></p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">collections.deque 返回一个新的双向队列对象,从左到右初始化(用方法 append()),从 iterable(迭代对象)数据创建。如果 iterable 没有指定,新队列为空。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">collections.deque 队列支持线程安全,对于从两端添加(append)或者弹出(pop),复杂度O(1)。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">虽然 list 对象也支持类似操作,但是这里优化了定长操作(pop(0)、insert(0,v))的开销。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">如果 maxlen 没有指定或者是 None ,deque 可以增长到任意长度。否则,deque 就限定到指定最大长度。一旦限定长度的 deque 满了,当新项加入时,同样数量的项就从另一端弹出。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">支持的方法:</p> <ul data-tool="mdnice编辑器" style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: black; list-style-type: disc;"> <li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">append(x):添加x到右端。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">appendleft(x):添加x到左端。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">clear():清除所有元素,长度变为0。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">copy():创建一份浅拷贝。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">count(x):计算队列中个数等于x的元素。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">extend(iterable):在队列右侧添加iterable中的元素。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">extendleft(iterable):在队列左侧添加iterable中的元素,注:在左侧添加时,iterable参数的顺序将会反过来添加。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">index(x[,start[,stop]]):返回第 x 个元素(从 start 开始计算,在 stop 之前)。返回第一个匹配,如果没找到的话,抛出 ValueError 。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">insert(i,x):在位置 i 插入 x 。注:如果插入会导致一个限长deque超出长度 maxlen 的话,就抛出一个 IndexError 。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">pop():移除最右侧的元素。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">popleft():移除最左侧的元素。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">remove(value):移去找到的第一个 value。没有抛出ValueError。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">reverse():将deque逆序排列。返回 None 。</p> </section></li><li><section style="margin-top: 5px; margin-bottom: 5px; line-height: 26px; text-align: left; color: rgb(1,1,1); font-weight: 500;"><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">maxlen:队列的最大长度,没有限定则为None。</p> </section></li></ul> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;d&nbsp;=&nbsp;collections.deque(maxlen=10)<br>&gt;&gt;&gt;&nbsp;d<br>deque([],&nbsp;maxlen=10)<br>&gt;&gt;&gt;&nbsp;d.extend(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'python'</span>)<br>&gt;&gt;&gt;&nbsp;[i.upper()&nbsp;<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span>&nbsp;i&nbsp;<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span>&nbsp;d]<br>[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'P'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'Y'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'T'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'H'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'O'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'N'</span>]<br>&gt;&gt;&gt;&nbsp;d.append(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'e'</span>)<br>&gt;&gt;&gt;&nbsp;d.appendleft(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'f'</span>)<br>&gt;&gt;&gt;&nbsp;d.appendleft(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'g'</span>)<br>&gt;&gt;&gt;&nbsp;d.appendleft(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'h'</span>)<br>&gt;&gt;&gt;&nbsp;d<br>deque([<span class="hljs-string" style="color: #98c379; line-height: 26px;">'h'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'g'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'f'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'p'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'t'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'h'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'o'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'n'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'e'</span>],&nbsp;maxlen=10)<br>&gt;&gt;&gt;&nbsp;d.appendleft(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'i'</span>)<br>&gt;&gt;&gt;&nbsp;d<br>deque([<span class="hljs-string" style="color: #98c379; line-height: 26px;">'i'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'h'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'g'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'f'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'p'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'t'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'h'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'o'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'n'</span>],&nbsp;maxlen=10)<br>&gt;&gt;&gt;&nbsp;d.append(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'m'</span>)<br>&gt;&gt;&gt;&nbsp;d<br>deque([<span class="hljs-string" style="color: #98c379; line-height: 26px;">'h'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'g'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'f'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'p'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'t'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'h'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'o'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'n'</span>,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'m'</span>],&nbsp;maxlen=10)<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;"><strong style="font-weight: bold; color: black;">ChainMap</strong></p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">问题背景是我们有多个字典或者映射,想把它们合并成为一个单独的映射,有人说可以用update进行合并,这样做的问题就是新建了一个数据结构以致于当我们对原来的字典进行更改的时候不会同步。如果想建立一个同步的查询方法,可以使用 ChainMap。</p> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">可以用来合并两个或者更多个字典,当查询的时候,从前往后依次查询。简单使用:</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;d1&nbsp;=&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>:1,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'banana'</span>:2}<br>&gt;&gt;&gt;&nbsp;d2&nbsp;=&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'orange'</span>:2,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>:3,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'pike'</span>:1}<br>&gt;&gt;&gt;&nbsp;combined1&nbsp;=&nbsp;collections.ChainMap(d1,d2)<br>&gt;&gt;&gt;&nbsp;combined2&nbsp;=&nbsp;collections.ChainMap(d2,d1)<br>&gt;&gt;&gt;&nbsp;combined1<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>:&nbsp;1,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'banana'</span>:&nbsp;2},&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'orange'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>:&nbsp;3,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'pike'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;combined2<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'orange'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>:&nbsp;3,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'pike'</span>:&nbsp;1},&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>:&nbsp;1,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'banana'</span>:&nbsp;2})<br>&gt;&gt;&gt;&nbsp;<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span>&nbsp;k,v&nbsp;<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span>&nbsp;combined1.items():<br>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: #e6c07b; line-height: 26px;">print</span>(k,v)<br>...<br>orange&nbsp;2<br>apple&nbsp;1<br>pike&nbsp;1<br>banana&nbsp;2<br>&gt;&gt;&gt;&nbsp;<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span>&nbsp;k,v&nbsp;<span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span>&nbsp;combined2.items():<br>...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-built_in" style="color: #e6c07b; line-height: 26px;">print</span>(k,v)<br>...<br>apple&nbsp;3<br>banana&nbsp;2<br>orange&nbsp;2<br>pike&nbsp;1<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">有一个注意点就是当对ChainMap进行修改的时候总是只会对第一个字典进行修改,如果第一个字典不存在该键,会添加。</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;d1&nbsp;=&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>:1,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'banana'</span>:2}<br>&gt;&gt;&gt;&nbsp;d2&nbsp;=&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'orange'</span>:2,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>:3,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'pike'</span>:1}<br>&gt;&gt;&gt;&nbsp;c&nbsp;=&nbsp;collections.ChainMap(d1,d2)<br>&gt;&gt;&gt;&nbsp;c<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>:&nbsp;1,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'banana'</span>:&nbsp;2},&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'orange'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>:&nbsp;3,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'pike'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;c[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>]<br>1<br>&gt;&gt;&gt;&nbsp;c[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>]&nbsp;=&nbsp;2<br>&gt;&gt;&gt;&nbsp;c<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'banana'</span>:&nbsp;2},&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'orange'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>:&nbsp;3,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'pike'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;c[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'pike'</span>]<br>1<br>&gt;&gt;&gt;&nbsp;c[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'pike'</span>]&nbsp;=&nbsp;3<br>&gt;&gt;&gt;&nbsp;c<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'banana'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'pike'</span>:&nbsp;3},&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'orange'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>:&nbsp;3,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'pike'</span>:&nbsp;1})<br></code></pre> <p data-tool="mdnice编辑器" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: black;">从原理上面讲,ChainMap 实际上是把放入的字典存储在一个队列中,当进行字典的增加删除等操作只会在第一个字典上进行,当进行查找的时候会依次查找,new_child() 方法实质上是在列表的第一个元素前放入一个字典,默认是{},而 parents 是去掉了列表开头的元素。</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;a&nbsp;=&nbsp;collections.ChainMap()<br>&gt;&gt;&gt;&nbsp;a[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>]&nbsp;=&nbsp;1<br>&gt;&gt;&gt;&nbsp;a<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;b&nbsp;=&nbsp;a.new_child()<br>&gt;&gt;&gt;&nbsp;b<br>ChainMap({},&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;b[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>]&nbsp;=&nbsp;2<br>&gt;&gt;&gt;&nbsp;b<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>:&nbsp;2},&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;b[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>]&nbsp;=&nbsp;3<br>&gt;&gt;&gt;&nbsp;b<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>:&nbsp;3},&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;a<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;c&nbsp;=&nbsp;a.new_child()<br>&gt;&gt;&gt;&nbsp;c<br>ChainMap({},&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;c[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>]&nbsp;=&nbsp;1<br>&gt;&gt;&gt;&nbsp;c[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>]&nbsp;=&nbsp;1<br>&gt;&gt;&gt;&nbsp;c<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>:&nbsp;1,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>:&nbsp;1},&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;d&nbsp;=&nbsp;c.parents<br>&gt;&gt;&gt;&nbsp;d<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>:&nbsp;1})<br>&gt;&gt;&gt;&nbsp;d&nbsp;is&nbsp;a<br>False<br>&gt;&gt;&gt;&nbsp;d&nbsp;==&nbsp;a<br>True<br></code></pre> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">&gt;&gt;&gt;&nbsp;a&nbsp;=&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>:1,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'z'</span>:3}<br>&gt;&gt;&gt;&nbsp;b&nbsp;=&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>:2,<span class="hljs-string" style="color: #98c379; line-height: 26px;">'z'</span>:4}<br>&gt;&gt;&gt;&nbsp;c&nbsp;=&nbsp;collections.ChainMap(a,b)<br>&gt;&gt;&gt;&nbsp;c<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>:&nbsp;1,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'z'</span>:&nbsp;3},&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'z'</span>:&nbsp;4})<br>&gt;&gt;&gt;&nbsp;c.maps<br>[{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>:&nbsp;1,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'z'</span>:&nbsp;3},&nbsp;{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'z'</span>:&nbsp;4}]<br>&gt;&gt;&gt;&nbsp;c.parents<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'z'</span>:&nbsp;4})<br>&gt;&gt;&gt;&nbsp;c.parents.maps<br>[{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>:&nbsp;2,&nbsp;<span class="hljs-string" style="color: #98c379; line-height: 26px;">'z'</span>:&nbsp;4}]<br>&gt;&gt;&gt;&nbsp;c.parents.parents<br>ChainMap({})<br>&gt;&gt;&gt;&nbsp;c.parents.parents.parents<br>ChainMap({})<br></code></pre> </section> [拿走不谢!Python 3.9 官方中文文档,限时领!] (http://dwz.date/dE6v)

[限时!速领!14张高清Python速查表,效率提升必备!] (http://dwz.date/dE6w)

[GitHub标星3W+,80个Python案例,带你轻松玩转Python学习!] (http://dwz.date/dE64)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!