{"id":1108775,"date":"2025-01-08T17:03:32","date_gmt":"2025-01-08T09:03:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108775.html"},"modified":"2025-01-08T17:03:34","modified_gmt":"2025-01-08T09:03:34","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8%e5%ad%97%e5%85%b8%e7%bc%96%e5%86%99%e9%80%9a%e8%ae%af%e5%bd%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108775.html","title":{"rendered":"python\u5982\u4f55\u7528\u5b57\u5178\u7f16\u5199\u901a\u8baf\u5f55"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072306\/af992aa0-cc8b-42f4-b0be-d164eb72304a.webp\" alt=\"python\u5982\u4f55\u7528\u5b57\u5178\u7f16\u5199\u901a\u8baf\u5f55\" \/><\/p>\n<p><p> <strong>Python\u4f7f\u7528\u5b57\u5178\u7f16\u5199\u901a\u8baf\u5f55\u7684\u65b9\u6cd5\u6709\uff1a\u521b\u5efa\u5b57\u5178\u5b58\u50a8\u8054\u7cfb\u4eba\u4fe1\u606f\u3001\u4f7f\u7528\u51fd\u6570\u6dfb\u52a0\u3001\u5220\u9664\u548c\u67e5\u627e\u8054\u7cfb\u4eba\u3001\u4fdd\u5b58\u548c\u8bfb\u53d6\u6570\u636e\u5230\u6587\u4ef6\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u521b\u5efa\u5b57\u5178\u5b58\u50a8\u8054\u7cfb\u4eba\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b57\u5178\u662f\u4e00\u79cd\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5b58\u50a8\u5173\u8054\u6570\u636e\u7684\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8054\u7cfb\u4eba\u59d3\u540d\u4f5c\u4e3a\u5b57\u5178\u7684\u952e\uff0c\u8054\u7cfb\u4eba\u8be6\u60c5\uff08\u5982\u7535\u8bdd\u53f7\u7801\u548c\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff09\u4f5c\u4e3a\u503c\u3002\u8fd9\u6837\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u901a\u8fc7\u59d3\u540d\u6765\u67e5\u627e\u5bf9\u5e94\u7684\u8054\u7cfb\u4eba\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u5b57\u5178\u7f16\u5199\u4e00\u4e2a\u7b80\u6613\u7684\u901a\u8baf\u5f55\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efa\u5b57\u5178\u5b58\u50a8\u8054\u7cfb\u4eba\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u901a\u8baf\u5f55\u65f6\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u7a7a\u7684\u5b57\u5178\uff0c\u7528\u4e8e\u5b58\u50a8\u8054\u7cfb\u4eba\u4fe1\u606f\u3002\u6bcf\u4e2a\u8054\u7cfb\u4eba\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5d4c\u5957\u5b57\u5178\u6765\u5b58\u50a8\u5176\u8be6\u7ec6\u4fe1\u606f\uff0c\u4f8b\u5982\u7535\u8bdd\u53f7\u7801\u548c\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u7a7a\u5b57\u5178<\/p>\n<p>contacts = {}<\/p>\n<h2><strong>\u6dfb\u52a0\u8054\u7cfb\u4eba\u4fe1\u606f<\/strong><\/h2>\n<p>contacts[&quot;Alice&quot;] = {&quot;phone&quot;: &quot;123-456-7890&quot;, &quot;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&quot;: &quot;alice@example.com&quot;}<\/p>\n<p>contacts[&quot;Bob&quot;] = {&quot;phone&quot;: &quot;098-765-4321&quot;, &quot;email&quot;: &quot;bob@example.com&quot;}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>contacts<\/code>\u7684\u7a7a\u5b57\u5178\uff0c\u7136\u540e\u4e3a\u4e24\u4e2a\u8054\u7cfb\u4eba\u6dfb\u52a0\u4e86\u4fe1\u606f\u3002\u6bcf\u4e2a\u8054\u7cfb\u4eba\u90fd\u662f\u4e00\u4e2a\u952e\uff0c\u5176\u503c\u662f\u4e00\u4e2a\u5305\u542b\u7535\u8bdd\u53f7\u7801\u548c\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u7684\u5d4c\u5957\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u51fd\u6570\u7ba1\u7406\u8054\u7cfb\u4eba\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u901a\u8baf\u5f55\u66f4\u52a0\u5b9e\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e9b\u51fd\u6570\u6765\u6dfb\u52a0\u3001\u5220\u9664\u548c\u67e5\u627e\u8054\u7cfb\u4eba\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>1. \u6dfb\u52a0\u8054\u7cfb\u4eba<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u6dfb\u52a0\u65b0\u7684\u8054\u7cfb\u4eba\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add_contact(contacts, name, phone, email):<\/p>\n<p>    contacts[name] = {&quot;phone&quot;: phone, &quot;email&quot;: email}<\/p>\n<p>    print(f&quot;Contact {name} added successfully.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5411\u901a\u8baf\u5f55\u4e2d\u6dfb\u52a0\u65b0\u7684\u8054\u7cfb\u4eba\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">add_contact(contacts, &quot;Charlie&quot;, &quot;321-654-0987&quot;, &quot;charlie@example.com&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5220\u9664\u8054\u7cfb\u4eba<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5220\u9664\u8054\u7cfb\u4eba\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def delete_contact(contacts, name):<\/p>\n<p>    if name in contacts:<\/p>\n<p>        del contacts[name]<\/p>\n<p>        print(f&quot;Contact {name} deleted successfully.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Contact {name} not found.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5220\u9664\u6307\u5b9a\u7684\u8054\u7cfb\u4eba\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">delete_contact(contacts, &quot;Alice&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u67e5\u627e\u8054\u7cfb\u4eba<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u67e5\u627e\u8054\u7cfb\u4eba\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_contact(contacts, name):<\/p>\n<p>    if name in contacts:<\/p>\n<p>        return contacts[name]<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Contact {name} not found.&quot;)<\/p>\n<p>        return None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u67e5\u627e\u6307\u5b9a\u8054\u7cfb\u4eba\u7684\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">contact_info = find_contact(contacts, &quot;Bob&quot;)<\/p>\n<p>if contact_info:<\/p>\n<p>    print(contact_info)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4fdd\u5b58\u548c\u8bfb\u53d6\u6570\u636e\u5230\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u901a\u8baf\u5f55\u7684\u6570\u636e\u6301\u4e45\u5316\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b57\u5178\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u5e76\u5728\u9700\u8981\u65f6\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><h4>1. \u4fdd\u5b58\u6570\u636e\u5230\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>json<\/code>\u6a21\u5757\u5c06\u5b57\u5178\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>def save_contacts_to_file(contacts, filename):<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        json.dump(contacts, file)<\/p>\n<p>    print(f&quot;Contacts saved to {filename}.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u901a\u8baf\u5f55\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">save_contacts_to_file(contacts, &quot;contacts.json&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u8054\u7cfb\u4eba\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def load_contacts_from_file(filename):<\/p>\n<p>    try:<\/p>\n<p>        with open(filename, &#39;r&#39;) as file:<\/p>\n<p>            contacts = json.load(file)<\/p>\n<p>        print(f&quot;Contacts loaded from {filename}.&quot;)<\/p>\n<p>        return contacts<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;File {filename} not found.&quot;)<\/p>\n<p>        return {}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u6587\u4ef6\u4e2d\u52a0\u8f7d\u901a\u8baf\u5f55\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">contacts = load_contacts_from_file(&quot;contacts.json&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u5c55\u793a\u4e0a\u8ff0\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5305\u542b\u6dfb\u52a0\u3001\u5220\u9664\u3001\u67e5\u627e\u3001\u4fdd\u5b58\u548c\u8bfb\u53d6\u8054\u7cfb\u4eba\u4fe1\u606f\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>def add_contact(contacts, name, phone, email):<\/p>\n<p>    contacts[name] = {&quot;phone&quot;: phone, &quot;email&quot;: email}<\/p>\n<p>    print(f&quot;Contact {name} added successfully.&quot;)<\/p>\n<p>def delete_contact(contacts, name):<\/p>\n<p>    if name in contacts:<\/p>\n<p>        del contacts[name]<\/p>\n<p>        print(f&quot;Contact {name} deleted successfully.&quot;)<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Contact {name} not found.&quot;)<\/p>\n<p>def find_contact(contacts, name):<\/p>\n<p>    if name in contacts:<\/p>\n<p>        return contacts[name]<\/p>\n<p>    else:<\/p>\n<p>        print(f&quot;Contact {name} not found.&quot;)<\/p>\n<p>        return None<\/p>\n<p>def save_contacts_to_file(contacts, filename):<\/p>\n<p>    with open(filename, &#39;w&#39;) as file:<\/p>\n<p>        json.dump(contacts, file)<\/p>\n<p>    print(f&quot;Contacts saved to {filename}.&quot;)<\/p>\n<p>def load_contacts_from_file(filename):<\/p>\n<p>    try:<\/p>\n<p>        with open(filename, &#39;r&#39;) as file:<\/p>\n<p>            contacts = json.load(file)<\/p>\n<p>        print(f&quot;Contacts loaded from {filename}.&quot;)<\/p>\n<p>        return contacts<\/p>\n<p>    except FileNotFoundError:<\/p>\n<p>        print(f&quot;File {filename} not found.&quot;)<\/p>\n<p>        return {}<\/p>\n<h2><strong>\u4e3b\u7a0b\u5e8f<\/strong><\/h2>\n<p>contacts = load_contacts_from_file(&quot;contacts.json&quot;)<\/p>\n<p>while True:<\/p>\n<p>    print(&quot;\\n1. Add Contact&quot;)<\/p>\n<p>    print(&quot;2. Delete Contact&quot;)<\/p>\n<p>    print(&quot;3. Find Contact&quot;)<\/p>\n<p>    print(&quot;4. Save Contacts&quot;)<\/p>\n<p>    print(&quot;5. Load Contacts&quot;)<\/p>\n<p>    print(&quot;6. Quit&quot;)<\/p>\n<p>    choice = input(&quot;Enter your choice: &quot;)<\/p>\n<p>    if choice == &quot;1&quot;:<\/p>\n<p>        name = input(&quot;Enter name: &quot;)<\/p>\n<p>        phone = input(&quot;Enter phone number: &quot;)<\/p>\n<p>        email = input(&quot;Enter email address: &quot;)<\/p>\n<p>        add_contact(contacts, name, phone, email)<\/p>\n<p>    elif choice == &quot;2&quot;:<\/p>\n<p>        name = input(&quot;Enter name to delete: &quot;)<\/p>\n<p>        delete_contact(contacts, name)<\/p>\n<p>    elif choice == &quot;3&quot;:<\/p>\n<p>        name = input(&quot;Enter name to find: &quot;)<\/p>\n<p>        contact_info = find_contact(contacts, name)<\/p>\n<p>        if contact_info:<\/p>\n<p>            print(contact_info)<\/p>\n<p>    elif choice == &quot;4&quot;:<\/p>\n<p>        save_contacts_to_file(contacts, &quot;contacts.json&quot;)<\/p>\n<p>    elif choice == &quot;5&quot;:<\/p>\n<p>        contacts = load_contacts_from_file(&quot;contacts.json&quot;)<\/p>\n<p>    elif choice == &quot;6&quot;:<\/p>\n<p>        break<\/p>\n<p>    else:<\/p>\n<p>        print(&quot;Invalid choice. Please try again.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u6613\u7684\u901a\u8baf\u5f55\u7cfb\u7edf\uff0c\u5305\u542b\u6dfb\u52a0\u3001\u5220\u9664\u3001\u67e5\u627e\u3001\u4fdd\u5b58\u548c\u8bfb\u53d6\u8054\u7cfb\u4eba\u4fe1\u606f\u7684\u529f\u80fd\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u7ba1\u7406\u8054\u7cfb\u4eba\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\uff0c\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528Python\u5b57\u5178\u7f16\u5199\u4e00\u4e2a\u7b80\u6613\u7684\u901a\u8baf\u5f55\u7cfb\u7edf\u3002\u6211\u4eec\u9996\u5148\u4ecb\u7ecd\u4e86\u5982\u4f55\u521b\u5efa\u5b57\u5178\u5b58\u50a8\u8054\u7cfb\u4eba\u4fe1\u606f\uff0c\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e9b\u51fd\u6570\u6765\u6dfb\u52a0\u3001\u5220\u9664\u548c\u67e5\u627e\u8054\u7cfb\u4eba\u3002\u63a5\u7740\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u901a\u8baf\u5f55\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u548c\u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\u3002\u6700\u540e\uff0c\u6211\u4eec\u7f16\u5199\u4e86\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u6765\u5c55\u793a\u4e0a\u8ff0\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5b57\u5178\u7f16\u5199\u901a\u8baf\u5f55\u7cfb\u7edf\u4e0d\u4ec5\u7b80\u5355\u6613\u7528\uff0c\u800c\u4e14\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u6269\u5c55\u548c\u4fee\u6539\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u901a\u8baf\u5f55\u5b57\u5178\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u901a\u8baf\u5f55\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528\u59d3\u540d\u4f5c\u4e3a\u952e\uff0c\u7535\u8bdd\u53f7\u7801\u4f5c\u4e3a\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">contacts = {\n    &quot;Alice&quot;: &quot;123-456-7890&quot;,\n    &quot;Bob&quot;: &quot;987-654-3210&quot;,\n    &quot;Charlie&quot;: &quot;555-555-5555&quot;\n}\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u5b57\u5178\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbf\u95ee\u548c\u7ba1\u7406\u8054\u7cfb\u4eba\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u6dfb\u52a0\u3001\u5220\u9664\u6216\u66f4\u65b0\u901a\u8baf\u5f55\u4e2d\u7684\u8054\u7cfb\u4eba\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u5178\u7684\u57fa\u672c\u64cd\u4f5c\u6765\u6dfb\u52a0\u3001\u5220\u9664\u6216\u66f4\u65b0\u8054\u7cfb\u4eba\u3002\u4f7f\u7528<code>contacts[&quot;\u65b0\u8054\u7cfb\u4eba&quot;] = &quot;\u65b0\u53f7\u7801&quot;<\/code>\u6765\u6dfb\u52a0\uff0c\u4f7f\u7528<code>del contacts[&quot;\u8054\u7cfb\u4eba\u540d&quot;]<\/code>\u6765\u5220\u9664\uff0c\u66f4\u65b0\u5219\u76f4\u63a5\u8d4b\u503c\u7ed9\u73b0\u6709\u7684\u952e\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\"># \u6dfb\u52a0\u8054\u7cfb\u4eba\ncontacts[&quot;David&quot;] = &quot;111-222-3333&quot;\n\n# \u5220\u9664\u8054\u7cfb\u4eba\ndel contacts[&quot;Alice&quot;]\n\n# \u66f4\u65b0\u8054\u7cfb\u4eba\u7684\u7535\u8bdd\u53f7\u7801\ncontacts[&quot;Bob&quot;] = &quot;000-111-2222&quot;\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u904d\u5386\u901a\u8baf\u5f55\u5b57\u5178\u4ee5\u663e\u793a\u6240\u6709\u8054\u7cfb\u4eba\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u904d\u5386\u5b57\u5178\uff0c\u6253\u5370\u51fa\u6240\u6709\u8054\u7cfb\u4eba\u7684\u59d3\u540d\u548c\u7535\u8bdd\u53f7\u7801\u3002\u4f7f\u7528<code>items()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u540c\u65f6\u83b7\u53d6\u952e\u548c\u503c\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">for name, phone in contacts.items():\n    print(f&quot;\u8054\u7cfb\u4eba: {name}, \u7535\u8bdd\u53f7\u7801: {phone}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u6e05\u6670\u5730\u5c55\u793a\u901a\u8baf\u5f55\u4e2d\u7684\u6240\u6709\u4fe1\u606f\uff0c\u65b9\u4fbf\u7528\u6237\u67e5\u770b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4f7f\u7528\u5b57\u5178\u7f16\u5199\u901a\u8baf\u5f55\u7684\u65b9\u6cd5\u6709\uff1a\u521b\u5efa\u5b57\u5178\u5b58\u50a8\u8054\u7cfb\u4eba\u4fe1\u606f\u3001\u4f7f\u7528\u51fd\u6570\u6dfb\u52a0\u3001\u5220\u9664\u548c\u67e5\u627e\u8054\u7cfb\u4eba\u3001\u4fdd\u5b58\u548c\u8bfb\u53d6\u6570\u636e [&hellip;]","protected":false},"author":3,"featured_media":1108781,"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\/1108775"}],"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=1108775"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108775\/revisions"}],"predecessor-version":[{"id":1108783,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108775\/revisions\/1108783"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108781"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}