{"id":3086,"date":"2025-05-29T16:40:04","date_gmt":"2025-06-04T08:57:04","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3086"},"modified":"2025-06-04T16:40:04","modified_gmt":"2025-06-04T08:57:04","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%adlen%e5%87%bd%e6%95%b8%e7%9a%84%e5%8a%9f%e8%83%bd-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%adlen%e5%87%bd%e6%95%b8%e7%9a%84%e5%8a%9f%e8%83%bd-2\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 len() \u51fd\u6578\uff1a\u4f7f\u7528\u65b9\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, len(), Python \u7a0b\u5f0f\u6559\u5b78\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 len() \u51fd\u6578\uff1a\u4f7f\u7528\u65b9\u6cd5\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>Python \u7684 `len()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5167\u5efa\u51fd\u6578\uff0c\u80fd\u5920\u8f15\u9b06\u5730\u7372\u53d6\u5404\u7a2e\u8cc7\u6599\u7d50\u69cb\uff08\u5982\u5b57\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\uff09\u7684\u9577\u5ea6\u3002\u638c\u63e1\u9019\u500b\u51fd\u6578\uff0c\u5c0d\u65bc Python \u7a0b\u5f0f\u8a2d\u8a08\u7684\u5b78\u7fd2\u8207\u5be6\u4f5c\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002<\/p>\n<h2>\u4f7f\u7528 len() \u51fd\u6578\u53d6\u5f97\u5b57\u4e32\u9577\u5ea6<\/h2>\n<p>\u8981\u53d6\u5f97\u5b57\u4e32\u7684\u9577\u5ea6\uff0c\u53ea\u9700\u5c07\u5b57\u4e32\u4f5c\u70ba\u53c3\u6578\u50b3\u5165 `len()` \u51fd\u6578\u5373\u53ef\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_string = &#8220;Hello World&#8221;<br \/>\nlength = len(my_string)<br \/>\nprint(f&#8221;The length of the string is: {length}&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u4ee5\u4e0a\u7a0b\u5f0f\u78bc\u5f8c\uff0c\u60a8\u5c07\u7372\u5f97\u5b57\u4e32 &#8220;Hello World&#8221; \u7684\u9577\u5ea6\uff0c\u7d50\u679c\u70ba 11\u3002<\/p>\n<h2>\u4f7f\u7528 len() \u51fd\u6578\u53d6\u5f97\u5217\u8868\u9577\u5ea6<\/h2>\n<p>\u540c\u6a23\u5730\uff0c\u53ef\u4ee5\u4f7f\u7528 `len()` \u51fd\u6578\u4f86\u7372\u53d6\u5217\u8868\u7684\u9577\u5ea6\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_list = [1, 2, 3, 4, 5]\nlength = len(my_list)<br \/>\nprint(f&#8221;The length of the list is: {length}&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c07\u8fd4\u56de\u5217\u8868 [1, 2, 3, 4, 5] \u7684\u9577\u5ea6\uff0c\u7d50\u679c\u70ba 5\u3002<\/p>\n<h2>\u4f7f\u7528 len() \u51fd\u6578\u53d6\u5f97\u5b57\u5178\u9577\u5ea6<\/h2>\n<p>\u5c0d\u65bc\u5b57\u5178\uff0c`len()` \u51fd\u6578\u4e5f\u80fd\u5f88\u597d\u5730\u5de5\u4f5c\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_dict = {&#8216;name&#8217;: &#8216;John&#8217;, &#8216;age&#8217;: 20}<br \/>\nlength = len(my_dict)<br \/>\nprint(f&#8221;The length of the dictionary is: {length}&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u4ee5\u4e0a\u7a0b\u5f0f\u78bc\u5c07\u5f97\u5230\u5b57\u5178 {&#8216;name&#8217;: &#8216;John&#8217;, &#8216;age&#8217;: 20} \u7684\u9577\u5ea6\uff0c\u7d50\u679c\u70ba 2\u3002<\/p>\n<h2>\u4f7f\u7528 len() \u51fd\u6578\u6aa2\u67e5\u8cc7\u6599\u7d50\u69cb\u662f\u5426\u70ba\u7a7a<\/h2>\n<p>`len()` \u51fd\u6578\u4e5f\u53ef\u4ee5\u7528\u4f86\u6aa2\u67e5\u8cc7\u6599\u7d50\u69cb\u662f\u5426\u70ba\u7a7a\u3002\u5982\u679c\u8cc7\u6599\u7d50\u69cb\u7684\u9577\u5ea6\u70ba 0\uff0c\u5247\u8868\u793a\u8a72\u8cc7\u6599\u7d50\u69cb\u662f\u7a7a\u7684\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nempty_list = []\nif len(empty_list) == 0:<br \/>\n    print(&#8220;The list is empty&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c07\u6aa2\u67e5\u5217\u8868\u662f\u5426\u70ba\u7a7a\uff0c\u82e5\u662f\uff0c\u5247\u6703\u8f38\u51fa &#8220;The list is empty&#8221; \u7684\u8a0a\u606f\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528 `len()` \u51fd\u6578\u6642\uff0c\u6709\u5e7e\u500b\u5e38\u898b\u7684\u932f\u8aa4\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<p>1. **\u932f\u8aa4\u7684\u8cc7\u6599\u985e\u578b**\uff1a\u78ba\u4fdd\u50b3\u5165 `len()` \u7684\u53c3\u6578\u662f\u53ef\u4ee5\u8a08\u7b97\u9577\u5ea6\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u4f8b\u5982\u5b57\u4e32\u3001\u5217\u8868\u6216\u5b57\u5178\uff0c\u5426\u5247\u5c07\u5f15\u767c `TypeError`\u3002<\/p>\n<p>2. **\u4f7f\u7528\u8b8a\u6578\u540d**\uff1a\u907f\u514d\u4f7f\u7528 Python \u7684\u95dc\u9375\u5b57\u6216\u5167\u5efa\u51fd\u6578\u4f5c\u70ba\u8b8a\u6578\u540d\uff0c\u50cf\u662f `list` \u548c `dict` \u7b49\uff0c\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u6f5b\u5728\u7684\u885d\u7a81\u6216\u932f\u8aa4\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u5728\u66f4\u8907\u96dc\u7684\u7a0b\u5f0f\u4e2d\uff0c`len()` \u51fd\u6578\u53ef\u4ee5\u8207\u5176\u4ed6\u51fd\u6578\u7d50\u5408\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 `len()` \u7d50\u5408\u689d\u4ef6\u8a9e\u53e5\u4f86\u9032\u884c\u8cc7\u6599\u9a57\u8b49\u6216\u6578\u64da\u8655\u7406\u3002\u9019\u6a23\u7684\u61c9\u7528\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5f0f\u7684\u5065\u58ef\u6027\u8207\u9748\u6d3b\u6027\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u66f4\u9ad8\u7d1a\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u5982\u96c6\u5408\uff08set\uff09\u548c\u5143\u7d44\uff08tuple\uff09\uff0c\u4e5f\u80fd\u5920\u5e6b\u52a9\u60a8\u66f4\u5168\u9762\u5730\u638c\u63e1 `len()` \u51fd\u6578\u7684\u61c9\u7528\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Python \u7684 `len()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u51fd\u6578\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u7372\u53d6\u5404\u7a2e\u8cc7\u6599\u7d50\u69cb\u7684\u9577\u5ea6\uff0c\u4e5f\u53ef\u7528\u4f86\u6aa2\u67e5\u8cc7\u6599\u7d50\u69cb\u662f\u5426\u70ba\u7a7a\u3002\u900f\u904e\u6b63\u78ba\u7684\u4f7f\u7528\u65b9\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u60a8\u53ef\u4ee5\u63d0\u5347\u7a0b\u5f0f\u7684\u54c1\u8cea\u8207\u53ef\u8b80\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5e0c\u671b\u6df1\u5165\u4e86\u89e3 Python \u7684\u5176\u4ed6\u57fa\u790e\u8207\u9032\u968e\u6982\u5ff5\uff0c\u5efa\u8b70\u53c3\u8003 [\u9019\u7bc7\u6587\u7ae0](https:\/\/vocus.cc\/article\/6053d6d6fd8978000128c7d2) \u4ee5\u7372\u5f97\u66f4\u591a\u6559\u5b78\u8cc7\u8a0a\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: `len()` \u51fd\u6578\u80fd\u7528\u65bc\u54ea\u4e9b\u8cc7\u6599\u985e\u578b\uff1f**<br \/>\nA1: `len()` \u51fd\u6578\u53ef\u4ee5\u7528\u65bc\u5b57\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u3001\u5143\u7d44\u7b49\u8cc7\u6599\u7d50\u69cb\uff0c\u4ee5\u7372\u53d6\u5176\u5143\u7d20\u7684\u6578\u91cf\u3002<\/p>\n<p>**Q2: \u5982\u679c\u50b3\u5165 `len()` \u7684\u53c3\u6578\u662f `None` \u6703\u767c\u751f\u4ec0\u9ebc\u4e8b\uff1f**<br \/>\nA2: \u5982\u679c\u5c07 `None` \u50b3\u5165 `len()` \u51fd\u6578\uff0c\u5247\u6703\u5f15\u767c `TypeError`\uff0c\u56e0\u70ba `None` \u6c92\u6709\u5b9a\u7fa9\u9577\u5ea6\u3002<\/p>\n<p>**Q3: \u6709\u4ec0\u9ebc\u65b9\u6cd5\u53ef\u4ee5\u5feb\u901f\u6aa2\u67e5\u4e00\u500b\u8cc7\u6599\u7d50\u69cb\u662f\u5426\u70ba\u7a7a\uff1f**<br \/>\nA3: \u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 `if not my_list:` \u6216 `if len(my_list) == 0:` \u4f86\u6aa2\u67e5\u8cc7\u6599\u7d50\u69cb\u662f\u5426\u70ba\u7a7a\uff0c\u5169\u8005\u5747\u53ef\u6709\u6548\u9054\u5230\u76ee\u7684\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2dlen()\u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u5b57\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u7b49\u7269\u4ef6\u7684\u9577\u5ea6\uff0c\u672c\u6587\u5c07\u4ecb\u7d39len()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3086","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-NM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3086"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3086\/revisions"}],"predecessor-version":[{"id":12936,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3086\/revisions\/12936"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}