{"id":3480,"date":"2025-06-03T14:45:20","date_gmt":"2025-06-04T07:36:20","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3480"},"modified":"2025-06-04T14:45:20","modified_gmt":"2025-06-04T07:36:20","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-3","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-3\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 len() \u51fd\u6578\uff1a\u529f\u80fd\u3001\u7528\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, len()\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 len() \u51fd\u6578\uff1a\u529f\u80fd\u3001\u7528\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>Python \u7684 len() \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5167\u5efa\u51fd\u6578\uff0c\u5b83\u80fd\u5920\u8f15\u9b06\u5730\u53d6\u5f97\u5404\u7a2e\u8cc7\u6599\u7d50\u69cb\uff08\u5982\u5b57\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\uff09\u7684\u9577\u5ea6\u3002\u6b64\u5916\uff0clen() \u51fd\u6578\u4e5f\u80fd\u5354\u52a9\u6211\u5011\u6aa2\u67e5\u8cc7\u6599\u7d50\u69cb\u662f\u5426\u70ba\u7a7a\u3002\u672c\u6587\u5c07\u4ecb\u7d39 len() \u51fd\u6578\u7684\u6700\u65b0\u7528\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u4f60\u66f4\u6709\u6548\u5730\u4f7f\u7528\u9019\u500b\u51fd\u6578\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\u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u5c07\u5b57\u4e32\u4f5c\u70ba\u53c3\u6578\u50b3\u5165 len() \u51fd\u6578\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_string = &#8220;Hello, World&#8221;<br \/>\nstring_length = len(my_string)<br \/>\nprint(string_length)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\u70ba\uff1a<\/p>\n<p>&#8220;`<br \/>\n13<br \/>\n&#8220;`<\/p>\n<p>\u6ce8\u610f\uff1a\u5728 Python 3.x \u4e2d\uff0c\u5b57\u4e32\u7684\u9577\u5ea6\u6703\u5305\u542b\u6240\u6709\u5b57\u7b26\uff0c\u5305\u62ec\u7a7a\u683c\u548c\u6a19\u9ede\u7b26\u865f\u3002<\/p>\n<h2>\u4f7f\u7528 len() \u51fd\u6578\u53d6\u5f97\u5217\u8868\u9577\u5ea6<\/h2>\n<p>\u540c\u6a23\u5730\uff0c\u53d6\u5f97\u5217\u8868\u7684\u9577\u5ea6\u4e5f\u5f88\u7c21\u55ae\u3002\u53ea\u9700\u5c07\u5217\u8868\u4f5c\u70ba\u53c3\u6578\u50b3\u5165 len() \u51fd\u6578\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_list = [1, 2, 3, 4, 5]\nlist_length = len(my_list)<br \/>\nprint(list_length)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\u70ba\uff1a<\/p>\n<p>&#8220;`<br \/>\n5<br \/>\n&#8220;`<\/p>\n<h2>\u4f7f\u7528 len() \u51fd\u6578\u53d6\u5f97\u5b57\u5178\u9577\u5ea6<\/h2>\n<p>\u8981\u53d6\u5f97\u5b57\u5178\u7684\u9577\u5ea6\uff0c\u9019\u88e1\u4e5f\u53ea\u9700\u5c07\u5b57\u5178\u4f5c\u70ba\u53c3\u6578\u50b3\u5165 len() \u51fd\u6578\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_dict = {&#8216;name&#8217;: &#8216;John&#8217;, &#8216;age&#8217;: 20}<br \/>\ndict_length = len(my_dict)<br \/>\nprint(dict_length)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\u70ba\uff1a<\/p>\n<p>&#8220;`<br \/>\n2<br \/>\n&#8220;`<\/p>\n<p>\u9019\u8868\u793a\u5b57\u5178\u4e2d\u6709\u5169\u500b\u9375\u503c\u5c0d\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\u4e0d\u50c5\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u9577\u5ea6\uff0c\u9084\u80fd\u5e6b\u52a9\u6211\u5011\u6aa2\u67e5\u8cc7\u6599\u7d50\u69cb\u662f\u5426\u70ba\u7a7a\u3002\u82e5\u8cc7\u6599\u7d50\u69cb\u70ba\u7a7a\uff0c\u5247 len() \u51fd\u6578\u5c07\u8fd4\u56de 0\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_list = []\nlist_length = len(my_list)<br \/>\nif list_length == 0:<br \/>\n    print(&#8220;List is empty&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\u70ba\uff1a<\/p>\n<p>&#8220;`<br \/>\nList is empty<br \/>\n&#8220;`<\/p>\n<p>\u9019\u662f\u4e00\u7a2e\u7c21\u55ae\u800c\u6709\u6548\u7684\u6aa2\u67e5\u65b9\u6cd5\u3002<\/p>\n<h2>\u5e38\u898b\u932f\u8aa4\u6392\u9664<\/h2>\n<p>1. **TypeError**\uff1a\u5982\u679c\u5c07\u975e\u53ef\u8a08\u7b97\u9577\u5ea6\u7684\u5c0d\u8c61\uff08\u5982\u6574\u6578\uff09\u50b3\u5165 len() \u51fd\u6578\uff0c\u5c07\u6703\u5f15\u767c TypeError\u3002\u78ba\u4fdd\u50b3\u5165\u7684\u53c3\u6578\u985e\u578b\u6b63\u78ba\u3002<\/p>\n<p>2. **\u7a7a\u5217\u8868\u6216\u5b57\u5178\u7684\u5224\u65b7**\uff1a\u4f7f\u7528 len() \u6aa2\u67e5\u662f\u5426\u70ba\u7a7a\u6642\uff0c\u5207\u52ff\u5c07\u9577\u5ea6\u8207 None \u9032\u884c\u6bd4\u8f03\uff0c\u56e0\u70ba None \u4e0d\u662f\u6709\u6548\u7684\u9577\u5ea6\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>len() \u51fd\u6578\u5728 Python \u7684\u6578\u64da\u8655\u7406\u4e2d\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u5c24\u5176\u5728\u8655\u7406\u8cc7\u6599\u6642\uff0c\u5e38\u5e38\u9700\u8981\u5feb\u901f\u7372\u53d6\u8cc7\u6599\u7684\u9577\u5ea6\u3002\u4f60\u53ef\u4ee5\u5c07 len() \u8207\u5176\u4ed6\u51fd\u6578\u7d50\u5408\u4f7f\u7528\uff0c\u4f8b\u5982\u5728\u5217\u8868\u63a8\u5c0e\u5f0f\u4e2d\uff0c\u904e\u6ffe\u51fa\u7279\u5b9a\u9577\u5ea6\u7684\u5b57\u4e32\u6216\u5217\u8868\u3002<\/p>\n<p>\u7bc4\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_strings = [&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;kiwi&#8221;, &#8220;pear&#8221;]\nfiltered_strings = [s for s in my_strings if len(s) > 4]\nprint(filtered_strings)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\u70ba\uff1a<\/p>\n<p>&#8220;`<br \/>\n[&#8216;banana&#8217;]\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c07\u904e\u6ffe\u51fa\u6240\u6709\u9577\u5ea6\u5927\u65bc 4 \u7684\u5b57\u4e32\u3002<\/p>\n<p>\u7e3d\u7d50\u4f86\u8aaa\uff0cPython \u7684 len() \u51fd\u6578\u662f\u8655\u7406\u5404\u985e\u8cc7\u6599\u7d50\u69cb\u9577\u5ea6\u7684\u57fa\u672c\u5de5\u5177\u3002\u4e0d\u8ad6\u662f\u5728\u958b\u767c\u9084\u662f\u65e5\u5e38\u4f7f\u7528\u4e2d\uff0c\u638c\u63e1\u5b83\u7684\u7528\u6cd5\u90fd\u5c07\u4f7f\u4f60\u7684\u7a0b\u5f0f\u78bc\u66f4\u52a0\u9ad8\u6548\u8207\u7c21\u6f54\u3002\u5982\u679c\u4f60\u60f3\u8981\u66f4\u6df1\u5165\u4e86\u89e3 Python \u7684 len() \u51fd\u6578\uff0c\u5efa\u8b70\u53c3\u8003\u5b98\u65b9\u6587\u4ef6\uff1a[Python \u5b98\u65b9\u6587\u6a94](https:\/\/docs.python.org\/3\/library\/functions.html#len)\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: len() \u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u54ea\u4e9b\u8cc7\u6599\u7d50\u69cb\u7684\u9577\u5ea6\uff1f**<br \/>\nA1: len() \u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u5b57\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u7b49\u591a\u7a2e\u8cc7\u6599\u7d50\u69cb\u7684\u9577\u5ea6\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u6aa2\u67e5\u4e00\u500b\u5b57\u5178\u662f\u5426\u70ba\u7a7a\uff1f**<br \/>\nA2: \u53ef\u4ee5\u4f7f\u7528 len() \u51fd\u6578\u4f86\u6aa2\u67e5\u5b57\u5178\u7684\u9577\u5ea6\uff0c\u5982\u679c\u8fd4\u56de 0\uff0c\u5247\u8868\u793a\u5b57\u5178\u70ba\u7a7a\u3002<\/p>\n<p>**Q3: len() \u51fd\u6578\u80fd\u5426\u7528\u65bc\u81ea\u5b9a\u7fa9\u8cc7\u6599\u985e\u578b\uff1f**<br \/>\nA3: \u53ef\u4ee5\u3002\u53ea\u8981\u81ea\u5b9a\u7fa9\u985e\u578b\u5be6\u73fe\u4e86 `__len__` \u65b9\u6cd5\uff0c\u5247\u53ef\u4ee5\u4f7f\u7528 len() \u51fd\u6578\u7372\u53d6\u5176\u9577\u5ea6\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u5b78\u7fd2 Python \u7684\u5176\u4ed6\u51fd\u6578\u8207\u6280\u5de7\uff0c\u8acb\u53c3\u8003\u9019\u88e1\u7684\u6559\u5b78\uff1a[Python \u6559\u5b78\u8cc7\u6e90](https:\/\/vocus.cc)\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2dlen()\u51fd\u6578\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u7528\u4f86\u8a08\u7b97\u5b57\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u3001\u5143\u7d44\u7b49\u7684\u9577\u5ea6\uff0c\u53ef\u4ee5\u5e6b\u52a9\u4f60\u66f4\u6709\u6548\u7387\u5730\u7ba1\u7406\u4f60\u7684\u8cc7\u6599\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-3480","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-U8","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3480","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=3480"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3480\/revisions"}],"predecessor-version":[{"id":12819,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3480\/revisions\/12819"}],"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=3480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}