{"id":1059137,"date":"2024-12-31T15:22:45","date_gmt":"2024-12-31T07:22:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1059137.html"},"modified":"2024-12-31T15:22:47","modified_gmt":"2024-12-31T07:22:47","slug":"%e4%bb%80%e4%b9%88%e6%98%afpython%e4%b8%ad%e7%9a%84%e5%ad%97%e5%85%b8%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1059137.html","title":{"rendered":"\u4ec0\u4e48\u662fpython\u4e2d\u7684\u5b57\u5178\u5982\u4f55\u8868\u793a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/0bc72532-aaac-404f-975f-50351c93ad7f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u4ec0\u4e48\u662fpython\u4e2d\u7684\u5b57\u5178\u5982\u4f55\u8868\u793a\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u7684\u5b57\u5178\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\u6570\u636e\u7ed3\u6784\u7684\u96c6\u5408\u7c7b\u578b\u3002<\/strong> \u5b57\u5178\u4e2d\u7684\u6bcf\u4e2a\u952e\u503c\u5bf9\u7531\u952e\u548c\u503c\u7ec4\u6210\uff0c\u5176\u4e2d\u952e\u662f\u552f\u4e00\u7684\u3002Python\u5b57\u5178\u7684\u8868\u793a\u65b9\u6cd5\u662f\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code>\uff0c\u952e\u503c\u5bf9\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<strong>\u5b57\u5178\u5177\u6709\u5feb\u901f\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u7684\u4f18\u52bf\uff0c\u53ef\u4ee5\u901a\u8fc7\u952e\u5feb\u901f\u8bbf\u95ee\u5bf9\u5e94\u7684\u503c\u3002<\/strong> \u4f8b\u5982\uff0c\u5b57\u5178\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u5b66\u751f\u4fe1\u606f\uff0c\u5176\u4e2d\u952e\u662f\u5b66\u751f\u59d3\u540d\uff0c\u503c\u662f\u4ed6\u4eec\u7684\u5b66\u53f7\u3002<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u521b\u5efa\u548c\u4f7f\u7528\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u5b57\u5178\u4e2d\u6dfb\u52a0\u3001\u5220\u9664\u3001\u66f4\u65b0\u952e\u503c\u5bf9\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u952e\u8bbf\u95ee\u5bf9\u5e94\u7684\u503c\u3002\u5b57\u5178\u8fd8\u652f\u6301\u4f7f\u7528\u5b57\u5178\u65b9\u6cd5\u8fdb\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u5982\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3001\u83b7\u53d6\u6240\u6709\u952e\u6216\u503c\u7b49\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5b57\u5178\u7684\u521b\u5efa\u3001\u57fa\u672c\u64cd\u4f5c\u548c\u9ad8\u7ea7\u7528\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u5178\u7684\u521b\u5efa\u4e0e\u8868\u793a<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5b57\u5178\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u521b\u5efa\u548c\u8868\u793a\u3002\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code>\uff0c\u5728\u5927\u62ec\u53f7\u5185\u90e8\u5199\u5165\u952e\u503c\u5bf9\uff0c\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\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">student_info = {<\/p>\n<p>    &quot;Alice&quot;: 1001,<\/p>\n<p>    &quot;Bob&quot;: 1002,<\/p>\n<p>    &quot;Charlie&quot;: 1003<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5b57\u5178 <code>student_info<\/code> \u5305\u542b\u4e09\u4e2a\u952e\u503c\u5bf9\uff0c\u5176\u4e2d\u952e\u662f\u5b66\u751f\u7684\u540d\u5b57\uff0c\u503c\u662f\u4ed6\u4eec\u7684\u5b66\u53f7\u3002<\/p>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u521b\u5efa\u5b57\u5178\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">student_info = dict(Alice=1001, Bob=1002, Charlie=1003)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u662f\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u4f46\u662f\u952e\u5fc5\u987b\u662f\u6709\u6548\u7684\u6807\u8bc6\u7b26\uff08\u5373\u4e0d\u80fd\u5305\u542b\u7a7a\u683c\u548c\u7279\u6b8a\u5b57\u7b26\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b57\u5178\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u5b57\u5178\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5b57\u5178\u8fdb\u884c\u5404\u79cd\u57fa\u672c\u64cd\u4f5c\uff0c\u5982\u8bbf\u95ee\u3001\u6dfb\u52a0\u3001\u4fee\u6539\u548c\u5220\u9664\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c<\/h4>\n<\/p>\n<p><p>\u8981\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528\u952e\u4f5c\u4e3a\u7d22\u5f15\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(student_info[&quot;Alice&quot;])  # \u8f93\u51fa: 1001<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u8bbf\u95ee\u7684\u952e\u4e0d\u5b58\u5728\uff0c\u4f1a\u5f15\u53d1 <code>KeyError<\/code> \u5f02\u5e38\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>get()<\/code> \u65b9\u6cd5\uff0c\u5b83\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u8fd4\u56de\u9ed8\u8ba4\u503c <code>None<\/code>\uff0c\u6216\u6307\u5b9a\u7684\u9ed8\u8ba4\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(student_info.get(&quot;Dave&quot;, &quot;Not Found&quot;))  # \u8f93\u51fa: Not Found<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6dfb\u52a0\u548c\u4fee\u6539\u952e\u503c\u5bf9<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u968f\u65f6\u5411\u5b57\u5178\u4e2d\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\uff0c\u6216\u4fee\u6539\u73b0\u6709\u952e\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">student_info[&quot;Dave&quot;] = 1004  # \u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/p>\n<p>student_info[&quot;Alice&quot;] = 1101  # \u4fee\u6539\u73b0\u6709\u952e\u7684\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5220\u9664\u952e\u503c\u5bf9<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>del<\/code> \u5173\u952e\u5b57\u6216 <code>pop()<\/code> \u65b9\u6cd5\u4ece\u5b57\u5178\u4e2d\u5220\u9664\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">del student_info[&quot;Bob&quot;]  # \u5220\u9664\u952e\u4e3a &quot;Bob&quot; \u7684\u952e\u503c\u5bf9<\/p>\n<p>value = student_info.pop(&quot;Charlie&quot;)  # \u5220\u9664\u952e\u4e3a &quot;Charlie&quot; \u7684\u952e\u503c\u5bf9\uff0c\u5e76\u8fd4\u56de\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5b57\u5178\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u64cd\u4f5c\u5916\uff0c\u5b57\u5178\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7528\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u904d\u5386\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u904d\u5386\u5b57\u5178\u7684\u952e\u3001\u503c\u6216\u952e\u503c\u5bf9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u952e<\/p>\n<p>for key in student_info.keys():<\/p>\n<p>    print(key)<\/p>\n<h2><strong>\u904d\u5386\u503c<\/strong><\/h2>\n<p>for value in student_info.values():<\/p>\n<p>    print(value)<\/p>\n<h2><strong>\u904d\u5386\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>for key, value in student_info.items():<\/p>\n<p>    print(f&quot;{key}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5408\u5e76\u5b57\u5178<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>update()<\/code> \u65b9\u6cd5\u5c06\u4e00\u4e2a\u5b57\u5178\u7684\u952e\u503c\u5bf9\u66f4\u65b0\u5230\u53e6\u4e00\u4e2a\u5b57\u5178\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">additional_info = {&quot;Eve&quot;: 1005, &quot;Frank&quot;: 1006}<\/p>\n<p>student_info.update(additional_info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u521b\u5efa\u5b57\u5178\u7684\u7b80\u6d01\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4ece\u73b0\u6709\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u751f\u6210\u65b0\u7684\u5b57\u5178\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = {x: x2 for x in range(6)}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4ee3\u7801\u751f\u6210\u4e86\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u662f\u4ece 0 \u5230 5 \u7684\u6574\u6570\uff0c\u503c\u662f\u8fd9\u4e9b\u6574\u6570\u7684\u5e73\u65b9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b57\u5178\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u5b57\u5178\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u65b9\u4fbf\u5730\u64cd\u4f5c\u5b57\u5178\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001<code>clear()<\/code><\/h4>\n<\/p>\n<p><p><code>clear()<\/code> \u65b9\u6cd5\u7528\u4e8e\u6e05\u7a7a\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u503c\u5bf9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">student_info.clear()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001<code>copy()<\/code><\/h4>\n<\/p>\n<p><p><code>copy()<\/code> \u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u5b57\u5178\u7684\u6d45\u590d\u5236\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">copy_info = student_info.copy()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001<code>fromkeys()<\/code><\/h4>\n<\/p>\n<p><p><code>fromkeys()<\/code> \u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\uff0c\u4ee5\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\uff0c\u6307\u5b9a\u7684\u503c\u4f5c\u4e3a\u5b57\u5178\u7684\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keys = [&quot;name&quot;, &quot;age&quot;, &quot;gender&quot;]<\/p>\n<p>default_value = &quot;unknown&quot;<\/p>\n<p>new_dict = dict.fromkeys(keys, default_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001<code>setdefault()<\/code><\/h4>\n<\/p>\n<p><p><code>setdefault()<\/code> \u65b9\u6cd5\u7528\u4e8e\u83b7\u53d6\u6307\u5b9a\u952e\u7684\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u5219\u63d2\u5165\u952e\u5e76\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">name = student_info.setdefault(&quot;Gina&quot;, 1007)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b57\u5178\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u5b58\u50a8\u4e0e\u68c0\u7d22<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u548c\u5feb\u901f\u68c0\u7d22\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u5b58\u50a8\u5b66\u751f\u7684\u6210\u7ee9\u5355\u3001\u56fe\u4e66\u9986\u7684\u4e66\u7c4d\u4fe1\u606f\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8ba1\u6570\u5668<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u4f5c\u8ba1\u6570\u5668\uff0c\u7528\u4e8e\u7edf\u8ba1\u5143\u7d20\u7684\u9891\u6b21\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7edf\u8ba1\u6587\u672c\u4e2d\u6bcf\u4e2a\u5355\u8bcd\u51fa\u73b0\u7684\u6b21\u6570\uff1a<\/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><h4>3\u3001\u5206\u7ec4<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u53ef\u4ee5\u7528\u4e8e\u5c06\u6570\u636e\u5206\u7ec4\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6839\u636e\u5458\u5de5\u7684\u90e8\u95e8\u5c06\u5458\u5de5\u4fe1\u606f\u5206\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">employees = [<\/p>\n<p>    {&quot;name&quot;: &quot;Alice&quot;, &quot;department&quot;: &quot;HR&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Bob&quot;, &quot;department&quot;: &quot;IT&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Charlie&quot;, &quot;department&quot;: &quot;HR&quot;},<\/p>\n<p>    {&quot;name&quot;: &quot;Dave&quot;, &quot;department&quot;: &quot;IT&quot;}<\/p>\n<p>]<\/p>\n<p>grouped_employees = {}<\/p>\n<p>for employee in employees:<\/p>\n<p>    department = employee[&quot;department&quot;]<\/p>\n<p>    if department not in grouped_employees:<\/p>\n<p>        grouped_employees[department] = []<\/p>\n<p>    grouped_employees[department].append(employee)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b57\u5178\u7684\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u5728Python\u4e2d\u662f\u975e\u5e38\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7279\u522b\u662f\u5728\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u4e0a\u3002\u5b57\u5178\u7684\u5b9e\u73b0\u57fa\u4e8e\u54c8\u5e0c\u8868\uff0c\u8fd9\u4f7f\u5f97\u5b83\u5728\u5e73\u5747\u60c5\u51b5\u4e0b\u80fd\u591f\u5728\u5e38\u6570\u65f6\u95f4\u5185\u5b8c\u6210\u8fd9\u4e9b\u64cd\u4f5c\u3002\u7136\u800c\uff0c\u5b57\u5178\u7684\u6027\u80fd\u4e5f\u4f9d\u8d56\u4e8e\u54c8\u5e0c\u51fd\u6570\u7684\u8d28\u91cf\u548c\u952e\u7684\u5206\u5e03\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u67e5\u627e\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u7531\u4e8e\u5b57\u5178\u4f7f\u7528\u54c8\u5e0c\u8868\u5b9e\u73b0\uff0c\u67e5\u627e\u64cd\u4f5c\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(1)\u3002\u8fd9\u610f\u5473\u7740\u65e0\u8bba\u5b57\u5178\u4e2d\u6709\u591a\u5c11\u5143\u7d20\uff0c\u67e5\u627e\u4e00\u4e2a\u952e\u7684\u65f6\u95f4\u662f\u6052\u5b9a\u7684\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u63d2\u5165\u548c\u5220\u9664\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u540c\u6837\u5730\uff0c\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e5f\u662f O(1)\u3002\u4f46\u662f\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u5178\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u8fdb\u884c\u518d\u6563\u5217\uff08rehashing\uff09\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u63d2\u5165\u6216\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u6682\u65f6\u53d8\u4e3a O(n)\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5185\u5b58\u6d88\u8017<\/h4>\n<\/p>\n<p><p>\u7531\u4e8e\u5b57\u5178\u4f7f\u7528\u54c8\u5e0c\u8868\u6765\u5b58\u50a8\u6570\u636e\uff0c\u5b83\u4eec\u53ef\u80fd\u4f1a\u6d88\u8017\u6bd4\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u66f4\u591a\u7684\u5185\u5b58\u3002\u7279\u522b\u662f\u5f53\u5b57\u5178\u4e2d\u6709\u5927\u91cf\u5143\u7d20\u65f6\uff0c\u5185\u5b58\u6d88\u8017\u53ef\u80fd\u4f1a\u663e\u8457\u589e\u52a0\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5b57\u5178\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u4f7f\u7528\u5b57\u5178\uff0c\u6211\u4eec\u53ef\u4ee5\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u9009\u62e9\u5408\u9002\u7684\u952e<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u952e\u53ef\u4ee5\u63d0\u9ad8\u5b57\u5178\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002\u952e\u5e94\u8be5\u662f\u4e0d\u53ef\u53d8\u7684\uff08\u5982\u5b57\u7b26\u4e32\u3001\u6574\u6570\u3001\u5143\u7ec4\uff09\uff0c\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5b57\u5178\u7684\u952e\u662f\u552f\u4e00\u4e14\u53ef\u9760\u7684\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u907f\u514d\u4f7f\u7528\u591a\u5c42\u5d4c\u5957<\/h4>\n<\/p>\n<p><p>\u591a\u5c42\u5d4c\u5957\u7684\u5b57\u5178\u4f1a\u589e\u52a0\u4ee3\u7801\u7684\u590d\u6742\u6027\u548c\u7ef4\u62a4\u6210\u672c\u3002\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u6216\u8bbe\u8ba1\u6a21\u5f0f\u6765\u7b80\u5316\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u3002\u5728\u9700\u8981\u4ece\u73b0\u6709\u6570\u636e\u751f\u6210\u65b0\u5b57\u5178\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u5408\u7406\u4f7f\u7528\u5b57\u5178\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5145\u5206\u5229\u7528\u5b57\u5178\u7684\u5185\u7f6e\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u5e76\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>get()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u907f\u514d\u952e\u4e0d\u5b58\u5728\u65f6\u5f15\u53d1\u7684\u5f02\u5e38\uff0c\u4f7f\u7528 <code>update()<\/code> \u65b9\u6cd5\u53ef\u4ee5\u9ad8\u6548\u5730\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5b57\u5178\u4e0e\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u7684\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5b57\u5178\u4e0e\u5176\u4ed6\u6570\u636e\u7ed3\u6784\uff08\u5982\u5217\u8868\u3001\u96c6\u5408\u3001\u5143\u7ec4\uff09\u6709\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u548c\u6027\u80fd\u7279\u70b9\u3002\u5728\u9009\u62e9\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u5e94\u8be5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u6743\u8861\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b57\u5178\u4e0e\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u662f\u6709\u5e8f\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20\uff0c\u9002\u7528\u4e8e\u987a\u5e8f\u5b58\u50a8\u548c\u5904\u7406\u7684\u6570\u636e\u3002\u5b57\u5178\u662f\u65e0\u5e8f\u7684\uff0c\u901a\u8fc7\u952e\u8bbf\u95ee\u503c\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u548c\u66f4\u65b0\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b57\u5178\u4e0e\u96c6\u5408<\/h4>\n<\/p>\n<p><p>\u96c6\u5408\u662f\u65e0\u5e8f\u7684\uff0c\u4e0d\u5141\u8bb8\u91cd\u590d\u5143\u7d20\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u68c0\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728\u7684\u6570\u636e\u3002\u5b57\u5178\u901a\u8fc7\u952e\u5b58\u50a8\u503c\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5b58\u50a8\u952e\u503c\u5bf9\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5b57\u5178\u4e0e\u5143\u7ec4<\/h4>\n<\/p>\n<p><p>\u5143\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5143\u7d20\uff0c\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u8bc1\u6570\u636e\u4e0d\u88ab\u4fee\u6539\u7684\u573a\u666f\u3002\u5b57\u5178\u662f\u53ef\u53d8\u7684\uff0c\u901a\u8fc7\u952e\u8bbf\u95ee\u503c\uff0c\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u66f4\u65b0\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0c\u5b57\u5178\u662fPython\u4e2d\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u5b58\u50a8\u548c\u5904\u7406\u573a\u666f\u3002\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u5408\u7406\u9009\u62e9\u548c\u4f7f\u7528\u5b57\u5178\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5b57\u5178\u5728Python\u4e2d\u6709\u4ec0\u4e48\u7279\u70b9\uff1f<\/strong><br \/>\u5b57\u5178\u662fPython\u4e2d\u7684\u4e00\u79cd\u5185\u7f6e\u6570\u636e\u7ed3\u6784\uff0c\u4e3b\u8981\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\uff08key-value p<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>rs\uff09\u3002\u6bcf\u4e2a\u952e\u90fd\u4e0e\u4e00\u4e2a\u503c\u76f8\u5173\u8054\uff0c\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684\u4e14\u4e0d\u53ef\u53d8\uff08\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u6216\u5143\u7ec4\uff09\uff0c\u800c\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u3002\u5b57\u5178\u7684\u7279\u70b9\u5305\u62ec\u65e0\u5e8f\u6027\u3001\u53ef\u53d8\u6027\u548c\u9ad8\u6548\u7684\u67e5\u627e\u901f\u5ea6\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u5feb\u901f\u67e5\u627e\u548c\u5b58\u50a8\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u521b\u5efa\u4e00\u4e2aPython\u5b57\u5178\uff1f<\/strong><br \/>\u521b\u5efa\u5b57\u5178\u7684\u65b9\u5f0f\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u4f7f\u7528\u5927\u62ec\u53f7 <code>{}<\/code> \u6216 <code>dict()<\/code> \u51fd\u6570\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u4f7f\u7528\u5927\u62ec\u53f7\u53ef\u4ee5\u8fd9\u6837\u8868\u793a\uff1a<code>my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25}<\/code>\u3002\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\u5219\u53ef\u4ee5\u8fd9\u6837\u521b\u5efa\uff1a<code>my_dict = dict(name=&#39;Alice&#39;, age=25)<\/code>\u3002\u65e0\u8bba\u54ea\u79cd\u65b9\u5f0f\uff0c\u5b57\u5178\u90fd\u80fd\u6709\u6548\u5730\u5b58\u50a8\u548c\u7ba1\u7406\u76f8\u5173\u6570\u636e\u3002<\/p>\n<p><strong>\u5b57\u5178\u7684\u5e38\u7528\u64cd\u4f5c\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5b57\u5178\u652f\u6301\u591a\u79cd\u64cd\u4f5c\uff0c\u5305\u62ec\u6dfb\u52a0\u3001\u66f4\u65b0\u548c\u5220\u9664\u952e\u503c\u5bf9\u3002\u53ef\u4ee5\u4f7f\u7528 <code>my_dict[&#39;key&#39;] = value<\/code> \u6765\u6dfb\u52a0\u6216\u66f4\u65b0\u5143\u7d20\uff0c\u4f7f\u7528 <code>del my_dict[&#39;key&#39;]<\/code> \u5220\u9664\u7279\u5b9a\u7684\u952e\u503c\u5bf9\u3002\u6b64\u5916\uff0c\u5b57\u5178\u8fd8\u63d0\u4f9b\u4e86\u65b9\u6cd5\u5982 <code>keys()<\/code>\u3001<code>values()<\/code> \u548c <code>items()<\/code>\uff0c\u5206\u522b\u7528\u4e8e\u83b7\u53d6\u6240\u6709\u952e\u3001\u6240\u6709\u503c\u4ee5\u53ca\u6240\u6709\u952e\u503c\u5bf9\u3002\u8fd9\u4e9b\u64cd\u4f5c\u4f7f\u5f97\u5b57\u5178\u5728\u6570\u636e\u7ba1\u7406\u4e0a\u975e\u5e38\u7075\u6d3b\u548c\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7684\u5b57\u5178\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\u6570\u636e\u7ed3\u6784\u7684\u96c6\u5408\u7c7b\u578b\u3002 \u5b57\u5178\u4e2d\u7684\u6bcf\u4e2a\u952e\u503c\u5bf9\u7531\u952e\u548c\u503c\u7ec4\u6210\uff0c\u5176\u4e2d\u952e\u662f\u552f\u4e00\u7684\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1059142,"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\/1059137"}],"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=1059137"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059137\/revisions"}],"predecessor-version":[{"id":1059145,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059137\/revisions\/1059145"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1059142"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1059137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1059137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1059137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}