{"id":1026809,"date":"2024-12-31T10:45:50","date_gmt":"2024-12-31T02:45:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1026809.html"},"modified":"2024-12-31T10:45:52","modified_gmt":"2024-12-31T02:45:52","slug":"python%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e4%b8%80%e4%b8%aa%e7%a9%ba%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1026809.html","title":{"rendered":"python\u5982\u4f55\u751f\u6210\u4e00\u4e2a\u7a7a\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/785eb9d7-e4c8-472c-ad9d-fcc3be28243b.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u751f\u6210\u4e00\u4e2a\u7a7a\u5b57\u5178\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u751f\u6210\u4e00\u4e2a\u7a7a\u5b57\u5178\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u548c <code>dict()<\/code> \u51fd\u6570\u3002<\/strong>\u8fd9\u4e24\u79cd\u65b9\u5f0f\u90fd\u662f\u521b\u5efa\u7a7a\u5b57\u5178\u7684\u6709\u6548\u65b9\u6cd5\uff0c\u5176\u4e2d\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u662f\u8f83\u4e3a\u5e38\u89c1\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u800c <code>dict()<\/code> \u51fd\u6570\u5219\u662f\u5229\u7528 Python \u5185\u7f6e\u7684\u6784\u9020\u51fd\u6570\u6765\u751f\u6210\u5b57\u5178\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e24\u79cd\u65b9\u6cd5\u53ca\u5176\u4f7f\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code><\/h3>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u662f\u6700\u7b80\u5355\u548c\u76f4\u63a5\u7684\u521b\u5efa\u7a7a\u5b57\u5178\u7684\u65b9\u6cd5\u3002\u53ea\u9700\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e00\u5bf9\u82b1\u62ec\u53f7\u5373\u53ef\u751f\u6210\u4e00\u4e2a\u7a7a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">empty_dict = {}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u521b\u5efa\u7a7a\u5b57\u5178\u7684\u65b9\u5f0f\u975e\u5e38\u7b80\u6d01\u660e\u4e86\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u3002\u82b1\u62ec\u53f7 <code>{}<\/code> \u662f\u5b57\u5178\u5728 Python \u4e2d\u7684\u5b57\u9762\u91cf\u8868\u793a\u5f62\u5f0f\uff0c\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u4ee3\u7801\u91cf\u5c11\uff0c\u800c\u4e14\u6613\u8bfb\u6613\u61c2\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u53e6\u5916\u4e00\u79cd\u521b\u5efa\u7a7a\u5b57\u5178\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 Python \u5185\u7f6e\u7684 <code>dict()<\/code> \u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e5f\u5f88\u5e38\u7528\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u66f4\u52a0\u660e\u786e\u5730\u8868\u660e\u6211\u4eec\u5728\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u5bf9\u8c61\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">empty_dict = dict()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> <code>dict()<\/code> \u51fd\u6570\u662f Python \u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u521b\u5efa\u5b57\u5178\u5bf9\u8c61\u3002\u4f7f\u7528 <code>dict()<\/code> \u521b\u5efa\u7a7a\u5b57\u5178\u4e0d\u4ec5\u6e05\u6670\u5730\u8868\u660e\u4e86\u6211\u4eec\u7684\u610f\u56fe\uff0c\u8fd8\u53ef\u4ee5\u4e0e\u5176\u4ed6\u7c7b\u578b\u7684\u5bf9\u8c61\u6784\u9020\u51fd\u6570\u4fdd\u6301\u4e00\u81f4\u6027\uff0c\u4f8b\u5982 <code>list()<\/code>, <code>set()<\/code>, <code>tuple()<\/code> \u7b49\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5176\u4ed6\u521b\u5efa\u5b57\u5178\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u4e24\u79cd\u521b\u5efa\u7a7a\u5b57\u5178\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u65b9\u5f0f\u521b\u5efa\u975e\u7a7a\u5b57\u5178\u6216\u4ece\u5df2\u6709\u6570\u636e\u7ed3\u6784\u4e2d\u521b\u5efa\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4ece\u952e\u503c\u5bf9\u521b\u5efa\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u5178\u7684\u82b1\u62ec\u53f7\u4e2d\u6dfb\u52a0\u952e\u503c\u5bf9\u6765\u521b\u5efa\u4e00\u4e2a\u975e\u7a7a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">non_empty_dict = {&#39;key1&#39;: &#39;value1&#39;, &#39;key2&#39;: &#39;value2&#39;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\u5e76\u4f20\u5165\u952e\u503c\u5bf9<\/h4>\n<\/p>\n<p><p><code>dict()<\/code> \u51fd\u6570\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4f20\u5165\u952e\u503c\u5bf9\u6765\u521b\u5efa\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">non_empty_dict = dict(key1=&#39;value1&#39;, key2=&#39;value2&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4ece\u5217\u8868\u6216\u5143\u7ec4\u521b\u5efa\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\u901a\u8fc7\u4f20\u5165\u5305\u542b\u952e\u503c\u5bf9\u7684\u5217\u8868\u6216\u5143\u7ec4\u6765\u521b\u5efa\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">p<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>rs = [(&#39;key1&#39;, &#39;value1&#39;), (&#39;key2&#39;, &#39;value2&#39;)]<\/p>\n<p>dict_from_pairs = dict(pairs)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u5728 Python \u4e2d\u521b\u5efa\u5b57\u5178\u7684\u7b80\u6d01\u65b9\u6cd5\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u751f\u6210\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squared_numbers = {x: x2 for x in range(5)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b57\u5178\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u5b57\u5178\u540e\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3\u5b57\u5178\u7684\u57fa\u672c\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5305\u62ec\u6dfb\u52a0\u3001\u5220\u9664\u3001\u4fee\u6539\u548c\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6dfb\u52a0\u548c\u4fee\u6539\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u952e\u6765\u6dfb\u52a0\u6216\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">empty_dict[&#39;new_key&#39;] = &#39;new_value&#39;  # \u6dfb\u52a0\u65b0\u5143\u7d20<\/p>\n<p>empty_dict[&#39;new_key&#39;] = &#39;updated_value&#39;  # \u4fee\u6539\u5df2\u6709\u5143\u7d20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5220\u9664\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>del<\/code> \u8bed\u53e5\u6216 <code>pop()<\/code> \u65b9\u6cd5\u5220\u9664\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">del empty_dict[&#39;new_key&#39;]  # \u4f7f\u7528 del \u8bed\u53e5<\/p>\n<p>value = empty_dict.pop(&#39;new_key&#39;, None)  # \u4f7f\u7528 pop() \u65b9\u6cd5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8bbf\u95ee\u5143\u7d20<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u952e\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u5143\u7d20\uff0c\u4f7f\u7528 <code>get()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">value = empty_dict[&#39;new_key&#39;]  # \u76f4\u63a5\u8bbf\u95ee<\/p>\n<p>value = empty_dict.get(&#39;new_key&#39;, &#39;default_value&#39;)  # \u4f7f\u7528 get() \u65b9\u6cd5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b57\u5178\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u64cd\u4f5c\u5916\uff0c\u5b57\u5178\u8fd8\u652f\u6301\u4e00\u4e9b\u9ad8\u7ea7\u64cd\u4f5c\u548c\u65b9\u6cd5\uff0c\u5305\u62ec\u5408\u5e76\u5b57\u5178\u3001\u8fed\u4ee3\u5b57\u5178\u3001\u5b57\u5178\u89c6\u56fe\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5408\u5e76\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>update()<\/code> \u65b9\u6cd5\u6216\u89e3\u5305\u64cd\u4f5c\u7b26 <code><\/code> \u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">dict1 = {&#39;key1&#39;: &#39;value1&#39;}<\/p>\n<p>dict2 = {&#39;key2&#39;: &#39;value2&#39;}<\/p>\n<p>dict1.update(dict2)  # \u4f7f\u7528 update() \u65b9\u6cd5<\/p>\n<p>merged_dict = {&lt;strong&gt;dict1, &lt;\/strong&gt;dict2}  # \u4f7f\u7528\u89e3\u5305\u64cd\u4f5c\u7b26<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8fed\u4ee3\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7 <code>items()<\/code>, <code>keys()<\/code>, <code>values()<\/code> \u65b9\u6cd5\u8fed\u4ee3\u5b57\u5178\u7684\u952e\u503c\u5bf9\u3001\u952e\u6216\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for key, value in empty_dict.items():<\/p>\n<p>    print(key, value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5b57\u5178\u89c6\u56fe<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u89c6\u56fe\u63d0\u4f9b\u4e86\u4e00\u79cd\u52a8\u6001\u67e5\u770b\u5b57\u5178\u5143\u7d20\u7684\u65b9\u6cd5\uff0c\u4f7f\u7528 <code>items()<\/code>, <code>keys()<\/code>, <code>values()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u5b57\u5178\u7684\u89c6\u56fe\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys_view = empty_dict.keys()<\/p>\n<p>values_view = empty_dict.values()<\/p>\n<p>items_view = empty_dict.items()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b57\u5178\u7684\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\uff0c\u5982\u5b58\u50a8\u914d\u7f6e\u4fe1\u606f\u3001\u5b9e\u73b0\u6570\u636e\u6620\u5c04\u3001\u7edf\u8ba1\u9891\u7387\u7b49\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b58\u50a8\u914d\u7f6e\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u5e38\u7528\u4e8e\u5b58\u50a8\u7a0b\u5e8f\u7684\u914d\u7f6e\u4fe1\u606f\uff0c\u4ee5\u4fbf\u7075\u6d3b\u8c03\u6574\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">config = {<\/p>\n<p>    &#39;host&#39;: &#39;localhost&#39;,<\/p>\n<p>    &#39;port&#39;: 8080,<\/p>\n<p>    &#39;debug&#39;: True<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5b9e\u73b0\u6570\u636e\u6620\u5c04<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u6570\u636e\u7684\u6620\u5c04\u548c\u67e5\u627e\uff0c\u5982\u5c06\u7528\u6237\u540d\u6620\u5c04\u5230\u7528\u6237\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">user_info = {<\/p>\n<p>    &#39;alice&#39;: {&#39;age&#39;: 25, &#39;email&#39;: &#39;alice@example.com&#39;},<\/p>\n<p>    &#39;bob&#39;: {&#39;age&#39;: 30, &#39;email&#39;: &#39;bob@example.com&#39;}<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7edf\u8ba1\u9891\u7387<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u4e8e\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u7684\u9891\u7387\uff0c\u4f8b\u5982\u7edf\u8ba1\u5355\u8bcd\u5728\u6587\u672c\u4e2d\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;hello world hello&quot;<\/p>\n<p>word_count = {}<\/p>\n<p>for word in text.split():<\/p>\n<p>    word_count[word] = word_count.get(word, 0) + 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5b57\u5178\u7684\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u5b57\u5178\u7684\u6027\u80fd\u4f18\u5316\u5c24\u4e3a\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u5efa\u8bae\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982 <code>defaultdict<\/code> \u548c <code>Counter<\/code> \u63d0\u4f9b\u4e86\u66f4\u9ad8\u6548\u7684\u9ed8\u8ba4\u503c\u5904\u7406\u548c\u8ba1\u6570\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict, Counter<\/p>\n<p>default_dict = defaultdict(int)<\/p>\n<p>counter = Counter(text.split())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u907f\u514d\u91cd\u590d\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u8fed\u4ee3\u548c\u67e5\u627e\u8fc7\u7a0b\u4e2d\uff0c\u907f\u514d\u91cd\u590d\u8ba1\u7b97\u548c\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u907f\u514d\u91cd\u590d\u8ba1\u7b97<\/p>\n<p>if &#39;key&#39; in empty_dict:<\/p>\n<p>    value = empty_dict[&#39;key&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728 Python \u4e2d\u751f\u6210\u7a7a\u5b57\u5178\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u548c <code>dict()<\/code> \u51fd\u6570\uff0c\u5e76\u6269\u5c55\u4ecb\u7ecd\u4e86\u5b57\u5178\u7684\u57fa\u672c\u64cd\u4f5c\u3001\u9ad8\u7ea7\u64cd\u4f5c\u3001\u5b9e\u9645\u5e94\u7528\u4ee5\u53ca\u6027\u80fd\u4f18\u5316\u7b49\u5185\u5bb9\u3002\u901a\u8fc7\u8fd9\u4e9b\u4ecb\u7ecd\uff0c\u5e0c\u671b\u8bfb\u8005\u80fd\u591f\u5168\u9762\u638c\u63e1\u5b57\u5178\u7684\u4f7f\u7528\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u66f4\u597d\u5730\u5e94\u7528\u5b57\u5178\u8fd9\u4e00\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u6216\u8005 <code>dict()<\/code> \u51fd\u6570\u6765\u751f\u6210\u4e00\u4e2a\u7a7a\u5b57\u5178\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>empty_dict = {}<\/code> \u6216\u8005 <code>empty_dict = dict()<\/code> \u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u5b57\u5178\u3002\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u662f\u6709\u6548\u7684\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u559c\u597d\u9009\u62e9\u5176\u4e2d\u4e00\u79cd\u3002<\/p>\n<p><strong>\u7a7a\u5b57\u5178\u53ef\u4ee5\u7528\u4e8e\u54ea\u4e9b\u573a\u666f\uff1f<\/strong><br \/>\u7a7a\u5b57\u5178\u5728\u591a\u79cd\u573a\u666f\u4e0b\u90fd\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5728\u9700\u8981\u52a8\u6001\u6dfb\u52a0\u952e\u503c\u5bf9\u7684\u60c5\u51b5\u4e0b\uff0c\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178\u662f\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u4f60\u53ef\u4ee5\u5728\u540e\u7eed\u7684\u4ee3\u7801\u4e2d\u9010\u6b65\u586b\u5145\u6570\u636e\uff0c\u9002\u7528\u4e8e\u6570\u636e\u5904\u7406\u3001\u5b58\u50a8\u914d\u7f6e\u6216\u7f13\u5b58\u4fe1\u606f\u7b49\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5411\u7a7a\u5b57\u5178\u4e2d\u6dfb\u52a0\u5143\u7d20\uff1f<\/strong><br \/>\u5411\u7a7a\u5b57\u5178\u4e2d\u6dfb\u52a0\u5143\u7d20\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528\u8d4b\u503c\u8bed\u53e5\u6765\u5b9e\u73b0\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u7a7a\u5b57\u5178 <code>my_dict = {}<\/code>\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>my_dict[&#39;key&#39;] = &#39;value&#39;<\/code> \u7684\u65b9\u5f0f\u6dfb\u52a0\u952e\u503c\u5bf9\u3002\u8fd9\u6837\uff0c\u5b57\u5178\u5c31\u4f1a\u66f4\u65b0\u5e76\u5305\u542b\u65b0\u7684\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u4f55\u68c0\u67e5\u5b57\u5178\u662f\u5426\u4e3a\u7a7a\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u4e00\u4e2a\u5b57\u5178\u662f\u5426\u4e3a\u7a7a\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>if not my_dict:<\/code> \u8fd9\u79cd\u65b9\u5f0f\u3002\u5982\u679c\u5b57\u5178\u4e3a\u7a7a\uff0c\u6761\u4ef6\u5c06\u8fd4\u56de True\uff0c\u53cd\u4e4b\u5219\u8fd4\u56de False\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u6613\u61c2\uff0c\u975e\u5e38\u9002\u5408\u5728\u9700\u8981\u5224\u65ad\u5b57\u5178\u5185\u5bb9\u65f6\u4f7f\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u751f\u6210\u4e00\u4e2a\u7a7a\u5b57\u5178\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u82b1\u62ec\u53f7 {} \u548c dict() \u51fd\u6570\u3002\u8fd9\u4e24\u79cd\u65b9\u5f0f\u90fd\u662f\u521b\u5efa\u7a7a [&hellip;]","protected":false},"author":3,"featured_media":1026817,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026809"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1026809"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026809\/revisions"}],"predecessor-version":[{"id":1026819,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1026809\/revisions\/1026819"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1026817"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1026809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1026809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1026809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}