{"id":1011193,"date":"2024-12-27T11:27:49","date_gmt":"2024-12-27T03:27:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1011193.html"},"modified":"2024-12-27T11:27:51","modified_gmt":"2024-12-27T03:27:51","slug":"python%e5%a6%82%e4%bd%95%e7%94%a8%e5%ad%97%e5%85%b8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1011193.html","title":{"rendered":"python\u5982\u4f55\u7528\u5b57\u5178"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085435\/1bf62c7e-1bb0-42a3-8fa4-70efd635335c.webp\" alt=\"python\u5982\u4f55\u7528\u5b57\u5178\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4f7f\u7528\u5b57\u5178\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u5b57\u5178\u3001\u6dfb\u52a0\u952e\u503c\u5bf9\u3001\u8bbf\u95ee\u548c\u4fee\u6539\u503c\u3001\u4f7f\u7528\u5b57\u5178\u65b9\u6cd5\u3001\u904d\u5386\u5b57\u5178\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u5b57\u5178\u662f\u4e00\u79cd\u53ef\u53d8\u6570\u636e\u7c7b\u578b\uff0c\u652f\u6301\u5feb\u901f\u7684\u67e5\u627e\u548c\u4fee\u6539\u64cd\u4f5c\u3002<\/strong> \u5176\u4e2d\uff0c\u5b57\u5178\u7684\u952e\u5fc5\u987b\u662f\u4e0d\u53ef\u53d8\u7c7b\u578b\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u6216\u5143\u7ec4\uff0c\u800c\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5b57\u5178\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u9645\u7684\u5e94\u7528\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u521b\u5efa\u548c\u521d\u59cb\u5316\u5b57\u5178<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u521b\u5efa\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u3002\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u82b1\u62ec\u53f7 <code>{}<\/code>\uff0c\u5728\u82b1\u62ec\u53f7\u5185\u7528\u5192\u53f7 <code>:<\/code> \u5206\u9694\u952e\u548c\u503c\uff0c\u5e76\u7528\u9017\u53f7 <code>,<\/code> \u5206\u9694\u591a\u4e2a\u952e\u503c\u5bf9\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u5efa\u51fd\u6570 <code>dict()<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u82b1\u62ec\u53f7\u521b\u5efa\u5b57\u5178<\/p>\n<p>student = {&#39;name&#39;: &#39;John&#39;, &#39;age&#39;: 25, &#39;courses&#39;: [&#39;Math&#39;, &#39;CompSci&#39;]}<\/p>\n<h2><strong>\u4f7f\u7528 dict() \u51fd\u6570\u521b\u5efa\u5b57\u5178<\/strong><\/h2>\n<p>student = dict(name=&#39;John&#39;, age=25, courses=[&#39;Math&#39;, &#39;CompSci&#39;])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u5b57\u5178<\/strong><\/h2>\n<p>empty_dict = {}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b57\u5178\u7684\u952e\u5fc5\u987b\u662f\u4e0d\u53ef\u53d8\u7684\u7c7b\u578b\uff0c\u6bd4\u5982\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u6216\u5143\u7ec4\u3002\u503c\u53ef\u4ee5\u662f\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c<\/p>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u503c\u3002\u5982\u679c\u5c1d\u8bd5\u8bbf\u95ee\u4e0d\u5b58\u5728\u7684\u952e\uff0c\u5c06\u4f1a\u5f15\u53d1 <code>KeyError<\/code>\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 <code>None<\/code> \u6216\u60a8\u6307\u5b9a\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u901a\u8fc7\u952e\u8bbf\u95ee\u503c<\/p>\n<p>name = student[&#39;name&#39;]<\/p>\n<p>print(name)  # \u8f93\u51fa: John<\/p>\n<h2><strong>\u4f7f\u7528 get() \u65b9\u6cd5\u8bbf\u95ee\u503c<\/strong><\/h2>\n<p>age = student.get(&#39;age&#39;)<\/p>\n<p>print(age)  # \u8f93\u51fa: 25<\/p>\n<h2><strong>\u4f7f\u7528 get() \u63d0\u4f9b\u9ed8\u8ba4\u503c<\/strong><\/h2>\n<p>favorite_color = student.get(&#39;favorite_color&#39;, &#39;Not specified&#39;)<\/p>\n<p>print(favorite_color)  # \u8f93\u51fa: Not specified<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u6dfb\u52a0\u548c\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9<\/p>\n<\/p>\n<p><p>\u5b57\u5178\u662f\u53ef\u53d8\u7684\u6570\u636e\u7c7b\u578b\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\u6216\u4fee\u6539\u73b0\u6709\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9<\/p>\n<p>student[&#39;phone&#39;] = &#39;555-5555&#39;<\/p>\n<h2><strong>\u4fee\u6539\u73b0\u6709\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>student[&#39;age&#39;] = 26<\/p>\n<p>print(student)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9<\/p>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>del<\/code> \u8bed\u53e5\u6216 <code>pop()<\/code> \u65b9\u6cd5\u6765\u5220\u9664\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528 del \u5220\u9664\u952e\u503c\u5bf9<\/p>\n<p>del student[&#39;age&#39;]<\/p>\n<h2><strong>\u4f7f\u7528 pop() \u65b9\u6cd5\u5220\u9664\u952e\u503c\u5bf9\u5e76\u83b7\u53d6\u5176\u503c<\/strong><\/h2>\n<p>phone = student.pop(&#39;phone&#39;)<\/p>\n<p>print(phone)  # \u8f93\u51fa: 555-5555<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5b57\u5178\u7684\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>Python \u5b57\u5178\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u5efa\u65b9\u6cd5\uff0c\u5e2e\u52a9\u60a8\u66f4\u65b9\u4fbf\u5730\u64cd\u4f5c\u5b57\u5178\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u5b57\u5178\u7684\u6240\u6709\u952e<\/p>\n<p>keys = student.keys()<\/p>\n<h2><strong>\u83b7\u53d6\u5b57\u5178\u7684\u6240\u6709\u503c<\/strong><\/h2>\n<p>values = student.values()<\/p>\n<h2><strong>\u83b7\u53d6\u5b57\u5178\u7684\u6240\u6709\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>items = student.items()<\/p>\n<h2><strong>\u6e05\u7a7a\u5b57\u5178<\/strong><\/h2>\n<p>student.clear()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u904d\u5386\u5b57\u5178<\/p>\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<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u904d\u5386\u5b57\u5178\u7684\u952e<\/p>\n<p>for key in student.keys():<\/p>\n<p>    print(key)<\/p>\n<h2><strong>\u904d\u5386\u5b57\u5178\u7684\u503c<\/strong><\/h2>\n<p>for value in student.values():<\/p>\n<p>    print(value)<\/p>\n<h2><strong>\u904d\u5386\u5b57\u5178\u7684\u952e\u503c\u5bf9<\/strong><\/h2>\n<p>for key, value in student.items():<\/p>\n<p>    print(f&#39;{key}: {value}&#39;)<\/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\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u67e5\u627e\u548c\u66f4\u65b0\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8ba1\u6570\u5668<\/strong>\uff1a\u5b57\u5178\u53ef\u4ee5\u7528\u6765\u8ba1\u6570\uff0c\u4f8b\u5982\u7edf\u8ba1\u6587\u672c\u4e2d\u6bcf\u4e2a\u5355\u8bcd\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;apple banana apple strawberry banana apple&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>print(word_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7f13\u5b58\u673a\u5236<\/strong>\uff1a\u5728\u4e00\u4e9b\u8ba1\u7b97\u6602\u8d35\u7684\u64cd\u4f5c\u4e2d\uff0c\u5b57\u5178\u53ef\u4ee5\u7528\u4f5c\u7f13\u5b58\u4ee5\u5b58\u50a8\u5df2\u7ecf\u8ba1\u7b97\u8fc7\u7684\u7ed3\u679c\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u6587\u4ef6<\/strong>\uff1a\u5b57\u5178\u5e38\u7528\u4e8e\u5b58\u50a8\u7a0b\u5e8f\u7684\u914d\u7f6e\u53c2\u6570\uff0c\u4ee5\u4fbf\u4e8e\u8bbf\u95ee\u548c\u4fee\u6539\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u638c\u63e1\u5b57\u5178\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u7f16\u7a0b\u4e2d\u66f4\u9ad8\u6548\u5730\u7ec4\u7ec7\u548c\u7ba1\u7406\u6570\u636e\u3002\u5b57\u5178\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u4e0d\u4ec5\u652f\u6301\u5feb\u901f\u7684\u67e5\u627e\u548c\u4fee\u6539\u64cd\u4f5c\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u65b9\u6cd5\u6765\u6ee1\u8db3\u5404\u79cd\u7f16\u7a0b\u9700\u6c42\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\u4e00\u79cd\u65e0\u5e8f\u7684\u53ef\u53d8\u96c6\u5408\uff0c\u4f7f\u7528\u952e-\u503c\u5bf9\u5b58\u50a8\u6570\u636e\u3002\u53ef\u4ee5\u901a\u8fc7\u5927\u62ec\u53f7 <code>{}<\/code> \u6765\u521b\u5efa\u5b57\u5178\uff0c\u6216\u8005\u4f7f\u7528 <code>dict()<\/code> \u51fd\u6570\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30, &#39;city&#39;: &#39;New York&#39;}\n<\/code><\/pre>\n<p>\u6216\u8005\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict = dict(name=&#39;Alice&#39;, age=30, city=&#39;New York&#39;)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u8bbf\u95ee\u548c\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u503c\uff1f<\/strong><br \/>\u5b57\u5178\u4e2d\u7684\u503c\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u8bbf\u95ee\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <code>my_dict[&#39;name&#39;]<\/code> \u53ef\u4ee5\u83b7\u53d6\u5230 &#39;Alice&#39;\u3002\u8981\u4fee\u6539\u5b57\u5178\u4e2d\u7684\u503c\uff0c\u53ef\u4ee5\u76f4\u63a5\u8d4b\u503c\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict[&#39;age&#39;] = 31  # \u73b0\u5728\u5e74\u9f84\u53d8\u621031\n<\/code><\/pre>\n<p>\u5982\u679c\u952e\u4e0d\u5b58\u5728\uff0c\u8d4b\u503c\u64cd\u4f5c\u4f1a\u81ea\u52a8\u6dfb\u52a0\u8be5\u952e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u5b57\u5178\u4e2d\u6dfb\u52a0\u6216\u5220\u9664\u952e\u503c\u5bf9\uff1f<\/strong><br \/>\u8981\u6dfb\u52a0\u65b0\u7684\u952e\u503c\u5bf9\uff0c\u53ea\u9700\u76f4\u63a5\u8d4b\u503c\u7ed9\u65b0\u7684\u952e\u5373\u53ef\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_dict[&#39;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l&#39;] = &#39;alice@example.com&#39;  # \u6dfb\u52a0email\u952e\n<\/code><\/pre>\n<p>\u5220\u9664\u952e\u503c\u5bf9\u53ef\u4ee5\u4f7f\u7528 <code>del<\/code> \u8bed\u53e5\u6216 <code>pop()<\/code> \u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">del my_dict[&#39;city&#39;]  # \u5220\u9664city\u952e\n# \u6216\u8005\nage = my_dict.pop(&#39;age&#39;)  # \u5220\u9664age\u952e\u5e76\u8fd4\u56de\u5176\u503c\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528\u5b57\u5178\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u5b57\u5178\u3001\u6dfb\u52a0\u952e\u503c\u5bf9\u3001\u8bbf\u95ee\u548c\u4fee\u6539\u503c\u3001\u4f7f\u7528\u5b57\u5178\u65b9\u6cd5\u3001\u904d\u5386\u5b57\u5178\u7b49\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u5b57 [&hellip;]","protected":false},"author":3,"featured_media":1011198,"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\/1011193"}],"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=1011193"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011193\/revisions"}],"predecessor-version":[{"id":1011199,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011193\/revisions\/1011199"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1011198"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1011193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1011193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1011193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}