{"id":570,"date":"2025-10-23T15:14:37","date_gmt":"2025-10-23T07:14:37","guid":{"rendered":"http:\/\/www.pyteam.cn\/?p=570"},"modified":"2025-10-23T15:14:38","modified_gmt":"2025-10-23T07:14:38","slug":"python-%e6%8e%a8%e5%af%bc%e5%bc%8f","status":"publish","type":"post","link":"http:\/\/www.pyteam.cn\/index.php\/2025\/10\/23\/python-%e6%8e%a8%e5%af%bc%e5%bc%8f\/","title":{"rendered":"Python \u63a8\u5bfc\u5f0f"},"content":{"rendered":"\n<p>Python \u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u72ec\u7279\u7684\u6570\u636e\u5904\u7406\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4ece\u4e00\u4e2a\u6570\u636e\u5e8f\u5217\u6784\u5efa\u53e6\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u5e8f\u5217\u7684\u7ed3\u6784\u4f53\u3002<\/p>\n\n\n\n<p>Python \u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7b80\u6d01\u7684\u8bed\u6cd5\uff0c\u9002\u7528\u4e8e\u751f\u6210\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u548c\u751f\u6210\u5668\u3002<\/p>\n\n\n\n<p>\u5728\u4f7f\u7528\u63a8\u5bfc\u5f0f\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u53ef\u8bfb\u6027\uff0c\u5c3d\u91cf\u4fdd\u6301\u8868\u8fbe\u5f0f\u7b80\u6d01\uff0c\u4ee5\u514d\u5f71\u54cd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n\n\n\n<p>Python \u652f\u6301\u5404\u79cd\u6570\u636e\u7ed3\u6784\u7684\u63a8\u5bfc\u5f0f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5217\u8868(list)\u63a8\u5bfc\u5f0f<\/li>\n\n\n\n<li>\u5b57\u5178(dict)\u63a8\u5bfc\u5f0f<\/li>\n\n\n\n<li>\u96c6\u5408(set)\u63a8\u5bfc\u5f0f<\/li>\n\n\n\n<li>\u5143\u7ec4(tuple)\u63a8\u5bfc\u5f0f<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u5217\u8868\u63a8\u5bfc\u5f0f<\/h2>\n\n\n\n<p>\u5217\u8868\u63a8\u5bfc\u5f0f\u683c\u5f0f\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[\u8868\u8fbe\u5f0f for \u53d8\u91cf in \u5217\u8868] \n[out_exp_res for out_exp in input_list]\n\n\u6216\u8005 \n\n[\u8868\u8fbe\u5f0f for \u53d8\u91cf in \u5217\u8868 if \u6761\u4ef6]\n[out_exp_res for out_exp in input_list if condition]<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>out_exp_res\uff1a\u5217\u8868\u751f\u6210\u5143\u7d20\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u662f\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\u3002<\/li>\n\n\n\n<li>for out_exp in input_list\uff1a\u8fed\u4ee3 input_list \u5c06 out_exp \u4f20\u5165\u5230 out_exp_res \u8868\u8fbe\u5f0f\u4e2d\u3002<\/li>\n\n\n\n<li>if condition\uff1a\u6761\u4ef6\u8bed\u53e5\uff0c\u53ef\u4ee5\u8fc7\u6ee4\u5217\u8868\u4e2d\u4e0d\u7b26\u5408\u6761\u4ef6\u7684\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fc7\u6ee4\u6389\u957f\u5ea6\u5c0f\u4e8e\u6216\u7b49\u4e8e3\u7684\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u5e76\u5c06\u5269\u4e0b\u7684\u8f6c\u6362\u6210\u5927\u5199\u5b57\u6bcd\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>&gt;&gt;&gt;&nbsp;names&nbsp;=&nbsp;[&#8216;Bob&#8217;,&#8217;Tom&#8217;,&#8217;alice&#8217;,&#8217;Jerry&#8217;,&#8217;Wendy&#8217;,&#8217;Smith&#8217;]<br>&gt;&gt;&gt;&nbsp;new_names&nbsp;=&nbsp;[name.upper()<strong>for<\/strong>&nbsp;name&nbsp;<strong>in<\/strong>&nbsp;names&nbsp;<strong>if<\/strong>&nbsp;len(name)&gt;3]<br>&gt;&gt;&gt;&nbsp;<strong>print<\/strong>(new_names)<br>[&#8216;ALICE&#8217;,&nbsp;&#8216;JERRY&#8217;,&nbsp;&#8216;WENDY&#8217;,&nbsp;&#8216;SMITH&#8217;]<\/p>\n\n\n\n<p>\u8ba1\u7b97 30 \u4ee5\u5185\u53ef\u4ee5\u88ab 3 \u6574\u9664\u7684\u6574\u6570\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>&gt;&gt;&gt;&nbsp;multiples&nbsp;=&nbsp;[i&nbsp;<strong>for<\/strong>&nbsp;i&nbsp;<strong>in<\/strong>&nbsp;range(30)&nbsp;<strong>if<\/strong>&nbsp;i %&nbsp;3&nbsp;==&nbsp;0]<br>&gt;&gt;&gt;&nbsp;<strong>print<\/strong>(multiples)<br>[0,&nbsp;3,&nbsp;6,&nbsp;9,&nbsp;12,&nbsp;15,&nbsp;18,&nbsp;21,&nbsp;24,&nbsp;27]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h2>\n\n\n\n<p>\u5b57\u5178\u63a8\u5bfc\u57fa\u672c\u683c\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ key_expr: value_expr for value in collection }\n\n\u6216\n\n{ key_expr: value_expr for value in collection if condition }<\/pre>\n\n\n\n<p>\u4f7f\u7528\u5b57\u7b26\u4e32\u53ca\u5176\u957f\u5ea6\u521b\u5efa\u5b57\u5178\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>listdemo&nbsp;=&nbsp;[&#8216;Google&#8217;,&#8217;Runoob&#8217;,&nbsp;&#8216;Taobao&#8217;]<br># \u5c06\u5217\u8868\u4e2d\u5404\u5b57\u7b26\u4e32\u503c\u4e3a\u952e\uff0c\u5404\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u4e3a\u503c\uff0c\u7ec4\u6210\u952e\u503c\u5bf9<br>&gt;&gt;&gt;&nbsp;newdict&nbsp;=&nbsp;{key:len(key)&nbsp;<strong>for<\/strong>&nbsp;key&nbsp;<strong>in<\/strong>&nbsp;listdemo}<br>&gt;&gt;&gt;&nbsp;newdict<br>{&#8216;Google&#8217;:&nbsp;6,&nbsp;&#8216;Runoob&#8217;:&nbsp;6,&nbsp;&#8216;Taobao&#8217;:&nbsp;6}<\/p>\n\n\n\n<p>\u63d0\u4f9b\u4e09\u4e2a\u6570\u5b57\uff0c\u4ee5\u4e09\u4e2a\u6570\u5b57\u4e3a\u952e\uff0c\u4e09\u4e2a\u6570\u5b57\u7684\u5e73\u65b9\u4e3a\u503c\u6765\u521b\u5efa\u5b57\u5178\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>&gt;&gt;&gt;&nbsp;dic&nbsp;=&nbsp;{x: x**2&nbsp;<strong>for<\/strong>&nbsp;x&nbsp;<strong>in<\/strong>&nbsp;(2,&nbsp;4,&nbsp;6)}<br>&gt;&gt;&gt;&nbsp;dic<br>{2:&nbsp;4,&nbsp;4:&nbsp;16,&nbsp;6:&nbsp;36}<br>&gt;&gt;&gt;&nbsp;type(dic)<br>&lt;<strong>class<\/strong>&nbsp;&#8216;dict&#8217;&gt;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u96c6\u5408\u63a8\u5bfc\u5f0f<\/h2>\n\n\n\n<p>\u96c6\u5408\u63a8\u5bfc\u5f0f\u57fa\u672c\u683c\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{ expression for item in Sequence }\n\u6216\n{ expression for item in Sequence if conditional }<\/pre>\n\n\n\n<p>\u8ba1\u7b97\u6570\u5b57 1,2,3 \u7684\u5e73\u65b9\u6570\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>&gt;&gt;&gt;&nbsp;setnew&nbsp;=&nbsp;{i**2&nbsp;<strong>for<\/strong>&nbsp;i&nbsp;<strong>in<\/strong>&nbsp;(1,2,3)}<br>&gt;&gt;&gt;&nbsp;setnew<br>{1,&nbsp;4,&nbsp;9}<\/p>\n\n\n\n<p>\u5224\u65ad\u4e0d\u662f abc \u7684\u5b57\u6bcd\u5e76\u8f93\u51fa\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>&gt;&gt;&gt;&nbsp;a&nbsp;=&nbsp;{x&nbsp;<strong>for<\/strong>&nbsp;x&nbsp;<strong>in<\/strong>&nbsp;&#8216;abracadabra&#8217;&nbsp;<strong>if<\/strong>&nbsp;x&nbsp;<strong>not<\/strong>&nbsp;<strong>in<\/strong>&nbsp;&#8216;abc&#8217;}<br>&gt;&gt;&gt;&nbsp;a<br>{&#8216;d&#8217;,&nbsp;&#8216;r&#8217;}<br>&gt;&gt;&gt;&nbsp;type(a)<br>&lt;<strong>class<\/strong>&nbsp;&#8216;set&#8217;&gt;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5143\u7ec4\u63a8\u5bfc\u5f0f\uff08\u751f\u6210\u5668\u8868\u8fbe\u5f0f\uff09<\/h2>\n\n\n\n<p>\u5143\u7ec4\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5229\u7528 range \u533a\u95f4\u3001\u5143\u7ec4\u3001\u5217\u8868\u3001\u5b57\u5178\u548c\u96c6\u5408\u7b49\u6570\u636e\u7c7b\u578b\uff0c\u5feb\u901f\u751f\u6210\u4e00\u4e2a\u6ee1\u8db3\u6307\u5b9a\u9700\u6c42\u7684\u5143\u7ec4\u3002<\/p>\n\n\n\n<p>\u5143\u7ec4\u63a8\u5bfc\u5f0f\u57fa\u672c\u683c\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">(expression for item in Sequence )\n\u6216\n(expression for item in Sequence if conditional )<\/pre>\n\n\n\n<p>\u5143\u7ec4\u63a8\u5bfc\u5f0f\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u7528\u6cd5\u4e5f\u5b8c\u5168\u76f8\u540c\uff0c\u53ea\u662f\u5143\u7ec4\u63a8\u5bfc\u5f0f\u662f\u7528&nbsp;<strong>()<\/strong>&nbsp;\u5706\u62ec\u53f7\u5c06\u5404\u90e8\u5206\u62ec\u8d77\u6765\uff0c\u800c\u5217\u8868\u63a8\u5bfc\u5f0f\u7528\u7684\u662f\u4e2d\u62ec\u53f7&nbsp;<strong>[]<\/strong>\uff0c\u53e6\u5916\u5143\u7ec4\u63a8\u5bfc\u5f0f\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6570\u5b57 1~9 \u7684\u5143\u7ec4\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>&gt;&gt;&gt;&nbsp;a&nbsp;=&nbsp;(x&nbsp;<strong>for<\/strong>&nbsp;x&nbsp;<strong>in<\/strong>&nbsp;range(1,10))<br>&gt;&gt;&gt;&nbsp;a<br>&lt;generator&nbsp;object&nbsp;&lt;genexpr&gt;&nbsp;at&nbsp;0x7faf6ee20a50&gt;&nbsp;# \u8fd4\u56de\u7684\u662f\u751f\u6210\u5668\u5bf9\u8c61<\/p>\n\n\n\n<p>&gt;&gt;&gt;&nbsp;tuple(a)&nbsp;&nbsp;# \u4f7f\u7528 tuple() \u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u751f\u6210\u5668\u5bf9\u8c61\u8f6c\u6362\u6210\u5143\u7ec4<br>(1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5,&nbsp;6,&nbsp;7,&nbsp;8,&nbsp;9)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u72ec\u7279\u7684\u6570\u636e\u5904\u7406\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4ece\u4e00\u4e2a\u6570\u636e\u5e8f\u5217\u6784\u5efa&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":487,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-570","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-1"],"views":596,"_links":{"self":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/comments?post=570"}],"version-history":[{"count":1,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/570\/revisions"}],"predecessor-version":[{"id":571,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/570\/revisions\/571"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/media\/487"}],"wp:attachment":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/media?parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/categories?post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/tags?post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}