{"id":429,"date":"2021-12-20T22:20:17","date_gmt":"2021-12-20T14:20:17","guid":{"rendered":"http:\/\/139.9.1.231\/?p=429"},"modified":"2021-12-20T22:20:18","modified_gmt":"2021-12-20T14:20:18","slug":"collections","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2021\/12\/20\/collections\/","title":{"rendered":"collections\u6a21\u5757"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large is-style-default\"><img src=\"https:\/\/cdn.pixabay.com\/photo\/2021\/11\/23\/13\/32\/forest-6818683_1280.jpg\" alt=\"\"\/><figcaption>https:\/\/pixabay.com\/<\/figcaption><\/figure>\n\n\n\n<p>collections\u6a21\u5757\u5b9e\u73b0\u4e00\u4e9b\u7279\u5b9a\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u66ff\u4ee3Python\u4e2d\u5e38\u7528\u7684\u5185\u7f6e\u6570\u636e\u7c7b\u578b\u5982dict, list, set, tuple\uff0c\u7b80\u5355\u8bf4\u5c31\u662f\u5bf9\u57fa\u672c\u6570\u636e\u7c7b\u578b\u505a\u4e86\u66f4\u4e0a\u4e00\u5c42\u7684\u5904\u7406\u3002<\/p>\n\n\n<form role=\"search\" method=\"get\" action=\"http:\/\/139.9.1.231\/\" class=\"wp-block-search__button-outside wp-block-search__text-button wp-block-search\"><label for=\"wp-block-search__input-1\" class=\"wp-block-search__label\">\u641c\u7d22<\/label><div class=\"wp-block-search__inside-wrapper\"><input type=\"search\" id=\"wp-block-search__input-1\" class=\"wp-block-search__input\" name=\"s\" value=\"\" placeholder=\"\"  required \/><button type=\"submit\" class=\"wp-block-search__button \">\u641c\u7d22<\/button><\/div><\/form>\n\n\n<p><a href=\"https:\/\/github.com\/python\/cpython\/blob\/3.10\/Lib\/collections\/init.py\">https:\/\/github.com\/python\/cpython\/blob\/3.10\/Lib\/collections\/init.py<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/docs.python.org\/zh-cn\/3\/library\/collections.html?highlight=deque#collections.namedtuple\">https:\/\/docs.python.org\/zh-cn\/3\/library\/collections.html?highlight=deque#collections.namedtuple<\/a><\/p>\n\n\n\n<p>init.py:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>'''This module implements specialized container datatypes providing\nalternatives to Python's general purpose built-in containers, dict,\nlist, set, and tuple.\n\n* namedtuple   factory function for creating tuple subclasses with named fields\n* deque        list-like container with fast appends and pops on either end\n* ChainMap     dict-like class for creating a single view of multiple mappings\n* Counter      dict subclass for counting hashable objects\n* OrderedDict  dict subclass that remembers the order entries were added\n* defaultdict  dict subclass that calls a factory function to supply missing values\n* UserDict     wrapper around dictionary objects for easier dict subclassing\n* UserList     wrapper around list objects for easier list subclassing\n* UserString   wrapper around string objects for easier string subclassing\n\n'''\n\n__all__ = &#91;'deque', 'defaultdict', 'namedtuple', 'UserDict', 'UserList',\n            'UserString', 'Counter', 'OrderedDict', 'ChainMap']<\/code><\/pre>\n\n\n\n<h2>\u4e00\u3001deque<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u7528\u9014\uff1a\u53cc\u7aef\u961f\u5217\uff0c\u5934\u90e8\u548c\u5c3e\u90e8\u90fd\u80fd\u4ee5O(1)\u65f6\u95f4\u590d\u6742\u5ea6\u63d2\u5165\u548c\u5220\u9664\u5143\u7d20\u3002\u7c7b\u4f3c\u4e8e\u5217\u8868\u7684\u5bb9\u5668<\/p><\/blockquote>\n\n\n\n<p>\u6240\u8c13<code>\u53cc\u7aef\u961f\u5217<\/code>\uff0c\u5c31\u662f\u4e24\u7aef\u90fd\u80fd\u64cd\u4f5c\uff0c\u4e0ePython\u5185\u7f6e\u7684list\u533a\u522b\u5728\u4e8e\uff1a<strong>\u5934\u90e8\u63d2\u5165\u4e0e\u5220\u9664\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)<\/strong>\uff0c\u6765\u4e2a\u6817\u5b50\u611f\u53d7\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python\n<em># -*- coding:utf-8 -*-<\/em>\n<em># __author__ = 'liao gao xiang'<\/em>\n\n\"\"\"\n\u4fdd\u7559\u6700\u540en\u4e2a\u5143\u7d20\n\"\"\"\nfrom collections import deque\n\n\ndef search(file, pattern, history=5):\n    previous_lines = deque(maxlen=history)\n    for l in file:\n        if pattern in l:\n            yield l, previous_lines  <em># \u4f7f\u7528yield\u8868\u8fbe\u5f0f\u7684\u751f\u6210\u5668\u51fd\u6570\uff0c\u5c06\u641c\u7d22\u8fc7\u7a0b\u7684\u4ee3\u7801\u548c\u641c\u7d22\u7ed3\u679c\u7684\u4ee3\u7801\u89e3\u8026<\/em>\n        previous_lines.append(l)\n\n\nwith open(b'file.txt', mode='r', encoding='utf-8') as f:\n    for line, prevlines in search(f, 'Python', 5):\n        for pline in prevlines:\n            print(pline, end='')\n        print(line, end='')\n\nd = deque()\nd.append(1)\nd.append(\"2\")\nprint(len(d))\nprint(d&#91;0], d&#91;1])\nd.extendleft(&#91;0])\nprint(d)\nd.extend(&#91;6, 7, 8])\nprint(d)\n\nd2 = deque('12345')\nprint(len(d2))\nd2.popleft()\nprint(d2)\nd2.pop()\nprint(d2)\n\n<em># \u5728\u961f\u5217\u4e24\u7aef\u63d2\u5165\u6216\u5220\u9664\u5143\u7d20\u65f6\u95f4\u590d\u6742\u5ea6\u90fd\u662f O(1) \uff0c\u533a\u522b\u4e8e\u5217\u8868\uff0c\u5728\u5217\u8868\u7684\u5f00\u5934\u63d2\u5165\u6216\u5220\u9664\u5143\u7d20\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(N)<\/em>\nd3 = deque(maxlen=2)\nd3.append(1)\nd3.append(2)\nprint(d3)\nd3.append(3)\nprint(d3)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4eba\u751f\u82e6\u77ed\n\u6211\u7528Python\n2\n1 2\ndeque(&#91;0, 1, '2'])\ndeque(&#91;0, 1, '2', 6, 7, 8])\n5\ndeque(&#91;'2', '3', '4', '5'])\ndeque(&#91;'2', '3', '4'])\ndeque(&#91;1, 2], maxlen=2)\ndeque(&#91;2, 3], maxlen=2)<\/code><\/pre>\n\n\n\n<p>\u56e0\u6b64\uff0c\u5982\u679c\u4f60\u9047\u5230\u7ecf\u5e38\u64cd\u4f5c\u5217\u8868\u5934\u7684\u573a\u666f\uff0c\u4f7f\u7528deque\u6700\u597d\u3002deque\u7c7b\u7684\u6240\u6709\u65b9\u6cd5\uff0c\u81ea\u884c\u64cd\u4f5c\u4e00\u904d\u5c31\u77e5\u9053\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class deque(object):\n    \"\"\"\n    deque(&#91;iterable&#91;, maxlen]]) --&gt; deque object\n\n    A list-like sequence optimized for data accesses near its endpoints.\n    \"\"\"\n    def append(self, *args, **kwargs): <em># real signature unknown<\/em>\n        \"\"\" Add an element to the right side of the deque. \"\"\"\n        pass\n\n    def appendleft(self, *args, **kwargs): <em># real signature unknown<\/em>\n        \"\"\" Add an element to the left side of the deque. \"\"\"\n        pass\n\n    def clear(self, *args, **kwargs): <em># real signature unknown<\/em>\n        \"\"\" Remove all elements from the deque. \"\"\"\n        pass\n\n    def copy(self, *args, **kwargs): <em># real signature unknown<\/em>\n        \"\"\" Return a shallow copy of a deque. \"\"\"\n        pass\n\n    def count(self, value): <em># real signature unknown; restored from __doc__<\/em>\n        \"\"\" D.count(value) -&gt; integer -- return number of occurrences of value \"\"\"\n        return 0\n\n    def extend(self, *args, **kwargs): <em># real signature unknown<\/em>\n        \"\"\" Extend the right side of the deque with elements from the iterable \"\"\"\n        pass\n\n    def extendleft(self, *args, **kwargs): <em># real signature unknown<\/em>\n        \"\"\" Extend the left side of the deque with elements from the iterable \"\"\"\n        pass\n\n    def index(self, value, start=None, stop=None): <em># real signature unknown; restored from __doc__<\/em>\n        \"\"\"\n        D.index(value, &#91;start, &#91;stop]]) -&gt; integer -- return first index of value.\n        Raises ValueError if the value is not present.\n        \"\"\"\n        return 0\n\n    def insert(self, index, p_object): <em># real signature unknown; restored from __doc__<\/em>\n        \"\"\" D.insert(index, object) -- insert object before index \"\"\"\n        pass\n\n    def pop(self, *args, **kwargs): <em># real signature unknown<\/em>\n        \"\"\" Remove and return the rightmost element. \"\"\"\n        pass\n\n    def popleft(self, *args, **kwargs): <em># real signature unknown<\/em>\n        \"\"\" Remove and return the leftmost element. \"\"\"\n        pass\n\n    def remove(self, value): <em># real signature unknown; restored from __doc__<\/em>\n        \"\"\" D.remove(value) -- remove first occurrence of value. \"\"\"\n        pass\n\n    def reverse(self): <em># real signature unknown; restored from __doc__<\/em>\n        \"\"\" D.reverse() -- reverse *IN PLACE* \"\"\"\n        pass\n\n    def rotate(self, *args, **kwargs): <em># real signature unknown<\/em>\n        \"\"\" Rotate the deque n steps to the right (default n=1).  If n is negative, rotates left. \"\"\"\n        pass<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u63d0\u793a\u4e00\u4e0b\uff0c\u6709\u4e9b\u51fd\u6570\u5bf9\u961f\u5217\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f46\u8fd4\u56de\u503c\u662fNone\uff0c\u6bd4\u5982<code>reverse()<\/code>\u53cd\u8f6c\u961f\u5217\uff0c<code>rotate(1)<\/code>\u5c06\u961f\u5217\u4e2d\u5143\u7d20\u5411\u53f3\u79fb1\u4f4d\uff0c\u5c3e\u90e8\u7684\u5143\u7d20\u79fb\u5230\u5934\u90e8\u3002<\/p>\n\n\n\n<h2>\u4e8c\u3001defaultdict<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u7528\u9014\uff1a\u5e26\u6709\u9ed8\u8ba4\u503c\u7684\u5b57\u5178\u3002\u7236\u7c7b\u4e3aPython\u5185\u7f6e\u7684dict<\/p><\/blockquote>\n\n\n\n<p>\u5b57\u5178\u5e26\u9ed8\u8ba4\u503c\u6709\u5565\u597d\u5904\uff1f\u4e3e\u4e2a\u6817\u5b50\uff0c\u4e00\u822c\u6765\u8bb2\uff0c\u521b\u5efa\u4e00\u4e2a\u591a\u503c\u6620\u5c04\u5b57\u5178\u662f\u5f88\u7b80\u5355\u7684\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u9009\u62e9\u81ea\u5df1\u5b9e\u73b0\u7684\u8bdd\uff0c \u90a3\u4e48\u5bf9\u4e8e\u503c\u7684\u521d\u59cb\u5316\u53ef\u80fd\u4f1a\u6709\u70b9\u9ebb\u70e6\uff0c\u4f60\u53ef\u80fd\u4f1a\u50cf\u4e0b\u9762\u8fd9\u6837\u6765\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>d = {}\nfor key, value in pairs:\n    if key not in d:\n        d&#91;key] = &#91;]\n    d&#91;key].append(value)<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u4f7f\u7528 defaultdict \u7684\u8bdd\u4ee3\u7801\u5c31\u66f4\u52a0\u7b80\u6d01\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>d = defaultdict(list)\nfor key, value in pairs:\n    d&#91;key].append(value)<\/code><\/pre>\n\n\n\n<p>defaultdict \u7684\u4e00\u4e2a\u7279\u5f81\u662f\u5b83\u4f1a\u81ea\u52a8\u521d\u59cb\u5316\u6bcf\u4e2a key \u521a\u5f00\u59cb\u5bf9\u5e94\u7684\u503c\uff0c\u6240\u4ee5\u4f60\u53ea\u9700\u8981 \u5173\u6ce8\u6dfb\u52a0\u5143\u7d20\u64cd\u4f5c\u4e86\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python\n<em># -*- coding:utf-8 -*-<\/em>\n<em># __author__ = 'liao gao xiang'<\/em>\n\n<em># \u5b57\u5178\u4e2d\u7684\u952e\u6620\u5c04\u591a\u4e2a\u503c<\/em>\nfrom collections import defaultdict\n\nd = defaultdict(list)\nprint(d)\nd&#91;'a'].append(&#91;1, 2, 3])\nd&#91;'b'].append(2)\nd&#91;'c'].append(3)\n\nprint(d)\n\nd = defaultdict(set)\nprint(d)\nd&#91;'a'].add(1)\nd&#91;'a'].add(2)\nd&#91;'b'].add(4)\n\nprint(d)<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>defaultdict(&lt;class 'list'&gt;, {})\ndefaultdict(&lt;class 'list'&gt;, {'a': &#91;&#91;1, 2, 3]], 'b': &#91;2], 'c': &#91;3]})\ndefaultdict(&lt;class 'set'&gt;, {})\ndefaultdict(&lt;class 'set'&gt;, {'a': {1, 2}, 'b': {4}})<\/code><\/pre>\n\n\n\n<h2>\u4e09\u3001namedtuple()<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u7528\u9014\uff1a\u521b\u5efa\u547d\u540d\u5b57\u6bb5\u7684\u5143\u7ec4\u3002\u5de5\u5382\u51fd\u6570<\/p><\/blockquote>\n\n\n\n<p>namedtuple\u4e3b\u8981\u7528\u6765\u4ea7\u751f\u53ef\u4ee5\u4f7f\u7528\u540d\u79f0\u6765\u8bbf\u95ee\u5143\u7d20\u7684\u6570\u636e\u5bf9\u8c61\uff0c\u901a\u5e38\u7528\u6765\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c \u5728\u8bbf\u95ee\u4e00\u4e9btuple\u7c7b\u578b\u7684\u6570\u636e\u65f6\u5c24\u5176\u597d\u7528\u3002<\/p>\n\n\n\n<p>\u6bd4\u5982\u6211\u4eec\u7528\u6237\u62e5\u6709\u4e00\u4e2a\u8fd9\u6837\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6bcf\u4e00\u4e2a\u5bf9\u8c61\u662f\u62e5\u6709\u4e09\u4e2a\u5143\u7d20\u7684tuple\u3002\u4f7f\u7528namedtuple\u65b9\u6cd5\u5c31\u53ef\u4ee5\u65b9\u4fbf\u7684\u901a\u8fc7tuple\u6765\u751f\u6210\u53ef\u8bfb\u6027\u66f4\u9ad8\u4e5f\u66f4\u597d\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from collections import namedtuple\n\nwebsites = &#91;\n    ('Sohu', 'http:\/\/www.sohu.com\/', u'\u5f20\u671d\u9633'),\n    ('Sina', 'http:\/\/www.sina.com.cn\/', u'\u738b\u5fd7\u4e1c'),\n    ('163', 'http:\/\/www.163.com\/', u'\u4e01\u78ca')\n]\n\nWebsite = namedtuple('Website', &#91;'name', 'url', 'founder'])\n\nfor website in websites:\n    website = Website._make(website)\n    print website\n\n\n<em># \u8f93\u51fa\u7ed3\u679c:<\/em>\nWebsite(name='Sohu', url='http:\/\/www.sohu.com\/', founder=u'\\u5f20\\u671d\\u9633')\nWebsite(name='Sina', url='http:\/\/www.sina.com.cn\/', founder=u'\\u738b\\u5fd7\\u4e1c')\nWebsite(name='163', url='http:\/\/www.163.com\/', founder=u'\\u4e01\\u78ca')<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f\uff0cnamedtuple\u662f\u51fd\u6570\uff0c\u4e0d\u662f\u7c7b\u3002<\/p>\n\n\n\n<h2>\u56db\u3001Counter<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u7528\u9014\uff1a\u7edf\u8ba1\u53ef\u54c8\u5e0c\u7684\u5bf9\u8c61\u3002\u7236\u7c7b\u4e3aPython\u5185\u7f6e\u7684dict<\/p><\/blockquote>\n\n\n\n<p>\u5bfb\u627e\u5e8f\u5217\u4e2d\u51fa\u73b0\u6b21\u6570\u6700\u591a\u7684\u5143\u7d20\u3002\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5355\u8bcd\u5217\u8868\u5e76\u4e14\u60f3\u627e\u51fa\u54ea\u4e2a\u5355\u8bcd\u51fa\u73b0\u9891\u7387\u6700\u9ad8\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python\n<em># -*- coding:utf-8 -*-<\/em>\n<em># __author__ = 'liao gao xiang'<\/em>\n\nfrom collections import Counter\n\nwords = &#91;\n    'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',\n    'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',\n    'eyes', \"don't\", 'look', 'around', 'the', 'eyes', 'look', 'into',\n    'my', 'eyes', \"you're\", 'under'\n]\n\nword_counts = Counter(words)\n\n<em># \u51fa\u73b0\u9891\u7387\u6700\u9ad8\u7684\u4e09\u4e2a\u5355\u8bcd<\/em>\ntop_three = word_counts.most_common(3)\nprint(top_three)\n<em># Outputs &#91;('eyes', 8), ('the', 5), ('look', 4)]<\/em>\nprint(word_counts&#91;'eyes'])\n\nmorewords = &#91;'why', 'are', 'you', 'not', 'looking', 'in', 'my', 'eyes']\n\n<em># \u5982\u679c\u4f60\u60f3\u624b\u52a8\u589e\u52a0\u8ba1\u6570\uff0c\u53ef\u4ee5\u7b80\u5355\u7684\u7528\u52a0\u6cd5\uff1a<\/em>\nfor word in morewords:\n    print(word)\n    word_counts&#91;word] += 1\nprint(word_counts&#91;'eyes'])<\/code><\/pre>\n\n\n\n<p>\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;('eyes', 8), ('the', 5), ('look', 4)]\n8\nwhy\nare\nyou\nnot\nlooking\nin\nmy\neyes\n9<\/code><\/pre>\n\n\n\n<p>\u56e0\u4e3aCounter\u7ee7\u627f\u81eadict\uff0c\u6240\u6709dict\u6709\u7684\u65b9\u6cd5\u5b83\u90fd\u6709\uff08defaultdict\u548cOrderedDict\u4e5f\u662f\u7684\uff09\uff0cCounter\u81ea\u5df1\u5b9e\u73b0\u6216\u91cd\u5199\u4e866\u4e2a\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul><li><code>most_common(self, n=None)<\/code>,<\/li><li><code>elements(self)<\/code><\/li><li><code>fromkeys(cls, iterable, v=None)<\/code><\/li><li><code>update(*args, **kwds)<\/code><\/li><li><code>subtract(*args, **kwds)<\/code><\/li><li><code>copy(self)<\/code><\/li><\/ul>\n\n\n\n<h2>\u4e94\u3001OrderedDict<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u7528\u9014\uff1a\u6392\u5e8f\u7684\u5b57\u6bb5\u3002\u7236\u7c7b\u4e3aPython\u5185\u7f6e\u7684dict<\/p><\/blockquote>\n\n\n\n<p>OrderedDict\u5728\u8fed\u4ee3\u64cd\u4f5c\u7684\u65f6\u5019\u4f1a\u4fdd\u6301\u5143\u7d20\u88ab\u63d2\u5165\u65f6\u7684\u987a\u5e8f\uff0cOrderedDict\u5185\u90e8\u7ef4\u62a4\u7740\u4e00\u4e2a\u6839\u636e\u952e\u63d2\u5165\u987a\u5e8f\u6392\u5e8f\u7684<strong>\u53cc\u5411\u94fe\u8868<\/strong>\u3002\u6bcf\u6b21\u5f53\u4e00\u4e2a\u65b0\u7684\u5143\u7d20\u63d2\u5165\u8fdb\u6765\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u88ab\u653e\u5230\u94fe\u8868\u7684\u5c3e\u90e8\u3002\u5bf9\u4e8e\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u952e\u7684\u91cd\u590d\u8d4b\u503c\u4e0d\u4f1a\u6539\u53d8\u952e\u7684\u987a\u5e8f\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e00\u4e2aOrderedDict\u7684\u5927\u5c0f\u662f\u4e00\u4e2a\u666e\u901a\u5b57\u5178\u7684\u4e24\u500d\uff0c\u56e0\u4e3a\u5b83\u5185\u90e8\u7ef4\u62a4\u7740\u53e6\u5916\u4e00\u4e2a\u94fe\u8868\u3002 \u6240\u4ee5\u5982\u679c\u4f60\u8981\u6784\u5efa\u4e00\u4e2a\u9700\u8981\u5927\u91cfOrderedDict \u5b9e\u4f8b\u7684\u6570\u636e\u7ed3\u6784\u7684\u65f6\u5019(\u6bd4\u5982\u8bfb\u53d6100,000\u884cCSV\u6570\u636e\u5230\u4e00\u4e2a OrderedDict \u5217\u8868\u4e2d\u53bb)\uff0c\u90a3\u4e48\u4f60\u5c31\u5f97\u4ed4\u7ec6\u6743\u8861\u4e00\u4e0b\u662f\u5426\u4f7f\u7528 OrderedDict\u5e26\u6765\u7684\u597d\u5904\u8981\u5927\u8fc7\u989d\u5916\u5185\u5b58\u6d88\u8017\u7684\u5f71\u54cd\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python\n<em># -*- coding:utf-8 -*-<\/em>\n<em># __author__ = 'liao gao xiang'<\/em>\n\nfrom collections import OrderedDict\n\nd = OrderedDict()\nd&#91;'foo'] = 1\nd&#91;'bar'] = 2\nd&#91;'spam'] = 3\nd&#91;'grok'] = 4\n<em># d&#91;'bar'] = 22 #\u5bf9\u4e8e\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u952e\uff0c\u91cd\u590d\u8d4b\u503c\u4e0d\u4f1a\u6539\u53d8\u952e\u7684\u987a\u5e8f<\/em>\nfor key in d:\n    print(key, d&#91;key])\n\nprint(d)\n\nimport json\n\nprint(json.dumps(d))<\/code><\/pre>\n\n\n\n<p>\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>foo 1\nbar 2\nspam 3\ngrok 4\nOrderedDict(&#91;('foo', 1), ('bar', 2), ('spam', 3), ('grok', 4)])\n{\"foo\": 1, \"bar\": 2, \"spam\": 3, \"grok\": 4}<\/code><\/pre>\n\n\n\n<p>OrderDict\u5b9e\u73b0\u6216\u91cd\u5199\u4e86\u5982\u4e0b\u65b9\u6cd5\u3002\u90fd\u662f\u5e72\u561b\u7684\uff1f\u8fd9\u4e2a\u7559\u7ed9\u5927\u5bb6\u5f53\u8bfe\u540e\u4f5c\u4e1a\u4e86^_^<\/p>\n\n\n\n<ul><li><code>clear(self)<\/code><\/li><li><code>popitem(self, last=True)<\/code><\/li><li><code>move_to_end(self, key, last=True)<\/code><\/li><li><code>keys(self)<\/code><\/li><li><code>items(self)<\/code><\/li><li><code>values(self)<\/code><\/li><li><code>pop(self, key, default=__marker)<\/code><\/li><li><code>setdefault(self, key, default=None)<\/code><\/li><li><code>copy(self)<\/code><\/li><li><code>fromkeys(cls, iterable, value=None)<\/code><\/li><\/ul>\n\n\n\n<h2>\u516d\u3001ChainMap<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u7528\u9014\uff1a\u521b\u5efa\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u96c6\u5408\u3002\u7c7b\u5b57\u5178\u7c7b\u578b<\/p><\/blockquote>\n\n\n\n<p>\u5f88\u7b80\u5355\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python\n<em># -*- coding:utf-8 -*-<\/em>\n<em># __author__ = 'liao gao xiang'<\/em>\n\nfrom collections import ChainMap\nfrom itertools import chain\n\n<em># \u4e0d\u540c\u96c6\u5408\u4e0a\u5143\u7d20\u7684\u8fed\u4ee3<\/em>\na = &#91;1, 2, 3, 4]\nb = ('x', 'y', 'z')\nc = {1, 'a'}\n\n<em># \u65b9\u6cd5\u4e00\uff0c\u4f7f\u7528chain<\/em>\nfor i in chain(a, b, c):\n    print(i)\nprint('--------------')\n<em># \u65b9\u6cd5\u4e8c\uff0c\u4f7f\u7528chainmap<\/em>\nfor j in ChainMap(a, b, c):\n    print(j)\n\n<em># \u8fd9\u4e24\u79cd\u5747\u4e3a\u8282\u7701\u5185\u5b58\uff0c\u6548\u7387\u66f4\u9ad8\u7684\u8fed\u4ee3\u65b9\u5f0f<\/em><\/code><\/pre>\n\n\n\n<p>\u4e00\u4e2a ChainMap \u63a5\u53d7\u591a\u4e2a\u5b57\u5178\u5e76\u5c06\u5b83\u4eec\u5728\u903b\u8f91\u4e0a\u53d8\u4e3a\u4e00\u4e2a\u5b57\u5178\u3002\u7136\u540e\uff0c\u8fd9\u4e9b\u5b57\u5178\u5e76\u4e0d\u662f\u771f\u7684\u5408\u5e76\u5728\u4e00\u8d77\u4e86\uff0cChainMap \u7c7b\u53ea\u662f\u5728\u5185\u90e8\u521b\u5efa\u4e86\u4e00\u4e2a\u5bb9\u7eb3\u8fd9\u4e9b\u5b57\u5178\u7684\u5217\u8868\u5e76\u91cd\u65b0\u5b9a\u4e49\u4e86\u4e00\u4e9b\u5e38\u89c1\u7684\u5b57\u5178\u64cd\u4f5c\u6765\u904d\u5386\u8fd9\u4e2a\u5217\u8868\u3002\u5927\u90e8\u5206\u5b57\u5178\u64cd\u4f5c\u90fd\u662f\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528\u7684\uff0c\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python\n<em># -*- coding:utf-8 -*-<\/em>\n<em># __author__ = 'liao gao xiang'<\/em>\n\n<em># \u5408\u5e76\u591a\u4e2a\u5b57\u5178\u548c\u6620\u5c04<\/em>\na = {'x': 1, 'z': 3}\nb = {'y': 2, 'z': 4}\n<em># \u73b0\u5728\u5047\u8bbe\u4f60\u5fc5\u987b\u5728\u4e24\u4e2a\u5b57\u5178\u4e2d\u6267\u884c\u67e5\u627e\u64cd\u4f5c<\/em>\n<em># (\u6bd4\u5982\u5148\u4ece a \u4e2d\u627e\uff0c\u5982\u679c\u627e\u4e0d\u5230\u518d\u5728 b \u4e2d\u627e)\u3002<\/em>\n<em># \u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u89e3\u51b3\u65b9\u6848\u5c31\u662f\u4f7f\u7528collections\u6a21\u5757\u4e2d\u7684ChainMap\u7c7b<\/em>\nfrom collections import ChainMap\n\nc = ChainMap(a, b)\n\nprint(c)\na&#91;'x'] = 11  <em># \u4f7f\u7528ChainMap\u65f6\uff0c\u539f\u5b57\u5178\u505a\u4e86\u66f4\u65b0\uff0c\u8fd9\u79cd\u66f4\u65b0\u4f1a\u5408\u5e76\u5230\u65b0\u7684\u5b57\u5178\u4e2d\u53bb<\/em>\n\nprint(c)  <em># \u6309\u987a\u5e8f\u5408\u5e76\u4e24\u4e2a\u5b57\u5178<\/em>\nprint(c&#91;'x'])\nprint(c&#91;'y'])\nprint(c&#91;'z'])\n\n<em># \u5bf9\u4e8e\u5b57\u5178\u7684\u66f4\u65b0\u6216\u5220\u9664\u64cd\u4f5c\u5f71\u54cd\u7684\u603b\u662f\u5217\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b57\u5178\u3002<\/em>\nc&#91;'z'] = 10\nc&#91;'w'] = 40\ndel c&#91;'x']\nprint(a)\n<em># del c&#91;'y']\u5c06\u51fa\u73b0\u62a5\u9519<\/em>\n\n<em># ChainMap\u5bf9\u4e8e\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u4f5c\u7528\u8303\u56f4\u53d8\u91cf\uff08\u6bd4\u5982globals,locals\u7b49\uff09<\/em>\n<em># \u662f\u975e\u5e38\u6709\u7528\u7684\u3002\u4e8b\u5b9e\u4e0a\uff0c\u6709\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u5b83\u53d8\u5f97\u7b80\u5355\uff1a<\/em>\nvalues = ChainMap()  <em># \u9ed8\u8ba4\u4f1a\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/em>\nprint('\\t', values)\nvalues&#91;'x'] = 1\nvalues = values.new_child()  <em># \u6dfb\u52a0\u4e00\u4e2a\u7a7a\u5b57\u5178<\/em>\nvalues&#91;'x'] = 2\nvalues = values.new_child()\nvalues&#91;'x'] = 30\n<em># values = values.new_child()<\/em>\nprint(values, values&#91;'x'])  <em># values&#91;'x']\u8f93\u51fa\u6700\u540e\u4e00\u6b21\u6dfb\u52a0\u7684\u503c<\/em>\nvalues = values.parents  <em># \u5220\u9664\u4e0a\u4e00\u6b21\u6dfb\u52a0\u7684\u5b57\u5178<\/em>\nprint(values&#91;'x'])\nvalues = values.parents\nprint(values)\n\na = {'x': 1, 'y': 2}\nb = {'y': 2, 'z': 3}\nmerge = dict(b)\nmerge.update(a)\nprint(merge&#91;'x'], merge&#91;'y'], merge&#91;'z'])\na&#91;'x'] = 11\nprint(merge&#91;'x'])<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ChainMap({'x': 1, 'z': 3}, {'y': 2, 'z': 4})\nChainMap({'x': 11, 'z': 3}, {'y': 2, 'z': 4})\n11\n2\n3\n{'z': 10, 'w': 40}\n     ChainMap({})\nChainMap({'x': 30}, {'x': 2}, {'x': 1}) 30\n2\nChainMap({'x': 1})\n1 2 3\n1<\/code><\/pre>\n\n\n\n<p>\u4f5c\u4e3aChainMap\u7684\u66ff\u4ee3\uff0c\u4f60\u53ef\u80fd\u4f1a\u8003\u8651\u4f7f\u7528 update() \u65b9\u6cd5\u5c06\u4e24\u4e2a\u5b57\u5178\u5408\u5e76\u3002\u8fd9\u6837\u4e5f\u80fd\u884c\u5f97\u901a\uff0c\u4f46\u662f\u5b83\u9700\u8981\u4f60\u521b\u5efa\u4e00\u4e2a\u5b8c\u5168\u4e0d\u540c\u7684\u5b57\u5178\u5bf9\u8c61(\u6216\u8005\u662f\u7834\u574f\u73b0\u6709\u5b57\u5178\u7ed3\u6784)\u3002\u540c\u65f6\uff0c\u5982\u679c\u539f\u5b57\u5178\u505a\u4e86\u66f4\u65b0\uff0c\u8fd9\u79cd\u6539\u53d8\u4e0d\u4f1a\u53cd\u5e94\u5230\u65b0\u7684\u5408\u5e76\u5b57\u5178\u4e2d\u53bb\u3002<\/p>\n\n\n\n<p>ChainMap\u5b9e\u73b0\u6216\u91cd\u5199\u4e86\u5982\u4e0b\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul><li><code>get(self, key, default=None)<\/code><\/li><li><code>fromkeys(cls, iterable, *args)<\/code><\/li><li><code>copy(self)<\/code><\/li><li><code>new_child(self, m=None)<\/code><\/li><li><code>parents(self)<\/code><\/li><li><code>popitem(self)<\/code><\/li><li><code>pop(self, key, *args)<\/code><\/li><li><code>clear(self)<\/code><\/li><\/ul>\n\n\n\n<h2>\u4e03\u3001UserDict\u3001UserList\u3001UserString<\/h2>\n\n\n\n<p>\u8fd9\u4e09\u4e2a\u7c7b\u662f\u5206\u522b\u5bf9 dict\u3001list\u3001str \u4e09\u79cd\u6570\u636e\u7c7b\u578b\u7684\u5305\u88c5\uff0c\u5176\u4e3b\u8981\u662f\u4e3a\u65b9\u4fbf\u7528\u6237\u5b9e\u73b0\u81ea\u5df1\u7684\u6570\u636e\u7c7b\u578b\u3002\u5728 Python2 \u4e4b\u524d\uff0c\u8fd9\u4e09\u4e2a\u7c7b\u5206\u522b\u4f4d\u4e8e UserDict\u3001UserList\u3001UserString \u4e09\u4e2a\u6a21\u5757\u4e2d\uff0c\u9700\u8981\u7528\u7c7b\u4f3c\u4e8e&nbsp;<code>from UserDict import UserDict<\/code>&nbsp;\u7684\u65b9\u5f0f\u5bfc\u5165\u3002\u5728 Python3 \u4e4b\u540e\u5219\u88ab\u632a\u5230\u4e86 collections \u6a21\u5757\u4e2d\u3002\u8fd9\u4e09\u4e2a\u7c7b\u90fd\u662f\u57fa\u7c7b\uff0c\u5982\u679c\u7528\u6237\u8981\u6269\u5c55\u8fd9\u4e09\u79cd\u7c7b\u578b\uff0c\u53ea\u9700\u7ee7\u627f\u8fd9\u4e09\u4e2a\u7c7b\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>collections\u6a21\u5757\u5b9e\u73b0\u4e00\u4e9b\u7279\u5b9a\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u66ff\u4ee3Python\u4e2d\u5e38\u7528\u7684\u5185\u7f6e\u6570\u636e\u7c7b\u578b\u5982dict, lis &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2021\/12\/20\/collections\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">collections\u6a21\u5757<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/429"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=429"}],"version-history":[{"count":6,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/429\/revisions"}],"predecessor-version":[{"id":435,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/429\/revisions\/435"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=429"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}