{"id":934864,"date":"2024-12-26T18:42:12","date_gmt":"2024-12-26T10:42:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/934864.html"},"modified":"2024-12-26T18:42:14","modified_gmt":"2024-12-26T10:42:14","slug":"python-%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e8%af%8d%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/934864.html","title":{"rendered":"python \u5982\u4f55\u521b\u5efa\u8bcd\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071649\/acec0434-0ac2-41dc-8ee5-0afc8f990275.webp\" alt=\"python \u5982\u4f55\u521b\u5efa\u8bcd\u5178\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u521b\u5efa\u8bcd\u5178\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5927\u62ec\u53f7\u3001dict()\u6784\u9020\u51fd\u6570\u548c\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\u3002<\/strong> \u4f7f\u7528\u5927\u62ec\u53f7\u662f\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3001\u800c\u4f7f\u7528dict()\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u63d0\u4f9b\u66f4\u591a\u7684\u7075\u6d3b\u6027\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f\u5219\u53ef\u4ee5\u7528\u4e8e\u52a8\u6001\u751f\u6210\u8bcd\u5178\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u51e0\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5927\u62ec\u53f7\u521b\u5efa\u8bcd\u5178<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u662f\u521b\u5efa\u8bcd\u5178\u6700\u5e38\u89c1\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u5c06\u952e\u503c\u5bf9\u653e\u5165\u5927\u62ec\u53f7\u4e2d\uff0c\u53ef\u4ee5\u5feb\u901f\u5b9a\u4e49\u4e00\u4e2a\u8bcd\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u8bcd\u5178<\/p>\n<p>student_grades = {&quot;Alice&quot;: 85, &quot;Bob&quot;: 92, &quot;Charlie&quot;: 78}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li><strong>\u952e\u503c\u5bf9\u7684\u6982\u5ff5<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u8bcd\u5178\u4e2d\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u952e\u503c\u5bf9\u3002\u952e\u548c\u503c\u4e4b\u95f4\u7528\u5192\u53f7 <code>:<\/code> \u5206\u9694\uff0c\u591a\u4e2a\u952e\u503c\u5bf9\u4e4b\u95f4\u7528\u9017\u53f7 <code>,<\/code> \u5206\u9694\u3002\u952e\u5fc5\u987b\u662f\u4e0d\u53ef\u53d8\u7684\uff08\u4f8b\u5982\uff0c\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u6216\u5143\u7ec4\uff09\uff0c\u800c\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8bbf\u95ee\u548c\u4fee\u6539\u8bcd\u5178<\/strong><\/li>\n<\/ol>\n<p><p>\u4e00\u65e6\u521b\u5efa\u4e86\u8bcd\u5178\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u8bbf\u95ee\u5bf9\u5e94\u7684\u503c\uff0c\u4e5f\u53ef\u4ee5\u6dfb\u52a0\u3001\u4fee\u6539\u6216\u5220\u9664\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbf\u95ee\u8bcd\u5178\u4e2d\u7684\u503c<\/p>\n<p>alice_grade = student_grades[&quot;Alice&quot;]<\/p>\n<h2><strong>\u4fee\u6539\u8bcd\u5178\u4e2d\u7684\u503c<\/strong><\/h2>\n<p>student_grades[&quot;Alice&quot;] = 88<\/p>\n<h2><strong>\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>student_grades[&quot;David&quot;] = 90<\/p>\n<h2><strong>\u5220\u9664\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>del student_grades[&quot;Charlie&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528dict()\u6784\u9020\u51fd\u6570\u521b\u5efa\u8bcd\u5178<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u76f4\u63a5\u4f7f\u7528\u5927\u62ec\u53f7\uff0cPython \u8fd8\u63d0\u4f9b\u4e86 <code>dict()<\/code> \u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u8bcd\u5178\u3002\u8fd9\u79cd\u65b9\u6cd5\u5141\u8bb8\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u6765\u5b9a\u4e49\u8bcd\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528dict()\u6784\u9020\u51fd\u6570\u521b\u5efa\u8bcd\u5178<\/p>\n<p>student_grades = dict(Alice=85, Bob=92, Charlie=78)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u5217\u8868\u6216\u5143\u7ec4\u521b\u5efa\u8bcd\u5178<\/strong><\/li>\n<\/ol>\n<p><p><code>dict()<\/code> \u8fd8\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u5305\u542b\u952e\u503c\u5bf9\u7684\u5217\u8868\u6216\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5217\u8868\u521b\u5efa\u8bcd\u5178<\/p>\n<p>student_grades = dict([(&quot;Alice&quot;, 85), (&quot;Bob&quot;, 92), (&quot;Charlie&quot;, 78)])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5b57\u5178\u7684\u53ef\u53d8\u6027<\/strong><\/li>\n<\/ol>\n<p><p>\u8bcd\u5178\u662f\u53ef\u53d8\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u52a8\u6001\u5730\u8fdb\u884c\u4fee\u6539\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u968f\u65f6\u6dfb\u52a0\u3001\u66f4\u65b0\u6216\u5220\u9664\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u8bcd\u5178<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u52a8\u6001\u751f\u6210\u8bcd\u5178\u3002\u5b83\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u7528\u4e8e\u751f\u6210\u8bcd\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u521b\u5efa\u8bcd\u5178<\/p>\n<p>squared_numbers = {x: x2 for x in range(1, 6)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol>\n<li><strong>\u6761\u4ef6\u63a8\u5bfc<\/strong><\/li>\n<\/ol>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u8fd8\u53ef\u4ee5\u5305\u542b\u6761\u4ef6\u903b\u8f91\uff0c\u4ee5\u4fbf\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u751f\u6210\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u6761\u4ef6\u751f\u6210\u8bcd\u5178<\/p>\n<p>even_squared_numbers = {x: x2 for x in range(1, 11) if x % 2 == 0}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u7ed3\u6784<\/strong><\/li>\n<\/ol>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u6765\u751f\u6210\u590d\u6742\u7684\u8bcd\u5178\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4ece\u4e24\u4e2a\u5217\u8868\u751f\u6210\u4e00\u4e2a\u8bcd\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ece\u4e24\u4e2a\u5217\u8868\u751f\u6210\u8bcd\u5178<\/p>\n<p>keys = [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;]<\/p>\n<p>values = [85, 92, 78]<\/p>\n<p>student_grades = {k: v for k, v in zip(keys, values)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u8bcd\u5178\u7684\u904d\u5386\u548c\u5e38\u7528\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u8bcd\u5178\u540e\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u904d\u5386\u548c\u64cd\u4f5c\u3002Python \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e9b\u9700\u6c42\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u904d\u5386\u8bcd\u5178<\/strong><\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u6765\u904d\u5386\u8bcd\u5178\u7684\u952e\u3001\u503c\u6216\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u8bcd\u5178\u7684\u952e<\/p>\n<p>for key in student_grades:<\/p>\n<p>    print(key)<\/p>\n<h2><strong>\u904d\u5386\u8bcd\u5178\u7684\u503c<\/strong><\/h2>\n<p>for value in student_grades.values():<\/p>\n<p>    print(value)<\/p>\n<h2><strong>\u904d\u5386\u8bcd\u5178\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>for key, value in student_grades.items():<\/p>\n<p>    print(f&quot;{key}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8bcd\u5178\u7684\u5e38\u7528\u65b9\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>Python \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u8bcd\u5178\uff0c\u4f8b\u5982 <code>get()<\/code>\u3001<code>pop()<\/code>\u3001<code>update()<\/code> \u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528get()\u65b9\u6cd5\u5b89\u5168\u5730\u8bbf\u95ee\u8bcd\u5178\u7684\u503c<\/p>\n<p>alice_grade = student_grades.get(&quot;Alice&quot;, &quot;Not Found&quot;)<\/p>\n<h2><strong>\u4f7f\u7528pop()\u65b9\u6cd5\u79fb\u9664\u5e76\u8fd4\u56de\u6307\u5b9a\u952e\u7684\u503c<\/strong><\/h2>\n<p>bob_grade = student_grades.pop(&quot;Bob&quot;, &quot;Not Found&quot;)<\/p>\n<h2><strong>\u4f7f\u7528update()\u65b9\u6cd5\u5408\u5e76\u4e24\u4e2a\u8bcd\u5178<\/strong><\/h2>\n<p>new_grades = {&quot;David&quot;: 90, &quot;Eve&quot;: 88}<\/p>\n<p>student_grades.update(new_grades)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u8bcd\u5178\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u8bcd\u5178\u662f\u4e00\u79cd\u975e\u5e38\u6709\u7528\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u7f16\u7a0b\u573a\u666f\u4e2d\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6570\u636e\u6620\u5c04<\/strong><\/li>\n<\/ol>\n<p><p>\u8bcd\u5178\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u548c\u67e5\u627e\u952e\u503c\u5bf9\uff0c\u5982\u914d\u7f6e\u6587\u4ef6\u3001\u7528\u6237\u4fe1\u606f\u3001\u5546\u54c1\u4ef7\u683c\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u8bcd\u5178\u5b58\u50a8\u5546\u54c1\u4ef7\u683c<\/p>\n<p>product_prices = {&quot;Apple&quot;: 0.5, &quot;Banana&quot;: 0.3, &quot;Cherry&quot;: 1.2}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8ba1\u6570\u7edf\u8ba1<\/strong><\/li>\n<\/ol>\n<p><p>\u8bcd\u5178\u53ef\u4ee5\u7528\u4e8e\u7edf\u8ba1\u6570\u636e\u9891\u7387\uff0c\u5982\u7edf\u8ba1\u5355\u8bcd\u51fa\u73b0\u6b21\u6570\u3001\u5b57\u7b26\u9891\u7387\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u8bcd\u5178\u7edf\u8ba1\u5355\u8bcd\u51fa\u73b0\u6b21\u6570<\/p>\n<p>word_count = {}<\/p>\n<p>text = &quot;hello world hello python&quot;<\/p>\n<p>words = text.split()<\/p>\n<p>for word in words:<\/p>\n<p>    word_count[word] = word_count.get(word, 0) + 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u7f13\u5b58\u673a\u5236<\/strong><\/li>\n<\/ol>\n<p><p>\u8bcd\u5178\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u7b80\u5355\u7684\u7f13\u5b58\u673a\u5236\uff0c\u901a\u8fc7\u5b58\u50a8\u8ba1\u7b97\u7ed3\u679c\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u8bcd\u5178\u5b9e\u73b0\u7b80\u5355\u7684\u7f13\u5b58\u673a\u5236<\/p>\n<p>cache = {}<\/p>\n<p>def fib(n):<\/p>\n<p>    if n in cache:<\/p>\n<p>        return cache[n]<\/p>\n<p>    if n &lt;= 1:<\/p>\n<p>        return n<\/p>\n<p>    result = fib(n-1) + fib(n-2)<\/p>\n<p>    cache[n] = result<\/p>\n<p>    return result<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u8bcd\u5178\u7684\u6df1\u5c42\u6b21\u7406\u89e3<\/p>\n<\/p>\n<p><p>\u6df1\u5165\u7406\u89e3\u8bcd\u5178\u7684\u5b9e\u73b0\u548c\u7279\u6027\uff0c\u6709\u52a9\u4e8e\u6211\u4eec\u66f4\u6709\u6548\u5730\u4f7f\u7528\u5b83\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u54c8\u5e0c\u8868<\/strong><\/li>\n<\/ol>\n<p><p>\u8bcd\u5178\u7684\u5e95\u5c42\u5b9e\u73b0\u662f\u54c8\u5e0c\u8868\uff0c\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u5c06\u952e\u6620\u5c04\u5230\u7279\u5b9a\u7684\u5b58\u50a8\u4f4d\u7f6e\uff0c\u4ece\u800c\u5b9e\u73b0\u5feb\u901f\u7684\u67e5\u627e\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5185\u5b58\u6d88\u8017<\/strong><\/li>\n<\/ol>\n<p><p>\u7531\u4e8e\u8bcd\u5178\u9700\u8981\u989d\u5916\u7684\u5185\u5b58\u6765\u5b58\u50a8\u54c8\u5e0c\u8868\uff0c\u56e0\u6b64\u5728\u5b58\u50a8\u5927\u91cf\u6570\u636e\u65f6\u53ef\u80fd\u4f1a\u6bd4\u5217\u8868\u6216\u5143\u7ec4\u6d88\u8017\u66f4\u591a\u7684\u5185\u5b58\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u7ebf\u7a0b\u5b89\u5168<\/strong><\/li>\n<\/ol>\n<p><p>Python \u7684\u8bcd\u5178\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u56e0\u6b64\u5728\u5e76\u53d1\u8bbf\u95ee\u65f6\u9700\u8981\u4f7f\u7528\u9501\u673a\u5236\u8fdb\u884c\u4fdd\u62a4\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u63d0\u5347\u8bcd\u5178\u6027\u80fd\u7684\u6280\u5de7<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u8bcd\u5178\u8fdb\u884c\u4f18\u5316\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/strong><\/li>\n<\/ol>\n<p><p>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4f8b\u5982\u5728\u9700\u8981\u6709\u5e8f\u5b58\u50a8\u65f6\u53ef\u4ee5\u4f7f\u7528 <code>collections.OrderedDict<\/code>\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u907f\u514d\u91cd\u590d\u8ba1\u7b97<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u7f13\u5b58\u673a\u5236\u6216\u5b57\u5178\u63a8\u5bfc\u5f0f\u907f\u514d\u91cd\u590d\u8ba1\u7b97\uff0c\u4ece\u800c\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/strong><\/li>\n<\/ol>\n<p><p>Python \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\u548c\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u63d0\u9ad8\u8bcd\u5178\u64cd\u4f5c\u7684\u6027\u80fd\uff0c\u4f8b\u5982 <code>collections.defaultdict<\/code> \u548c <code>collections.Counter<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython \u63d0\u4f9b\u4e86\u591a\u79cd\u521b\u5efa\u8bcd\u5178\u7684\u65b9\u6cd5\uff0c\u5e76\u4e14\u8bcd\u5178\u4f5c\u4e3a\u4e00\u79cd\u9ad8\u6548\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5728\u7f16\u7a0b\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u901a\u8fc7\u638c\u63e1\u8bcd\u5178\u7684\u57fa\u672c\u7528\u6cd5\u3001\u5e38\u7528\u64cd\u4f5c\u548c\u6027\u80fd\u4f18\u5316\u6280\u5de7\uff0c\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u8bcd\u5178\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u6216 <code>dict()<\/code> \u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u7a7a\u8bcd\u5178\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u5927\u62ec\u53f7\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>my_dict = {}<\/code>\u3002\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\u5219\u662f\uff1a<code>my_dict = dict()<\/code>\u3002\u8fd9\u4e24\u79cd\u65b9\u5f0f\u90fd\u53ef\u4ee5\u6709\u6548\u5730\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684\u5b57\u5178\uff0c\u540e\u7eed\u53ef\u4ee5\u901a\u8fc7\u952e\u503c\u5bf9\u6dfb\u52a0\u5143\u7d20\u3002<\/p>\n<p><strong>\u5982\u4f55\u5411Python\u8bcd\u5178\u4e2d\u6dfb\u52a0\u6216\u66f4\u65b0\u5143\u7d20\uff1f<\/strong><br \/>\u8981\u5411\u8bcd\u5178\u4e2d\u6dfb\u52a0\u65b0\u5143\u7d20\u6216\u66f4\u65b0\u5df2\u6709\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u952e\u6765\u8bbf\u95ee\u8bcd\u5178\u5e76\u8d4b\u503c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8981\u6dfb\u52a0\u4e00\u4e2a\u952e\u4e3a <code>&quot;name&quot;<\/code>\uff0c\u503c\u4e3a <code>&quot;Alice&quot;<\/code> \u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>my_dict[&quot;name&quot;] = &quot;Alice&quot;<\/code>\u3002\u5982\u679c <code>&quot;name&quot;<\/code> \u5df2\u7ecf\u5b58\u5728\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u5c06\u66f4\u65b0\u5176\u503c\u4e3a <code>&quot;Alice&quot;<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5220\u9664\u8bcd\u5178\u4e2d\u7684\u67d0\u4e2a\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u8981\u5220\u9664\u8bcd\u5178\u4e2d\u7684\u67d0\u4e2a\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>del<\/code> \u8bed\u53e5\u6216 <code>pop()<\/code> \u65b9\u6cd5\u3002\u4f7f\u7528 <code>del<\/code> \u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>del my_dict[&quot;name&quot;]<\/code>\uff0c\u8fd9\u5c06\u5220\u9664 <code>&quot;name&quot;<\/code> \u8fd9\u4e2a\u952e\u53ca\u5176\u5bf9\u5e94\u7684\u503c\u3002\u800c\u4f7f\u7528 <code>pop()<\/code> \u65b9\u6cd5\u5219\u662f\uff1a<code>my_dict.pop(&quot;name&quot;)<\/code>\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u8fd8\u53ef\u4ee5\u8fd4\u56de\u88ab\u5220\u9664\u7684\u503c\u3002\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c<code>pop()<\/code> \u65b9\u6cd5\u4f1a\u5f15\u53d1 KeyError \u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u521b\u5efa\u8bcd\u5178\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5927\u62ec\u53f7\u3001dict()\u6784\u9020\u51fd\u6570\u548c\u901a\u8fc7\u5b57\u5178\u63a8\u5bfc\u5f0f\u3002 \u4f7f\u7528\u5927\u62ec\u53f7\u662f\u6700\u76f4\u63a5 [&hellip;]","protected":false},"author":3,"featured_media":934867,"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\/934864"}],"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=934864"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/934864\/revisions"}],"predecessor-version":[{"id":934868,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/934864\/revisions\/934868"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/934867"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=934864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=934864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=934864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}