{"id":968216,"date":"2024-12-27T05:04:59","date_gmt":"2024-12-26T21:04:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/968216.html"},"modified":"2024-12-27T05:05:01","modified_gmt":"2024-12-26T21:05:01","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e9%94%ae%e5%80%bc%e5%af%b9","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/968216.html","title":{"rendered":"python\u5982\u4f55\u5b9a\u4e49\u952e\u503c\u5bf9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183105\/b2727daf-a5ac-4365-8371-d0bd317b8141.webp\" alt=\"python\u5982\u4f55\u5b9a\u4e49\u952e\u503c\u5bf9\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u952e\u503c\u5bf9\u7684\u4e3b\u8981\u65b9\u5f0f\u662f\u901a\u8fc7\u5b57\u5178\uff08dictionary\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u82b1\u62ec\u53f7{}\u3001dict()\u6784\u9020\u51fd\u6570\u7b49\u65b9\u5f0f\u6765\u5b9a\u4e49\u3002\u5b57\u5178\u662f\u4e00\u79cd\u53ef\u53d8\u7684\u3001\u65e0\u5e8f\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u901a\u8fc7\u952e\u6765\u5b58\u50a8\u548c\u8bbf\u95ee\u503c\u3002\u5e38\u7528\u7684\u5b9a\u4e49\u65b9\u5f0f\u5305\u62ec\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528dict()\u51fd\u6570\u4ee5\u53ca\u901a\u8fc7zip()\u51fd\u6570\u521b\u5efa\u3002<\/strong> \u5176\u4e2d\uff0c\u76f4\u63a5\u8d4b\u503c\u662f\u6700\u5e38\u7528\u4e14\u7b80\u5355\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u4f7f\u7528\u82b1\u62ec\u53f7{}\u76f4\u63a5\u5c06\u952e\u503c\u5bf9\u5199\u5165\uff0c\u5373\u53ef\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u5c55\u5f00\u5982\u4f55\u5b9a\u4e49\u548c\u4f7f\u7528\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u82b1\u62ec\u53f7{}\u5b9a\u4e49\u5b57\u5178<\/p>\n<\/p>\n<p><p>Python\u5b57\u5178\u662f\u901a\u8fc7\u82b1\u62ec\u53f7{}\u6765\u5b9a\u4e49\u7684\uff0c\u6bcf\u4e2a\u952e\u503c\u5bf9\u7531\u5192\u53f7:\u5206\u9694\uff0c\u591a\u4e2a\u952e\u503c\u5bf9\u4e4b\u95f4\u7528\u9017\u53f7,\u5206\u9694\u3002\u952e\u901a\u5e38\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u4f46\u4e5f\u53ef\u4ee5\u662f\u4efb\u4f55\u4e0d\u53ef\u53d8\u7c7b\u578b\uff0c\u5982\u6574\u6570\u3001\u5143\u7ec4\u7b49\uff0c\u503c\u5219\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u5217\u8868\u3001\u5143\u7ec4\u3001\u751a\u81f3\u53e6\u4e00\u4e2a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178<\/p>\n<p>person = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 25, &quot;city&quot;: &quot;New York&quot;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>person<\/code>\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u5305\u542b\u4e09\u4e2a\u952e\u503c\u5bf9\uff1a&quot;name&quot;\u5bf9\u5e94\u7684\u503c\u662f&quot;Alice&quot;\uff0c&quot;age&quot;\u5bf9\u5e94\u7684\u503c\u662f25\uff0c&quot;city&quot;\u5bf9\u5e94\u7684\u503c\u662f&quot;New York&quot;\u3002\u8fd9\u79cd\u5b9a\u4e49\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u5e76\u4e14\u662f\u6700\u5e38\u7528\u7684\u4e00\u79cd\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528dict()\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u82b1\u62ec\u53f7\u76f4\u63a5\u5b9a\u4e49\u5b57\u5178\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>dict()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u5b57\u5178\u3002<code>dict()<\/code>\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4e00\u7cfb\u5217\u952e\u503c\u5bf9\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd9\u4e9b\u53c2\u6570\u53ef\u4ee5\u901a\u8fc7\u5173\u952e\u5b57\u53c2\u6570\u4f20\u9012\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u4e8c\u5143\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528dict()\u51fd\u6570\u5b9a\u4e49\u5b57\u5178<\/p>\n<p>person = dict(name=&quot;Alice&quot;, age=25, city=&quot;New York&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u5143\u7ec4\u5217\u8868\u5b9a\u4e49\u5b57\u5178<\/strong><\/h2>\n<p>person = dict([(&quot;name&quot;, &quot;Alice&quot;), (&quot;age&quot;, 25), (&quot;city&quot;, &quot;New York&quot;)])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>dict()<\/code>\u51fd\u6570\u5b9a\u4e49\u5b57\u5178\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u5c24\u5176\u5728\u9700\u8981\u52a8\u6001\u751f\u6210\u5b57\u5178\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528zip()\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4ece\u4e24\u4e2a\u5217\u8868\u521b\u5efa\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u4e00\u4e2a\u5217\u8868\u5305\u542b\u952e\uff0c\u53e6\u4e00\u4e2a\u5217\u8868\u5305\u542b\u76f8\u5e94\u7684\u503c\u3002\u8fd9\u65f6\u5019\u53ef\u4ee5\u4f7f\u7528<code>zip()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528zip()\u51fd\u6570\u5b9a\u4e49\u5b57\u5178<\/p>\n<p>keys = [&quot;name&quot;, &quot;age&quot;, &quot;city&quot;]<\/p>\n<p>values = [&quot;Alice&quot;, 25, &quot;New York&quot;]<\/p>\n<p>person = dict(zip(keys, values))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>zip()<\/code>\u51fd\u6570\uff0c\u5c06\u4e24\u4e2a\u5217\u8868\u7ec4\u5408\u6210\u4e00\u4e2a\u53ef\u8fed\u4ee3\u7684\u5143\u7ec4\u5bf9\uff0c\u7136\u540e\u518d\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u5178\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5904\u7406\u6570\u636e\u96c6\u5408\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b57\u5178\u7684\u5e38\u7528\u64cd\u4f5c<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u4f1a\u629b\u51fa<code>KeyError<\/code>\u5f02\u5e38\uff0c\u56e0\u6b64\u5efa\u8bae\u4f7f\u7528<code>get()<\/code>\u65b9\u6cd5\u6765\u5b89\u5168\u5730\u8bbf\u95ee\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c<\/p>\n<p>name = person[&quot;name&quot;]  # \u76f4\u63a5\u8bbf\u95ee<\/p>\n<p>age = person.get(&quot;age&quot;)  # \u4f7f\u7528get()\u65b9\u6cd5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u503c<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u5f0f\u6765\u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u503c\u3002\u5982\u679c\u952e\u5b58\u5728\uff0c\u5219\u66f4\u65b0\u5bf9\u5e94\u7684\u503c\uff1b\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u66f4\u65b0\u5b57\u5178\u4e2d\u7684\u503c<\/p>\n<p>person[&quot;age&quot;] = 30  # \u66f4\u65b0<\/p>\n<p>person[&quot;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&quot;] = &quot;alice@example.com&quot;  # \u6dfb\u52a0\u65b0\u952e\u503c\u5bf9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>del<\/code>\u8bed\u53e5\u6216<code>pop()<\/code>\u65b9\u6cd5\u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002<code>del<\/code>\u8bed\u53e5\u76f4\u63a5\u5220\u9664\u6307\u5b9a\u952e\u5bf9\u5e94\u7684\u952e\u503c\u5bf9\uff0c\u800c<code>pop()<\/code>\u65b9\u6cd5\u5219\u4f1a\u8fd4\u56de\u88ab\u5220\u9664\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9<\/p>\n<p>del person[&quot;city&quot;]  # \u4f7f\u7528del\u8bed\u53e5<\/p>\n<p>email = person.pop(&quot;email&quot;, None)  # \u4f7f\u7528pop()\u65b9\u6cd5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u904d\u5386\u5b57\u5178<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u904d\u5386\u5b57\u5178\u4e2d\u7684\u952e\u3001\u503c\u6216\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u5b57\u5178<\/p>\n<p>for key in person:<\/p>\n<p>    print(key, person[key])  # \u904d\u5386\u952e<\/p>\n<p>for value in person.values():<\/p>\n<p>    print(value)  # \u904d\u5386\u503c<\/p>\n<p>for key, value in person.items():<\/p>\n<p>    print(key, value)  # \u904d\u5386\u952e\u503c\u5bf9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5b57\u5178\u7684\u9ad8\u7ea7\u5e94\u7528<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b57\u5178\u63a8\u5bfc\u5f0f<\/strong><\/li>\n<\/ol>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u5feb\u901f\u521b\u5efa\u5b57\u5178\u7684\u65b9\u5f0f\uff0c\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u6839\u636e\u67d0\u4e2a\u89c4\u5219\u751f\u6210\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b57\u5178\u63a8\u5bfc\u5f0f<\/p>\n<p>squares = {x: x2 for x in range(6)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>squares<\/code>\u5b57\u5178\u5c06\u6570\u5b570\u52305\u4f5c\u4e3a\u952e\uff0c\u5e73\u65b9\u503c\u4f5c\u4e3a\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5408\u5e76\u5b57\u5178<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Python 3.9\u53ca\u4ee5\u4e0a\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>|<\/code>\u8fd0\u7b97\u7b26\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5408\u5e76\u5b57\u5178<\/p>\n<p>dict1 = {&quot;a&quot;: 1, &quot;b&quot;: 2}<\/p>\n<p>dict2 = {&quot;b&quot;: 3, &quot;c&quot;: 4}<\/p>\n<p>merged_dict = dict1 | dict2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5408\u5e76\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u4e24\u4e2a\u5b57\u5178\u4e2d\u5b58\u5728\u76f8\u540c\u7684\u952e\uff0c\u540e\u9762\u7684\u5b57\u5178\u4e2d\u7684\u503c\u4f1a\u8986\u76d6\u524d\u9762\u7684\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528<code>collections.defaultdict<\/code><\/strong><\/li>\n<\/ol>\n<p><p><code>collections.defaultdict<\/code>\u662f<code>dict<\/code>\u7684\u4e00\u4e2a\u5b50\u7c7b\uff0c\u5b83\u5728\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\u65f6\u4e0d\u4f1a\u629b\u51fa<code>KeyError<\/code>\u5f02\u5e38\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import defaultdict<\/p>\n<h2><strong>\u4f7f\u7528defaultdict<\/strong><\/h2>\n<p>dd = defaultdict(int)<\/p>\n<p>dd[&quot;a&quot;] += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>defaultdict<\/code>\u5728\u5904\u7406\u8ba1\u6570\u4efb\u52a1\u65f6\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u81ea\u52a8\u521d\u59cb\u5316\u8ba1\u6570\u5668\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5b57\u5178\u7684\u6027\u80fd\u4f18\u5316<\/p>\n<\/p>\n<ol>\n<li><strong>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u9700\u8981\u4fdd\u6301\u987a\u5e8f\u65f6\uff0c\u4f7f\u7528<code>collections.OrderedDict<\/code>\uff0c\u800c\u5728\u9700\u8981\u5904\u7406\u591a\u5bf9\u4e00\u5173\u7cfb\u65f6\uff0c\u4f7f\u7528<code>collections.defaultdict<\/code>\u53ef\u80fd\u66f4\u5408\u9002\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u62f7\u8d1d<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u8fdb\u884c\u5b57\u5178\u64cd\u4f5c\u65f6\uff0c\u5c3d\u91cf\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5b57\u5178\u62f7\u8d1d\u3002\u4f7f\u7528\u5b57\u5178\u89c6\u56fe\uff08\u5982<code>keys()<\/code>\u3001<code>values()<\/code>\u3001<code>items()<\/code>\uff09\u6765\u8bbf\u95ee\u5b57\u5178\u5185\u5bb9\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u590d\u5236\u5b57\u5178\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528<code>setdefault()<\/code>\u65b9\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p><code>setdefault()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff0c\u4ece\u800c\u907f\u514d\u91cd\u590d\u7684\u6761\u4ef6\u68c0\u67e5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528setdefault()\u65b9\u6cd5<\/p>\n<p>person.setdefault(&quot;email&quot;, &quot;unknown@example.com&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u5b57\u5178\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u5728Python\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u3001\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c\u7684\u573a\u5408\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u914d\u7f6e\u7ba1\u7406<\/strong><\/li>\n<\/ol>\n<p><p>\u5b57\u5178\u5e38\u7528\u4e8e\u5b58\u50a8\u548c\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\u7684\u914d\u7f6e\u53c2\u6570\u3002\u8fd9\u4e9b\u914d\u7f6e\u53c2\u6570\u53ef\u4ee5\u662f\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u7684\u952e\u503c\u5bf9\uff0c\u4e5f\u53ef\u4ee5\u662f\u4ece\u547d\u4ee4\u884c\u53c2\u6570\u89e3\u6790\u5f97\u5230\u7684\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6570\u636e\u805a\u5408<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u4e2d\uff0c\u5b57\u5178\u5e38\u7528\u4e8e\u6570\u636e\u7684\u805a\u5408\u548c\u7edf\u8ba1\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u8ba1\u6570\u5b57\u7b26\u51fa\u73b0\u7684\u9891\u7387\uff0c\u6216\u8005\u805a\u5408\u67d0\u4e00\u5b57\u6bb5\u7684\u6570\u503c\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u7f13\u5b58\u673a\u5236<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5b9e\u73b0\u7f13\u5b58\u673a\u5236\u65f6\uff0c\u5b57\u5178\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u3002\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u5b58\u50a8\u5df2\u7ecf\u8ba1\u7b97\u8fc7\u7684\u7ed3\u679c\uff0c\u4ee5\u4fbf\u5728\u540e\u7eed\u8ba1\u7b97\u4e2d\u76f4\u63a5\u4f7f\u7528\uff0c\u51cf\u5c11\u91cd\u590d\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u6570\u636e\u6620\u5c04<\/strong><\/li>\n<\/ol>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u6620\u5c04\uff0c\u5c06\u4e00\u4e2a\u503c\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u503c\u3002\u8fd9\u5728\u6570\u636e\u6e05\u6d17\u3001\u6570\u636e\u8f6c\u6362\u7b49\u64cd\u4f5c\u4e2d\u975e\u5e38\u5e38\u89c1\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5b57\u5178\u662f\u4e00\u79cd\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u63d0\u4f9b\u4e86\u5feb\u901f\u7684\u952e\u503c\u5bf9\u5b58\u50a8\u548c\u8bbf\u95ee\u65b9\u5f0f\u3002\u901a\u8fc7\u638c\u63e1\u5b57\u5178\u7684\u5b9a\u4e49\u548c\u64cd\u4f5c\uff0c\u53ef\u4ee5\u5728\u6570\u636e\u5904\u7406\u4e2d\u66f4\u9ad8\u6548\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u6570\u636e\u3002\u65e0\u8bba\u662f\u5728\u7b80\u5355\u7684\u6570\u636e\u5b58\u50a8\uff0c\u8fd8\u662f\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5b57\u5178\u90fd\u80fd\u53d1\u6325\u51fa\u5176\u72ec\u7279\u7684\u4f18\u52bf\u3002\u5e0c\u671b\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u60a8\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3\u548c\u8fd0\u7528Python\u5b57\u5178\uff0c\u66f4\u52a0\u719f\u7ec3\u5730\u5904\u7406\u952e\u503c\u5bf9\u76f8\u5173\u7684\u7f16\u7a0b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u5b57\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u7528\u6765\u5b58\u50a8\u952e\u503c\u5bf9\u7684\u96c6\u5408\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code> \u6765\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178\u3002\u4f8b\u5982\uff1a<code>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/code>\u3002\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c&#39;name&#39; \u548c &#39;age&#39; \u662f\u952e\uff0c\u5bf9\u5e94\u7684\u503c\u5206\u522b\u662f &#39;Alice&#39; \u548c 25\u3002<\/p>\n<p><strong>Python\u5b57\u5178\u4e2d\u7684\u952e\u548c\u503c\u53ef\u4ee5\u662f\u4ec0\u4e48\u7c7b\u578b\uff1f<\/strong><br \/>\u5728Python\u5b57\u5178\u4e2d\uff0c\u952e\u53ef\u4ee5\u662f\u4efb\u4f55\u4e0d\u53ef\u53d8\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u6216\u5143\u7ec4\uff0c\u800c\u503c\u5219\u53ef\u4ee5\u662f\u4efb\u610f\u7c7b\u578b\uff0c\u5305\u62ec\u5217\u8868\u3001\u5b57\u5178\u3001\u5bf9\u8c61\u7b49\u3002\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u5f97\u5b57\u5178\u6210\u4e3a\u5b58\u50a8\u590d\u6742\u6570\u636e\u7ed3\u6784\u7684\u7406\u60f3\u9009\u62e9\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bbf\u95ee\u548c\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u503c\uff1f<\/strong><br \/>\u8981\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u952e\u8fdb\u884c\u7d22\u5f15\uff0c\u4f8b\u5982\uff1a<code>my_dict[&#39;name&#39;]<\/code> \u5c06\u8fd4\u56de &#39;Alice&#39;\u3002\u5982\u679c\u9700\u8981\u4fee\u6539\u67d0\u4e2a\u952e\u5bf9\u5e94\u7684\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u8d4b\u503c\uff0c\u5982\uff1a<code>my_dict[&#39;age&#39;] = 30<\/code>\u3002\u8fd9\u5c06\u628a &#39;age&#39; \u7684\u503c\u66f4\u65b0\u4e3a 30\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5b9a\u4e49\u952e\u503c\u5bf9\u7684\u4e3b\u8981\u65b9\u5f0f\u662f\u901a\u8fc7\u5b57\u5178\uff08dictionary\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u82b1\u62ec\u53f7{}\u3001dict()\u6784\u9020 [&hellip;]","protected":false},"author":3,"featured_media":968220,"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\/968216"}],"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=968216"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/968216\/revisions"}],"predecessor-version":[{"id":968222,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/968216\/revisions\/968222"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/968220"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=968216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=968216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=968216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}