{"id":783,"date":"2024-10-06T05:44:37","date_gmt":"2024-10-06T05:44:37","guid":{"rendered":"https:\/\/blue16.cn\/?p=783"},"modified":"2024-10-06T07:23:03","modified_gmt":"2024-10-06T07:23:03","slug":"python%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0-%e4%b8%80%e4%ba%9b%e6%9c%89%e8%b6%a3%e7%9a%84%e8%af%ad%e6%b3%95%e6%90%9c%e9%9b%86","status":"publish","type":"post","link":"https:\/\/blue16.cn\/index.php\/2024\/10\/06\/783\/","title":{"rendered":"Python\u5217\u8868\u63a8\u5bfc\u5f0f"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u5b9a\u4e49<\/h2>\n\n\n\n<p>\u5728Python\u4e2d\uff0c\u63a8\u5bfc\u5f0f\uff08comprehensions\uff09\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u548c\u751f\u6210\u5668\u3002\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u8ba9\u6211\u4eec\u7528\u4e00\u884c\u4ee3\u7801\u5c31\u80fd\u5b8c\u6210\u5728\u8fed\u4ee3\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u548c\u8f6c\u6362\u6570\u636e\u7ed3\u6784\u7684\u4efb\u52a1\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5217\u8868\u63a8\u5bfc\u5f0f\uff08List Comprehensions\uff09<\/h3>\n\n\n\n<p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u6700\u5e38\u89c1\u7684\u63a8\u5bfc\u5f0f\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5217\u8868\u8f6c\u6362\u6210\u53e6\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u57fa\u672c\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\nnumbers = &#91;1, 2, 3, 4, 5]\nsquared = &#91;x**2 for x in numbers]\nprint(squared)  # \u8f93\u51fa: &#91;1, 4, 9, 16, 25]\n# \u5e26\u6709\u6761\u4ef6\u7684\u5217\u8868\u63a8\u5bfc\u5f0f\neven_squares = &#91;x**2 for x in numbers if x % 2 == 0]\nprint(even_squares)  # \u8f93\u51fa: &#91;4, 16]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b57\u5178\u63a8\u5bfc\u5f0f\uff08Dictionary Comprehensions\uff09<\/h3>\n\n\n\n<p>\u5b57\u5178\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u521b\u5efa\u65b0\u7684\u5b57\u5178\uff0c\u5b83\u901a\u5e38\u7528\u6765\u8f6c\u6362\u952e\u548c\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5b57\u5178\u63a8\u5bfc\u5f0f\noriginal_dict = {'a': 1, 'b': 2, 'c': 3}\nsquared_dict = {k: v**2 for k, v in original_dict.items()}\nprint(squared_dict)  # \u8f93\u51fa: {'a': 1, 'b': 4, 'c': 9}\n# \u5e26\u6761\u4ef6\u7684\u5b57\u5178\u63a8\u5bfc\u5f0f\nfiltered_dict = {k: v for k, v in original_dict.items() if v % 2 == 0}\nprint(filtered_dict)  # \u8f93\u51fa: {'b': 2}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u96c6\u5408\u63a8\u5bfc\u5f0f\uff08Set Comprehensions\uff09<\/h3>\n\n\n\n<p>\u96c6\u5408\u63a8\u5bfc\u5f0f\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u662f\u5b83\u751f\u6210\u7684\u662f\u96c6\u5408\uff0c\u56e0\u6b64\u4e0d\u4f1a\u6709\u91cd\u590d\u7684\u5143\u7d20\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u96c6\u5408\u63a8\u5bfc\u5f0f\nnumbers = &#91;1, 2, 2, 3, 4, 4, 5]\nunique_squares = {x**2 for x in numbers}\nprint(unique_squares)  # \u8f93\u51fa: {1, 4, 9, 16, 25}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u751f\u6210\u5668\u63a8\u5bfc\u5f0f\uff08Generator Comprehensions\uff09<\/h3>\n\n\n\n<p>\u751f\u6210\u5668\u63a8\u5bfc\u5f0f\u548c\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u662f\u5b83\u4f7f\u7528\u5706\u62ec\u53f7\u800c\u4e0d\u662f\u65b9\u62ec\u53f7\uff0c\u5b83\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u751f\u6210\u5668\u63a8\u5bfc\u5f0f\nnumbers = &#91;1, 2, 3, 4, 5]\nsquared_gen = (x**2 for x in numbers)\nfor square in squared_gen:\n    print(square)  # \u8f93\u51fa: 1 4 9 16 25<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8bb0\u5fc6\u65b9\u6cd5<\/h3>\n\n\n\n<p>for i in range(n,2)\uff0c\u8fd9\u662f\u4e00\u4e2a\u5faa\u73af\uff0c\u524d\u9762\u52a0\u4e0a\u8d4b\u503c\u7684\u5143\u7d20\uff0c\u6bd4\u5982i**2\uff0c\u540e\u9762\u52a0\u4e0a\u6761\u4ef6\uff0c\u4f8b\u5982i%2==1\uff0c\u7136\u540e\u7528\u4ec0\u4e48\u62ec\u8d77\u6765\uff08\u5217\u8868\u3001\u5b57\u5178\u3001\u751f\u6210\u5668\uff09\uff0c\u5c31\u662f\u4ec0\u4e48\u63a8\u5bfc\u5f0f\u3002\u63a8\u5bfc\u5f0f\u7684\u76ee\u7684\u662f\u751f\u6210\u5217\u8868\u3001\u5b57\u5178\uff0c\u6216\u8005\u751f\u6210\u751f\u6210\u5668\u7136\u540e\u518d\u8c03\u7528\u5faa\u73af\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9a\u4e49 \u5728Python\u4e2d\uff0c\u63a8\u5bfc\u5f0f\uff08comprehensions\uff09\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u5217\u8868\u3001\u5b57\u5178\u3001\u96c6\u5408\u548c\u751f\u6210\u5668\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41,26,27],"tags":[],"class_list":["post-783","post","type-post","status-publish","format-standard","hentry","category-python","category-study","category-study_note"],"_links":{"self":[{"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/posts\/783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/comments?post=783"}],"version-history":[{"count":0,"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/posts\/783\/revisions"}],"wp:attachment":[{"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/media?parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/categories?post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue16.cn\/index.php\/wp-json\/wp\/v2\/tags?post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}