﻿{"id":1192,"date":"2020-06-14T10:24:20","date_gmt":"2020-06-14T02:24:20","guid":{"rendered":"http:\/\/www.tansoo.cn\/?p=1192"},"modified":"2020-06-14T12:17:04","modified_gmt":"2020-06-14T04:17:04","slug":"python%e5%88%97%e8%a1%a8%e6%8e%a8%e5%af%bc%e5%bc%8f%ef%bc%88list-comprehensions%ef%bc%89","status":"publish","type":"post","link":"http:\/\/www.tansoo.cn\/?p=1192","title":{"rendered":"python\u5217\u8868\u63a8\u5bfc\u5f0f\uff08list comprehensions\uff09"},"content":{"rendered":"<p><strong class=\"text-strong\">\u5217\u8868\u63a8\u5bfc\u5f0f\uff08list comprehensions\uff09<\/strong><br \/>\n\u5217\u8868\u63a8\u5bfc\u5f0f\uff08\u53c8\u79f0\u5217\u8868\u89e3\u6790\u5f0f\uff09\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u660e\u627c\u8981\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u5217\u8868\u3002<br \/>\n\u5b83\u7684\u7ed3\u6784\u662f\u5728\u4e00\u4e2a\u4e2d\u62ec\u53f7\u91cc\u5305\u542b\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u7136\u540e\u662f\u4e00\u4e2afor\u8bed\u53e5\uff0c\u7136\u540e\u662f0\u4e2a\u6216\u591a\u4e2afor\u6216\u8005if\u8bed\u53e5\u3002\u90a3\u4e2a\u8868\u8fbe\u5f0f\u53ef\u4ee5\u662f\u4efb\u610f\u7684\uff0c\u610f\u601d\u662f\u4f60\u53ef\u4ee5\u5728\u5217\u8868\u4e2d\u653e\u5165\u4efb\u610f\u7c7b\u578b\u7684\u5bf9\u8c61\u3002\u8fd4\u56de\u7ed3\u679c\u5c06\u662f\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5728\u8fd9\u4e2a\u4ee5if\u548cfor\u8bed\u53e5\u4e3a\u4e0a\u4e0b\u6587\u7684\u8868\u8fbe\u5f0f\u8fd0\u884c\u5b8c\u6210\u4e4b\u540e\u4ea7\u751f\u3002<\/p>\n<p><!--more-->\u89c4\u8303<\/p>\n<div class=\"codebox\" title=\"CODEBOX_PLUS_TITLE\" data-language=\"python\" data-filename=\"Untitled.py\">\n<p>\u4ee3\u7801:<\/p>\n<p><code>variable = [out_exp for out_exp in input_list if out_exp == 2]<\/code><\/p>\n<\/div>\n<p>\u8fd9\u91cc\u662f\u53e6\u5916\u4e00\u4e2a\u7b80\u660e\u4f8b\u5b50:<\/p>\n<div class=\"codebox\" title=\"CODEBOX_PLUS_TITLE\" data-language=\"python\" data-filename=\"Untitled.py\">\n<p>\u4ee3\u7801:<\/p>\n<p><code>multiples = [i for i in range(30) if i % 3 is 0]<br \/>\nprint(multiples)<br \/>\n# Output: [0, 3, 6, 9, 12, 15, 18, 21, 24, 27]<\/code><\/p>\n<\/div>\n<p>\u8fd9\u5c06\u5bf9\u5feb\u901f\u751f\u6210\u5217\u8868\u975e\u5e38\u6709\u7528\u3002<br \/>\n\u6709\u4e9b\u4eba\u751a\u81f3\u66f4\u559c\u6b22\u4f7f\u7528\u5b83\u800c\u4e0d\u662ffilter\u51fd\u6570\u3002<br \/>\n\u5217\u8868\u63a8\u5bfc\u5f0f\u5728\u6709\u4e9b\u60c5\u51b5\u4e0b\u8d85\u8d5e\uff0c\u7279\u522b\u662f\u5f53\u4f60\u9700\u8981\u4f7f\u7528for\u5faa\u73af\u6765\u751f\u6210\u4e00\u4e2a\u65b0\u5217\u8868\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4f60\u901a\u5e38\u4f1a\u8fd9\u6837\u505a\uff1a<\/p>\n<div class=\"codebox\" title=\"CODEBOX_PLUS_TITLE\" data-language=\"python\" data-filename=\"Untitled.py\">\n<p>\u4ee3\u7801:<\/p>\n<p><code>squared = []<br \/>\nfor x in range(10):<br \/>\nsquared.append(x**2)<\/code><\/p>\n<\/div>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u7b80\u5316\u5b83\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p>\n<div class=\"codebox\" title=\"CODEBOX_PLUS_TITLE\" data-language=\"python\" data-filename=\"Untitled.py\">\n<p>\u4ee3\u7801:<\/p>\n<p><code>squared = [x**2 for x in range(10)]<\/code><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5217\u8868\u63a8\u5bfc\u5f0f\uff08list comprehensions\uff09 \u5217\u8868\u63a8\u5bfc\u5f0f\uff08\u53c8\u79f0\u5217\u8868\u89e3\u6790\u5f0f\uff09\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u660e\u627c\u8981\u7684\u65b9\u6cd5\u6765\u521b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[70],"class_list":["post-1192","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-python"],"_links":{"self":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1192"}],"version-history":[{"count":3,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1192\/revisions"}],"predecessor-version":[{"id":1195,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1192\/revisions\/1195"}],"wp:attachment":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1192"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}