<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;">>>> import collections<br>>>> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 统计字符出现的次数</span><br>... collections.Counter(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello world'</span>)<br>Counter({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'l'</span>: 3, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'o'</span>: 2, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'h'</span>: 1, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'e'</span>: 1, <span class="hljs-string" style="color: #98c379; line-height: 26px;">' '</span>: 1, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'w'</span>: 1, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'r'</span>: 1, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'d'</span>: 1})<br>>>> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 统计单词个数</span><br>... collections.Counter(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello world hello lucy'</span>.split())<br>Counter({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>: 2, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'world'</span>: 1, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'lucy'</span>: 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;">>>> c = collections.Counter(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello world hello lucy'</span>.split())<br>>>> c<br>Counter({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>: 2, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'world'</span>: 1, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'lucy'</span>: 1})<br>>>> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 获取指定对象的访问次数,也可以使用get方法</span><br>... c[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>]<br>2<br>>>> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 查看元素</span><br>... list(c.elements())<br>[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'world'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'lucy'</span>]<br>>>> c1 = collections.Counter(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello world'</span>.split())<br>>>> c2 = collections.Counter(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello lucy'</span>.split())<br>>>> c1<br>Counter({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>: 1, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'world'</span>: 1})<br>>>> c2<br>Counter({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>: 1, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'lucy'</span>: 1})<br>>>> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 追加对象,+或者c1.update(c2)</span><br>... c1+c2<br>Counter({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>: 2, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'world'</span>: 1, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'lucy'</span>: 1})<br>>>> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 减少对象,-或者c1.subtract(c2)</span><br>... c1-c2<br>Counter({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'world'</span>: 1})<br>>>> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 清除</span><br>... c.clear()<br>>>> 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;">>>> d = collections.defaultdict()<br>>>> d<br>defaultdict(None, {})<br>>>> e = collections.defaultdict(str)<br>>>> e<br>defaultdict(<class <span class="hljs-string" style="color: #98c379; line-height: 26px;">'str'</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> <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;">>>> e = collections.defaultdict(str)<br>>>> e<br>defaultdict(<class <span class="hljs-string" style="color: #98c379; line-height: 26px;">'str'</span>>, {})<br>>>> 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>>>> e<br>defaultdict(<class <span class="hljs-string" style="color: #98c379; line-height: 26px;">'str'</span>>, {<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>: <span class="hljs-string" style="color: #98c379; line-height: 26px;">''</span>})<br>>>> <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 普通字典调用不存在的键时,报错</span><br>... e1 = {}<br>>>> e1[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'hello'</span>]<br>Traceback (most recent call last):<br> File <span class="hljs-string" style="color: #98c379; line-height: 26px;">"<stdin>"</span>, line 1, <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> <module><br>KeyError: <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;">>>> fruit = collections.defaultdict(int)<br>>>> fruit[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>] = 2<br>>>> fruit<br>defaultdict(<class <span class="hljs-string" style="color: #98c379; line-height: 26px;">'int'</span>>, {<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>: 2})<br>>>> fruit[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'banana'</span>] <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 没有对象时,返回0</span><br>0<br>>>> fruit<br>defaultdict(<class <span class="hljs-string" style="color: #98c379; line-height: 26px;">'int'</span>>, {<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>: 2, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'banana'</span>: 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;">>>> s = [(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'yellow'</span>, 1), (<span class="hljs-string" style="color: #98c379; line-height: 26px;">'blue'</span>, 2), (<span class="hljs-string" style="color: #98c379; line-height: 26px;">'yellow'</span>, 3), (<span class="hljs-string" style="color: #98c379; line-height: 26px;">'blue'</span>, 4), (<span class="hljs-string" style="color: #98c379; line-height: 26px;">'red'</span>, 1)]<br>>>> d = collections.defaultdict(list)<br>>>> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> k,v <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> s:<br>... d[k].append(v)<br>...<br>>>> d<br>defaultdict(<class <span class="hljs-string" style="color: #98c379; line-height: 26px;">'list'</span>>, {<span class="hljs-string" style="color: #98c379; line-height: 26px;">'yellow'</span>: [1, 3], <span class="hljs-string" style="color: #98c379; line-height: 26px;">'blue'</span>: [2, 4], <span class="hljs-string" style="color: #98c379; line-height: 26px;">'red'</span>: [1]})<br>>>> d.items()<br>dict_items([(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'yellow'</span>, [1, 3]), (<span class="hljs-string" style="color: #98c379; line-height: 26px;">'blue'</span>, [2, 4]), (<span class="hljs-string" style="color: #98c379; line-height: 26px;">'red'</span>, [1])])<br>>>> sorted(d.items())<br>[(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'blue'</span>, [2, 4]), (<span class="hljs-string" style="color: #98c379; line-height: 26px;">'red'</span>, [1]), (<span class="hljs-string" style="color: #98c379; line-height: 26px;">'yellow'</span>, [1, 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;">>>> nums = collections.defaultdict(dict)<br>>>> nums[1] = {<span class="hljs-string" style="color: #98c379; line-height: 26px;">'one'</span>:1}<br>>>> nums<br>defaultdict(<class <span class="hljs-string" style="color: #98c379; line-height: 26px;">'dict'</span>>, {1: {<span class="hljs-string" style="color: #98c379; line-height: 26px;">'one'</span>: 1}})<br>>>> nums[2]<br>{}<br>>>> nums<br>defaultdict(<class <span class="hljs-string" style="color: #98c379; line-height: 26px;">'dict'</span>>, {1: {<span class="hljs-string" style="color: #98c379; line-height: 26px;">'one'</span>: 1}, 2: {}})<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;">>>> types = collections.defaultdict(<span class="hljs-built_in" style="color: #e6c07b; line-height: 26px;">set</span>)<br>>>> 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>>>> 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>>>> 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>>>> types<br>defaultdict(<class <span class="hljs-string" style="color: #98c379; line-height: 26px;">'set'</span>>, {<span class="hljs-string" style="color: #98c379; line-height: 26px;">'手机'</span>: {<span class="hljs-string" style="color: #98c379; line-height: 26px;">'华为'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'小米'</span>}, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'显示器'</span>: {<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;">>>> o = collections.OrderedDict()<br>>>> o[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k1'</span>] = <span class="hljs-string" style="color: #98c379; line-height: 26px;">'v1'</span><br>>>> o[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k3'</span>] = <span class="hljs-string" style="color: #98c379; line-height: 26px;">'v3'</span><br>>>> o[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k2'</span>] = <span class="hljs-string" style="color: #98c379; line-height: 26px;">'v2'</span><br>>>> o<br>OrderedDict([(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k1'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'v1'</span>), (<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k3'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'v3'</span>), (<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k2'</span>, <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;">>>> o[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k1'</span>] = 666<br>>>> o<br>OrderedDict([(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k1'</span>, 666), (<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k3'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'v3'</span>), (<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k2'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'v2'</span>)])<br>>>> dict(o)<br>{<span class="hljs-string" style="color: #98c379; line-height: 26px;">'k1'</span>: 666, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'k3'</span>: <span class="hljs-string" style="color: #98c379; line-height: 26px;">'v3'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'k2'</span>: <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;">>>> P1 = 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>>>> P2 = 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>>>> P3 = 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 age 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;">>>> lucy = P1(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'lucy'</span>,23,180)<br>>>> lucy<br>Person1(name=<span class="hljs-string" style="color: #98c379; line-height: 26px;">'lucy'</span>, age=23, height=180)<br>>>> jack = P2(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'jack'</span>,20,190)<br>>>> jack<br>Person2(name=<span class="hljs-string" style="color: #98c379; line-height: 26px;">'jack'</span>, age=20, height=190)<br>>>> lucy.name <span class="hljs-comment" style="color: #5c6370; font-style: italic; line-height: 26px;"># 直接通过 实例名.属性 来调用</span><br><span class="hljs-string" style="color: #98c379; line-height: 26px;">'lucy'</span><br>>>> 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;">>>> d = collections.deque(maxlen=10)<br>>>> d<br>deque([], maxlen=10)<br>>>> d.extend(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'python'</span>)<br>>>> [i.upper() <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> i <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> d]<br>[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'P'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'Y'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'T'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'H'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'O'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'N'</span>]<br>>>> d.append(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'e'</span>)<br>>>> d.appendleft(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'f'</span>)<br>>>> d.appendleft(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'g'</span>)<br>>>> d.appendleft(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'h'</span>)<br>>>> d<br>deque([<span class="hljs-string" style="color: #98c379; line-height: 26px;">'h'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'g'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'f'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'p'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'t'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'h'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'o'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'n'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'e'</span>], maxlen=10)<br>>>> d.appendleft(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'i'</span>)<br>>>> d<br>deque([<span class="hljs-string" style="color: #98c379; line-height: 26px;">'i'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'h'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'g'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'f'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'p'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'t'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'h'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'o'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'n'</span>], maxlen=10)<br>>>> d.append(<span class="hljs-string" style="color: #98c379; line-height: 26px;">'m'</span>)<br>>>> d<br>deque([<span class="hljs-string" style="color: #98c379; line-height: 26px;">'h'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'g'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'f'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'p'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'t'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'h'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'o'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'n'</span>, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'m'</span>], 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;">>>> d1 = {<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>>>> d2 = {<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>>>> combined1 = collections.ChainMap(d1,d2)<br>>>> combined2 = collections.ChainMap(d2,d1)<br>>>> combined1<br>ChainMap({<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}, {<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>>>> combined2<br>ChainMap({<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}, {<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>>>> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> k,v <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> combined1.items():<br>... <span class="hljs-built_in" style="color: #e6c07b; line-height: 26px;">print</span>(k,v)<br>...<br>orange 2<br>apple 1<br>pike 1<br>banana 2<br>>>> <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">for</span> k,v <span class="hljs-keyword" style="color: #c678dd; line-height: 26px;">in</span> combined2.items():<br>... <span class="hljs-built_in" style="color: #e6c07b; line-height: 26px;">print</span>(k,v)<br>...<br>apple 3<br>banana 2<br>orange 2<br>pike 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;">>>> d1 = {<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>>>> d2 = {<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>>>> c = collections.ChainMap(d1,d2)<br>>>> c<br>ChainMap({<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}, {<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>>>> c[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>]<br>1<br>>>> c[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>] = 2<br>>>> c<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>: 2, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'banana'</span>: 2}, {<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>>>> c[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'pike'</span>]<br>1<br>>>> c[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'pike'</span>] = 3<br>>>> c<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'apple'</span>: 2, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'banana'</span>: 2, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'pike'</span>: 3}, {<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></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;">>>> a = collections.ChainMap()<br>>>> a[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>] = 1<br>>>> a<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>: 1})<br>>>> b = a.new_child()<br>>>> b<br>ChainMap({}, {<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>: 1})<br>>>> b[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>] = 2<br>>>> b<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>: 2}, {<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>: 1})<br>>>> b[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>] = 3<br>>>> b<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>: 2, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>: 3}, {<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>: 1})<br>>>> a<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>: 1})<br>>>> c = a.new_child()<br>>>> c<br>ChainMap({}, {<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>: 1})<br>>>> c[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>] = 1<br>>>> c[<span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>] = 1<br>>>> c<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>: 1, <span class="hljs-string" style="color: #98c379; line-height: 26px;">'y'</span>: 1}, {<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>: 1})<br>>>> d = c.parents<br>>>> d<br>ChainMap({<span class="hljs-string" style="color: #98c379; line-height: 26px;">'x'</span>: 1})<br>>>> d is a<br>False<br>>>> d == 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;">>>> a = {<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>>>> b = {<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>>>> c = collections.ChainMap(a,b)<br>>>> c<br>ChainMap({<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}, {<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>>>> c.maps<br>[{<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}, {<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>>>> c.parents<br>ChainMap({<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>>>> c.parents.maps<br>[{<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>>>> c.parents.parents<br>ChainMap({})<br>>>> 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)
来源:oschina
链接:https://my.oschina.net/u/4630617/blog/4892617