{"id":2921,"date":"2024-12-15T19:31:14","date_gmt":"2024-12-15T11:31:14","guid":{"rendered":"http:\/\/viplao.com\/?p=2921"},"modified":"2024-12-15T19:31:18","modified_gmt":"2024-12-15T11:31:18","slug":"python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/","title":{"rendered":"PYTHON\u57fa\u7840\u6280\u80fd \u2013\u00a0Python\u5b57\u5178\u4e0e\u96c6\u5408\u768415\u4e2a\u9ad8\u7ea7\u64cd\u4f5c\u4e0e\u4f18\u5316\u5efa\u8bae"},"content":{"rendered":"\n<p>\u4eca\u5929\u6211\u4eec\u5c06\u4e00\u8d77\u63a2\u7d22Python\u4e2d\u6700\u5f3a\u5927\u4e14\u9ad8\u6548\u7684\u4e24\u4e2a\u6570\u636e\u7ed3\u6784\u2014\u2014\u5b57\u5178\u548c\u96c6\u5408\uff0c\u5b83\u4eec\u5728\u6570\u636e\u5904\u7406\u4e2d\u626e\u6f14\u7740\u81f3\u5173\u91cd\u8981\u7684\u89d2\u8272\u3002\u901a\u8fc7\u8fd915\u4e2a\u9ad8\u7ea7\u64cd\u4f5c\u548c\u4f18\u5316\u6280\u5de7\uff0c\u4f60\u4eec\u5c06\u5b66\u4f1a\u5982\u4f55\u50cf\u4e13\u5bb6\u4e00\u6837\u9a7e\u9a6d\u5b83\u4eec\uff0c\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u9ad8\u6548\u3002<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_71 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">\u6587\u7ae0\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#1_%E5%AD%97%E5%85%B8%E6%8E%A8%E5%AF%BC%E5%BC%8F%EF%BC%88Dictionary_Comprehensions%EF%BC%89\" title=\"1. \u5b57\u5178\u63a8\u5bfc\u5f0f\uff08Dictionary Comprehensions\uff09\">1. \u5b57\u5178\u63a8\u5bfc\u5f0f\uff08Dictionary Comprehensions\uff09<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#2_%E5%90%88%E5%B9%B6%E5%AD%97%E5%85%B8%EF%BC%88Using_update%EF%BC%89\" title=\"2. \u5408\u5e76\u5b57\u5178\uff08Using update()\uff09\">2. \u5408\u5e76\u5b57\u5178\uff08Using update()\uff09<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#3_%E9%9B%86%E5%90%88%E7%9A%84%E5%9F%BA%E6%9C%AC%E8%BF%90%E7%AE%97\" title=\"3. \u96c6\u5408\u7684\u57fa\u672c\u8fd0\u7b97\">3. \u96c6\u5408\u7684\u57fa\u672c\u8fd0\u7b97<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#4_%E5%AD%97%E5%85%B8%E7%9A%84get%E6%96%B9%E6%B3%95\" title=\"4. \u5b57\u5178\u7684get()\u65b9\u6cd5\">4. \u5b57\u5178\u7684get()\u65b9\u6cd5<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#5_%E9%9B%86%E5%90%88%E6%8E%A8%E5%AF%BC%E5%BC%8F\" title=\"5. \u96c6\u5408\u63a8\u5bfc\u5f0f\">5. \u96c6\u5408\u63a8\u5bfc\u5f0f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#6_%E5%AD%97%E5%85%B8%E7%9A%84items%E9%81%8D%E5%8E%86\" title=\"6. \u5b57\u5178\u7684items()\u904d\u5386\">6. \u5b57\u5178\u7684items()\u904d\u5386<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#7_%E9%9B%86%E5%90%88%E7%9A%84%E4%BA%92%E6%96%A5%E6%80%A7%E6%A3%80%E6%9F%A5%EF%BC%88isdisjoint%EF%BC%89\" title=\"7. \u96c6\u5408\u7684\u4e92\u65a5\u6027\u68c0\u67e5\uff08isdisjoint()\uff09\">7. \u96c6\u5408\u7684\u4e92\u65a5\u6027\u68c0\u67e5\uff08isdisjoint()\uff09<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#8_%E5%AD%97%E5%85%B8%E7%9A%84pop%E6%96%B9%E6%B3%95\" title=\"8. \u5b57\u5178\u7684pop()\u65b9\u6cd5\">8. \u5b57\u5178\u7684pop()\u65b9\u6cd5<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#9_%E9%9B%86%E5%90%88%E7%9A%84add%E5%92%8Cremove\" title=\"9. \u96c6\u5408\u7684add()\u548cremove()\">9. \u96c6\u5408\u7684add()\u548cremove()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#10_%E5%AD%97%E5%85%B8%E7%9A%84fromkeys\" title=\"10. \u5b57\u5178\u7684fromkeys()\">10. \u5b57\u5178\u7684fromkeys()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#11_%E9%9B%86%E5%90%88%E7%9A%84copy\" title=\"11. \u96c6\u5408\u7684copy()\">11. \u96c6\u5408\u7684copy()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#12_%E5%AD%97%E5%85%B8%E7%9A%84defaultdict\" title=\"12. \u5b57\u5178\u7684defaultdict\">12. \u5b57\u5178\u7684defaultdict<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#13_%E9%9B%86%E5%90%88%E7%9A%84frozenset\" title=\"13. \u96c6\u5408\u7684frozenset()\">13. \u96c6\u5408\u7684frozenset()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#14_%E5%AD%97%E5%85%B8%E8%A7%86%E5%9B%BE%E5%AF%B9%E8%B1%A1\" title=\"14. \u5b57\u5178\u89c6\u56fe\u5bf9\u8c61\">14. \u5b57\u5178\u89c6\u56fe\u5bf9\u8c61<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#15_%E4%BC%98%E5%8C%96%E5%BB%BA%E8%AE%AE%EF%BC%9A%E5%88%A9%E7%94%A8%E5%AD%97%E5%85%B8%E5%92%8C%E9%9B%86%E5%90%88%E7%9A%84%E7%89%B9%E6%80%A7\" title=\"15. \u4f18\u5316\u5efa\u8bae\uff1a\u5229\u7528\u5b57\u5178\u548c\u96c6\u5408\u7684\u7279\u6027\">15. \u4f18\u5316\u5efa\u8bae\uff1a\u5229\u7528\u5b57\u5178\u548c\u96c6\u5408\u7684\u7279\u6027<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#%E5%AE%9E%E6%88%98%E6%A1%88%E4%BE%8B%E5%88%86%E6%9E%90\" title=\"\u5b9e\u6218\u6848\u4f8b\u5206\u6790\">\u5b9e\u6218\u6848\u4f8b\u5206\u6790<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#16_%E5%AD%97%E5%85%B8%E7%9A%84%E5%90%88%E5%B9%B6%E4%B8%8E%E6%9B%B4%E6%96%B0%E6%8A%80%E5%B7%A7\" title=\"16. \u5b57\u5178\u7684\u5408\u5e76\u4e0e\u66f4\u65b0\u6280\u5de7\">16. \u5b57\u5178\u7684\u5408\u5e76\u4e0e\u66f4\u65b0\u6280\u5de7<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#17_%E9%9B%86%E5%90%88%E7%9A%84%E9%AB%98%E7%BA%A7%E8%BF%90%E7%AE%97%E5%AE%9E%E6%88%98\" title=\"17. \u96c6\u5408\u7684\u9ad8\u7ea7\u8fd0\u7b97\u5b9e\u6218\">17. \u96c6\u5408\u7684\u9ad8\u7ea7\u8fd0\u7b97\u5b9e\u6218<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#18_%E5%AD%97%E5%85%B8%E6%98%A0%E5%B0%84%E5%87%BD%E6%95%B0%E5%80%BC\" title=\"18. \u5b57\u5178\u6620\u5c04\u51fd\u6570\u503c\">18. \u5b57\u5178\u6620\u5c04\u51fd\u6570\u503c<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#19_%E9%9B%86%E5%90%88%E7%9A%84%E4%BA%A4%E9%9B%86%E7%94%A8%E4%BA%8E%E8%BF%87%E6%BB%A4\" title=\"19. \u96c6\u5408\u7684\u4ea4\u96c6\u7528\u4e8e\u8fc7\u6ee4\">19. \u96c6\u5408\u7684\u4ea4\u96c6\u7528\u4e8e\u8fc7\u6ee4<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/#20_%E5%AD%97%E5%85%B8%E7%9A%84%E6%B7%B1%E6%8B%B7%E8%B4%9D%E4%B8%8E%E6%B5%85%E6%8B%B7%E8%B4%9D%E7%90%86%E8%A7%A3\" title=\"20. \u5b57\u5178\u7684\u6df1\u62f7\u8d1d\u4e0e\u6d45\u62f7\u8d1d\u7406\u89e3\">20. \u5b57\u5178\u7684\u6df1\u62f7\u8d1d\u4e0e\u6d45\u62f7\u8d1d\u7406\u89e3<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_%E5%AD%97%E5%85%B8%E6%8E%A8%E5%AF%BC%E5%BC%8F%EF%BC%88Dictionary_Comprehensions%EF%BC%89\"><\/span><strong>1. \u5b57\u5178\u63a8\u5bfc\u5f0f\uff08Dictionary Comprehensions\uff09<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u662f\u521b\u5efa\u5b57\u5178\u7684\u5feb\u6377\u65b9\u5f0f\u3002\u6bd4\u5982\uff0c\u5c06\u4e00\u4e2a\u5217\u8868\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5176\u5e73\u65b9\u4f5c\u4e3a\u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>squares&nbsp;=&nbsp;{num:&nbsp;num**2&nbsp;for&nbsp;num&nbsp;in&nbsp;range(1,&nbsp;6)}<br>print(squares)&nbsp;&nbsp;<em>#&nbsp;\u8f93\u51fa:&nbsp;{1:&nbsp;1,&nbsp;2:&nbsp;4,&nbsp;3:&nbsp;9,&nbsp;4:&nbsp;16,&nbsp;5:&nbsp;25}<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_%E5%90%88%E5%B9%B6%E5%AD%97%E5%85%B8%EF%BC%88Using_update%EF%BC%89\"><\/span><strong>2. \u5408\u5e76\u5b57\u5178\uff08Using <code>update()<\/code>\uff09<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5408\u5e76\u4e24\u4e2a\u5b57\u5178\u65f6\uff0c<code>update()<\/code>\u65b9\u6cd5\u975e\u5e38\u6709\u7528\uff0c\u5b83\u4f1a\u5c06\u4e00\u4e2a\u5b57\u5178\u7684\u952e\u503c\u5bf9\u6dfb\u52a0\u5230\u53e6\u4e00\u4e2a\u5b57\u5178\u4e2d\uff0c\u5982\u679c\u6709\u91cd\u590d\u7684\u952e\uff0c\u5219\u540e\u8005\u7684\u503c\u4f1a\u8986\u76d6\u524d\u8005\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dict1&nbsp;=&nbsp;{'a':&nbsp;1,&nbsp;'b':&nbsp;2}<br>dict2&nbsp;=&nbsp;{'b':&nbsp;3,&nbsp;'c':&nbsp;4}<br>dict1.update(dict2)<br>print(dict1)&nbsp;&nbsp;<em>#&nbsp;\u8f93\u51fa:&nbsp;{'a':&nbsp;1,&nbsp;'b':&nbsp;3,&nbsp;'c':&nbsp;4}<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_%E9%9B%86%E5%90%88%E7%9A%84%E5%9F%BA%E6%9C%AC%E8%BF%90%E7%AE%97\"><\/span><strong>3. \u96c6\u5408\u7684\u57fa\u672c\u8fd0\u7b97<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u96c6\u5408\u652f\u6301\u5e76\u96c6(<code>|<\/code>)\u3001\u4ea4\u96c6(<code>&amp;<\/code>)\u3001\u5dee\u96c6(<code>-<\/code>)\u548c\u5bf9\u79f0\u5dee\u96c6(<code>^<\/code>)\u7b49\u64cd\u4f5c\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u6570\u636e\u7684\u53bb\u91cd\u548c\u6bd4\u8f83\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set1&nbsp;=&nbsp;{1,&nbsp;2,&nbsp;3}<br>set2&nbsp;=&nbsp;{2,&nbsp;3,&nbsp;4}<br>print(set1&nbsp;|&nbsp;set2)&nbsp;&nbsp;<em>#&nbsp;\u5e76\u96c6:&nbsp;{1,&nbsp;2,&nbsp;3,&nbsp;4}<\/em><br>print(set1&nbsp;&amp;&nbsp;set2)&nbsp;&nbsp;<em>#&nbsp;\u4ea4\u96c6:&nbsp;{2,&nbsp;3}<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_%E5%AD%97%E5%85%B8%E7%9A%84get%E6%96%B9%E6%B3%95\"><\/span><strong>4. \u5b57\u5178\u7684get()\u65b9\u6cd5<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u907f\u514dKeyError\u7684\u597d\u65b9\u6cd5\uff0c\u5f53\u952e\u4e0d\u5b58\u5728\u65f6\uff0c\u53ef\u4ee5\u8fd4\u56de\u9ed8\u8ba4\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_dict&nbsp;=&nbsp;{'name':&nbsp;'Alice'}<br>print(my_dict.get('age',&nbsp;'Not&nbsp;Available'))&nbsp;&nbsp;<em>#&nbsp;\u8f93\u51fa:&nbsp;Not&nbsp;Available<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_%E9%9B%86%E5%90%88%E6%8E%A8%E5%AF%BC%E5%BC%8F\"><\/span><strong>5. \u96c6\u5408\u63a8\u5bfc\u5f0f<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u7c7b\u4f3c\u4e8e\u5b57\u5178\u63a8\u5bfc\uff0c\u7528\u4e8e\u5feb\u901f\u521b\u5efa\u96c6\u5408\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>even_numbers&nbsp;=&nbsp;{num&nbsp;for&nbsp;num&nbsp;in&nbsp;range(1,&nbsp;11)&nbsp;if&nbsp;num&nbsp;%&nbsp;2&nbsp;==&nbsp;0}<br>print(even_numbers)&nbsp;&nbsp;<em>#&nbsp;\u8f93\u51fa:&nbsp;{2,&nbsp;4,&nbsp;6,&nbsp;8,&nbsp;10}<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6_%E5%AD%97%E5%85%B8%E7%9A%84items%E9%81%8D%E5%8E%86\"><\/span><strong>6. \u5b57\u5178\u7684items()\u904d\u5386<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u904d\u5386\u5b57\u5178\u7684\u952e\u503c\u5bf9\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u540c\u65f6\u5904\u7406\u952e\u548c\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for&nbsp;key,&nbsp;value&nbsp;in&nbsp;squares.items():<br>&nbsp;&nbsp;&nbsp;&nbsp;print(f\"{key}:&nbsp;{value}\")<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7_%E9%9B%86%E5%90%88%E7%9A%84%E4%BA%92%E6%96%A5%E6%80%A7%E6%A3%80%E6%9F%A5%EF%BC%88isdisjoint%EF%BC%89\"><\/span><strong>7. \u96c6\u5408\u7684\u4e92\u65a5\u6027\u68c0\u67e5\uff08isdisjoint()\uff09<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5224\u65ad\u4e24\u4e2a\u96c6\u5408\u662f\u5426\u6709\u4ea4\u96c6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set1&nbsp;=&nbsp;{1,&nbsp;2,&nbsp;3}<br>set2&nbsp;=&nbsp;{4,&nbsp;5,&nbsp;6}<br>print(set1.isdisjoint(set2))&nbsp;&nbsp;<em>#&nbsp;\u8f93\u51fa:&nbsp;True<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"8_%E5%AD%97%E5%85%B8%E7%9A%84pop%E6%96%B9%E6%B3%95\"><\/span><strong>8. \u5b57\u5178\u7684pop()\u65b9\u6cd5<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u79fb\u9664\u6307\u5b9a\u952e\u7684\u9879\uff0c\u5e76\u8fd4\u56de\u5176\u503c\uff0c\u82e5\u952e\u4e0d\u5b58\u5728\uff0c\u53ef\u8bbe\u7f6e\u9ed8\u8ba4\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>value&nbsp;=&nbsp;my_dict.pop('name',&nbsp;'Default')<br>print(value)&nbsp;&nbsp;<em>#&nbsp;\u8f93\u51fa:&nbsp;Alice<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"9_%E9%9B%86%E5%90%88%E7%9A%84add%E5%92%8Cremove\"><\/span><strong>9. \u96c6\u5408\u7684add()\u548cremove()<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u52a8\u6001\u5730\u6dfb\u52a0\u6216\u79fb\u9664\u5143\u7d20\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set1.add(5)<br>set1.remove(3)<br>print(set1)&nbsp;&nbsp;<em>#&nbsp;\u5047\u8bbe\u521d\u59cb\u4e3a{1,&nbsp;2,&nbsp;3,&nbsp;4},&nbsp;\u8f93\u51fa\u5c06\u662f{1,&nbsp;2,&nbsp;4,&nbsp;5}<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"10_%E5%AD%97%E5%85%B8%E7%9A%84fromkeys\"><\/span><strong>10. \u5b57\u5178\u7684fromkeys()<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5feb\u901f\u751f\u6210\u4e00\u4e2a\u5b57\u5178\uff0c\u6240\u6709\u952e\u90fd\u6709\u76f8\u540c\u7684\u521d\u59cb\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>keys&nbsp;=&nbsp;&#91;'a',&nbsp;'b',&nbsp;'c']<br>values&nbsp;=&nbsp;0<br>my_dict&nbsp;=&nbsp;dict.fromkeys(keys,&nbsp;values)<br>print(my_dict)&nbsp;&nbsp;<em>#&nbsp;\u8f93\u51fa:&nbsp;{'a':&nbsp;0,&nbsp;'b':&nbsp;0,&nbsp;'c':&nbsp;0}<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"11_%E9%9B%86%E5%90%88%E7%9A%84copy\"><\/span><strong>11. \u96c6\u5408\u7684copy()<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u521b\u5efa\u96c6\u5408\u7684\u6d45\u590d\u5236\uff0c\u9002\u7528\u4e8e\u4e0d\u5e0c\u671b\u539f\u96c6\u5408\u6539\u53d8\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>original_set&nbsp;=&nbsp;{1,&nbsp;2,&nbsp;3}<br>copied_set&nbsp;=&nbsp;original_set.copy()<br>copied_set.add(4)<br>print(original_set)&nbsp;&nbsp;<em>#&nbsp;\u8f93\u51fa:&nbsp;{1,&nbsp;2,&nbsp;3}<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"12_%E5%AD%97%E5%85%B8%E7%9A%84defaultdict\"><\/span><strong>12. \u5b57\u5178\u7684defaultdict<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u907f\u514dKeyError\uff0c\u81ea\u52a8\u4e3a\u4e0d\u5b58\u5728\u7684\u952e\u5206\u914d\u9ed8\u8ba4\u503c\u7c7b\u578b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from&nbsp;collections&nbsp;import&nbsp;defaultdict<br>d&nbsp;=&nbsp;defaultdict(int)&nbsp;&nbsp;<em>#&nbsp;\u9ed8\u8ba4\u503c\u4e3a0<\/em><br>print(d&#91;'new_key'])&nbsp;&nbsp;<em>#&nbsp;\u8f93\u51fa:&nbsp;0<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"13_%E9%9B%86%E5%90%88%E7%9A%84frozenset\"><\/span><strong>13. \u96c6\u5408\u7684frozenset()<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u4e0d\u53ef\u53d8\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u4f7f\u7528<code>frozenset<\/code>\uff0c\u8fd9\u5bf9\u4e8e\u7528\u4f5c\u5b57\u5178\u952e\u6216\u4f5c\u4e3a\u96c6\u5408\u7684\u5143\u7d20\u975e\u5e38\u6709\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>frozen_set&nbsp;=&nbsp;frozenset(&#91;1,&nbsp;2,&nbsp;3])<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"14_%E5%AD%97%E5%85%B8%E8%A7%86%E5%9B%BE%E5%AF%B9%E8%B1%A1\"><\/span><strong>14. \u5b57\u5178\u89c6\u56fe\u5bf9\u8c61<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><code>keys()<\/code>, <code>values()<\/code>, \u548c <code>items()<\/code> \u8fd4\u56de\u7684\u662f\u89c6\u56fe\u5bf9\u8c61\uff0c\u8fd9\u610f\u5473\u7740\u5b83\u4eec\u53cd\u6620\u4e86\u5b57\u5178\u7684\u53d8\u5316\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>view&nbsp;=&nbsp;my_dict.keys()<br>my_dict&#91;'age']&nbsp;=&nbsp;25<br>print(view)&nbsp;&nbsp;<em>#&nbsp;\u8f93\u51fa\u5c06\u5305\u542b\u65b0\u6dfb\u52a0\u7684\u952e'age'<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"15_%E4%BC%98%E5%8C%96%E5%BB%BA%E8%AE%AE%EF%BC%9A%E5%88%A9%E7%94%A8%E5%AD%97%E5%85%B8%E5%92%8C%E9%9B%86%E5%90%88%E7%9A%84%E7%89%B9%E6%80%A7\"><\/span><strong>15. \u4f18\u5316\u5efa\u8bae\uff1a\u5229\u7528\u5b57\u5178\u548c\u96c6\u5408\u7684\u7279\u6027<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul>\n<li>\u5728\u9700\u8981\u5feb\u901f\u67e5\u627e\u548c\u53bb\u91cd\u7684\u573a\u666f\u4e0b\u4f18\u5148\u8003\u8651\u4f7f\u7528\u5b57\u5178\u548c\u96c6\u5408\u3002<\/li>\n\n\n\n<li>\u5bf9\u4e8e\u9891\u7e41\u7684\u6210\u5458\u8d44\u683c\u6d4b\u8bd5(<code>in<\/code>\u64cd\u4f5c)\uff0c\u96c6\u5408\u6bd4\u5217\u8868\u5feb\u5f97\u591a\u3002<\/li>\n\n\n\n<li>\u5229\u7528\u5b57\u5178\u63a8\u5bfc\u548c\u96c6\u5408\u63a8\u5bfc\u51cf\u5c11\u4ee3\u7801\u91cf\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u9ad8\u7ea7\u64cd\u4f5c\u548c\u4f18\u5316\u7b56\u7565\uff0c\u4f60\u7684Python\u7f16\u7a0b\u80fd\u529b\u5c06\u5f97\u5230\u663e\u8457\u63d0\u5347\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AE%9E%E6%88%98%E6%A1%88%E4%BE%8B%E5%88%86%E6%9E%90\"><\/span><strong>\u5b9e\u6218\u6848\u4f8b\u5206\u6790<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"16_%E5%AD%97%E5%85%B8%E7%9A%84%E5%90%88%E5%B9%B6%E4%B8%8E%E6%9B%B4%E6%96%B0%E6%8A%80%E5%B7%A7\"><\/span><strong>16. \u5b57\u5178\u7684\u5408\u5e76\u4e0e\u66f4\u65b0\u6280\u5de7<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u6709\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5408\u5e76\u591a\u4e2a\u5b57\u5178\uff0c\u7279\u522b\u662f\u5f53\u5904\u7406\u914d\u7f6e\u6587\u4ef6\u6216\u53c2\u6570\u4f20\u9012\u65f6\u3002Python 3.5\u53ca\u4ee5\u4e0a\u7248\u672c\u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u7279\u6027\uff0c\u4f7f\u5f97\u8fd9\u4e2a\u8fc7\u7a0b\u66f4\u52a0\u4f18\u96c5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dict1&nbsp;=&nbsp;{'x':&nbsp;1,&nbsp;'y':&nbsp;2}<br>dict2&nbsp;=&nbsp;{'y':&nbsp;3,&nbsp;'z':&nbsp;4}<br>merged_dict&nbsp;=&nbsp;{**dict1,&nbsp;**dict2}&nbsp;&nbsp;<em>#&nbsp;Python&nbsp;3.5+&nbsp;\u7684\u5b57\u5178\u89e3\u6784<\/em><br>print(merged_dict)&nbsp;&nbsp;<em>#&nbsp;\u8f93\u51fa:&nbsp;{'x':&nbsp;1,&nbsp;'y':&nbsp;3,&nbsp;'z':&nbsp;4}<\/em><\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\uff0c<code>**<\/code>\u64cd\u4f5c\u7b26\u5141\u8bb8\u6211\u4eec\u201c\u5c55\u5f00\u201d\u5b57\u5178\uff0c\u4ece\u800c\u76f4\u63a5\u5408\u5e76\u5b83\u4eec\uff0c\u9047\u5230\u76f8\u540c\u952e\u65f6\uff0c\u540e\u9762\u7684\u5b57\u5178\u4e2d\u7684\u503c\u4f1a\u8986\u76d6\u524d\u9762\u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"17_%E9%9B%86%E5%90%88%E7%9A%84%E9%AB%98%E7%BA%A7%E8%BF%90%E7%AE%97%E5%AE%9E%E6%88%98\"><\/span><strong>17. \u96c6\u5408\u7684\u9ad8\u7ea7\u8fd0\u7b97\u5b9e\u6218<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u96c6\u5408\u9664\u4e86\u57fa\u672c\u8fd0\u7b97\u5916\uff0c\u8fd8\u53ef\u4ee5\u7528\u6765\u89e3\u51b3\u590d\u6742\u7684\u903b\u8f91\u95ee\u9898\uff0c\u4f8b\u5982\u627e\u51fa\u4e24\u4e2a\u5217\u8868\u7684\u552f\u4e00\u5143\u7d20\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list1&nbsp;=&nbsp;&#91;1,&nbsp;2,&nbsp;3,&nbsp;4]<br>list2&nbsp;=&nbsp;&#91;3,&nbsp;4,&nbsp;5,&nbsp;6]<br>unique_elements&nbsp;=&nbsp;set(list1)&nbsp;^&nbsp;set(list2)&nbsp;&nbsp;<em>#&nbsp;\u5bf9\u79f0\u5dee\u96c6<\/em><br>print(unique_elements)&nbsp;&nbsp;<em>#&nbsp;\u8f93\u51fa:&nbsp;{1,&nbsp;2,&nbsp;5,&nbsp;6}<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"18_%E5%AD%97%E5%85%B8%E6%98%A0%E5%B0%84%E5%87%BD%E6%95%B0%E5%80%BC\"><\/span><strong>18. \u5b57\u5178\u6620\u5c04\u51fd\u6570\u503c<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u5178\uff0c\u5e76\u60f3\u5bf9\u6bcf\u4e2a\u503c\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u7ed3\u5408\u5b57\u5178\u63a8\u5bfc\u5f0f\u548c\u51fd\u6570\u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>prices&nbsp;=&nbsp;{'apple':&nbsp;1.0,&nbsp;'banana':&nbsp;2.0}<br>discounted_prices&nbsp;=&nbsp;{item:&nbsp;price&nbsp;*&nbsp;0.9&nbsp;for&nbsp;item,&nbsp;price&nbsp;in&nbsp;prices.items()}<br>print(discounted_prices)&nbsp;&nbsp;<em>#&nbsp;\u8f93\u51fa:&nbsp;{'apple':&nbsp;0.9,&nbsp;'banana':&nbsp;1.8}<\/em><\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\uff0c\u6211\u4eec\u5bf9\u6bcf\u4e2a\u4ef7\u683c\u5e94\u7528\u4e8690%\u7684\u6298\u6263\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"19_%E9%9B%86%E5%90%88%E7%9A%84%E4%BA%A4%E9%9B%86%E7%94%A8%E4%BA%8E%E8%BF%87%E6%BB%A4\"><\/span><strong>19. \u96c6\u5408\u7684\u4ea4\u96c6\u7528\u4e8e\u8fc7\u6ee4<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5047\u8bbe\u4f60\u6709\u4e24\u4e2a\u5217\u8868\uff0c\u60f3\u8981\u627e\u51fa\u540c\u65f6\u5b58\u5728\u4e8e\u4e24\u4e2a\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u96c6\u5408\u7684\u4ea4\u96c6\u529f\u80fd\u5c31\u975e\u5e38\u5408\u9002\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>list_a&nbsp;=&nbsp;&#91;1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5]<br>list_b&nbsp;=&nbsp;&#91;4,&nbsp;5,&nbsp;6,&nbsp;7,&nbsp;8]<br>common_elements&nbsp;=&nbsp;set(list_a)&nbsp;&amp;&nbsp;set(list_b)<br>print(common_elements)&nbsp;&nbsp;<em>#&nbsp;\u8f93\u51fa:&nbsp;{4,&nbsp;5}<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"20_%E5%AD%97%E5%85%B8%E7%9A%84%E6%B7%B1%E6%8B%B7%E8%B4%9D%E4%B8%8E%E6%B5%85%E6%8B%B7%E8%B4%9D%E7%90%86%E8%A7%A3\"><\/span><strong>20. \u5b57\u5178\u7684\u6df1\u62f7\u8d1d\u4e0e\u6d45\u62f7\u8d1d\u7406\u89e3<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5bf9\u4e8e\u590d\u6742\u7684\u5d4c\u5957\u5b57\u5178\uff0c\u6d45\u62f7\u8d1d\u53ef\u80fd\u4e0d\u8db3\u4ee5\u6ee1\u8db3\u9700\u6c42\uff0c\u6b64\u65f6\u5e94\u4f7f\u7528<code>copy.deepcopy()<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import&nbsp;copy<br>nested_dict&nbsp;=&nbsp;{'a':&nbsp;1,&nbsp;'b':&nbsp;{'c':&nbsp;2}}<br>shallow_copy&nbsp;=&nbsp;copy.copy(nested_dict)<br>deep_copy&nbsp;=&nbsp;copy.deepcopy(nested_dict)<br><br><em>#&nbsp;\u4fee\u6539\u539f\u5b57\u5178\u7684\u5d4c\u5957\u90e8\u5206<\/em><br>nested_dict&#91;'b']&#91;'c']&nbsp;=&nbsp;3<br><br>print(shallow_copy&#91;'b']&#91;'c'])&nbsp;&nbsp;<em>#&nbsp;\u8f93\u51fa:&nbsp;3\uff0c\u56e0\u4e3a\u662f\u6d45\u62f7\u8d1d<\/em><br>print(deep_copy&#91;'b']&#91;'c'])&nbsp;&nbsp;&nbsp;&nbsp;<em>#&nbsp;\u8f93\u51fa:&nbsp;2\uff0c\u6df1\u62f7\u8d1d\u4e0d\u53d7\u5f71\u54cd<\/em><\/code><\/pre>\n\n\n\n<p>\u901a\u8fc7\u8fd9\u4e9b\u5b9e\u6218\u6848\u4f8b\uff0c\u4f60\u4e0d\u4ec5\u5b66\u4f1a\u4e86\u5b57\u5178\u4e0e\u96c6\u5408\u7684\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u8fd8\u4e86\u89e3\u4e86\u5982\u4f55\u5728\u5177\u4f53\u573a\u666f\u4e2d\u5e94\u7528\u8fd9\u4e9b\u77e5\u8bc6\uff0c\u63d0\u5347\u4e86\u89e3\u51b3\u95ee\u9898\u7684\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u6211\u4eec\u5c06\u4e00\u8d77\u63a2\u7d22Python\u4e2d\u6700\u5f3a\u5927\u4e14\u9ad8\u6548\u7684\u4e24\u4e2a\u6570\u636e\u7ed3\u6784\u2014\u2014\u5b57\u5178\u548c\u96c6\u5408\uff0c\u5b83\u4eec\u5728\u6570\u636e\u5904\u7406\u4e2d\u626e\u6f14\u7740\u81f3\u5173&hellip; <a href=\"http:\/\/viplao.com\/index.php\/2024\/12\/15\/python%e5%9f%ba%e7%a1%80%e6%8a%80%e8%83%bd-python%e5%ad%97%e5%85%b8%e4%b8%8e%e9%9b%86%e5%90%88%e7%9a%8415%e4%b8%aa%e9%ab%98%e7%ba%a7%e6%93%8d%e4%bd%9c%e4%b8%8e%e4%bc%98%e5%8c%96\/\" class=\"more-link read-more\" rel=\"bookmark\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">PYTHON\u57fa\u7840\u6280\u80fd \u2013\u00a0Python\u5b57\u5178\u4e0e\u96c6\u5408\u768415\u4e2a\u9ad8\u7ea7\u64cd\u4f5c\u4e0e\u4f18\u5316\u5efa\u8bae<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[28],"views":652,"_links":{"self":[{"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/posts\/2921"}],"collection":[{"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/comments?post=2921"}],"version-history":[{"count":1,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/posts\/2921\/revisions"}],"predecessor-version":[{"id":2922,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/posts\/2921\/revisions\/2922"}],"wp:attachment":[{"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/media?parent=2921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/categories?post=2921"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/viplao.com\/index.php\/wp-json\/wp\/v2\/tags?post=2921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}