﻿{"id":1471,"date":"2020-06-10T12:18:09","date_gmt":"2020-06-10T04:18:09","guid":{"rendered":"https:\/\/www.tansoo.cn\/?p=1471"},"modified":"2020-06-10T12:18:09","modified_gmt":"2020-06-10T04:18:09","slug":"phpbb-31","status":"publish","type":"post","link":"https:\/\/www.tansoo.cn\/?p=1471","title":{"rendered":"python\u5217\u8868\u63a8\u5bfc\u5f0f\uff08list comprehensions\uff09"},"content":{"rendered":"<p><span style=\"font-size:150%;line-height:116%\"><strong class=\"text-strong\">\u5217\u8868\u63a8\u5bfc\u5f0f\uff08list comprehensions\uff09<\/strong><\/span><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>\u89c4\u8303<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"python\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">variable <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: black;\">&#91;<\/span>out_exp <span style=\"color: #ff7700;font-weight:bold;\">for<\/span> out_exp <span style=\"color: #ff7700;font-weight:bold;\">in<\/span> input_list <span style=\"color: #ff7700;font-weight:bold;\">if<\/span> out_exp <span style=\"color: #66cc66;\">==<\/span> <span style=\"color: #ff4500;\">2<\/span><span style=\"color: black;\">&#93;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p>\u8fd9\u91cc\u662f\u53e6\u5916\u4e00\u4e2a\u7b80\u660e\u4f8b\u5b50:<\/p>\n<div class=\"codebox_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"python\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">multiples <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: black;\">&#91;<\/span>i <span style=\"color: #ff7700;font-weight:bold;\">for<\/span> i <span style=\"color: #ff7700;font-weight:bold;\">in<\/span> <span style=\"color: #008000;\">range<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #ff4500;\">30<\/span><span style=\"color: black;\">&#41;<\/span> <span style=\"color: #ff7700;font-weight:bold;\">if<\/span> i % <span style=\"color: #ff4500;\">3<\/span> <span style=\"color: #ff7700;font-weight:bold;\">is<\/span> <span style=\"color: #ff4500;\">0<\/span><span style=\"color: black;\">&#93;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">print<\/span><span style=\"color: black;\">&#40;<\/span>multiples<span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #808080; font-style: italic;\"># Output: [0, 3, 6, 9, 12, 15, 18, 21, 24, 27]<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\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_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"python\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">squared <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: black;\">&#91;<\/span><span style=\"color: black;\">&#93;<\/span><\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\"><span style=\"color: #ff7700;font-weight:bold;\">for<\/span> x <span style=\"color: #ff7700;font-weight:bold;\">in<\/span> <span style=\"color: #008000;\">range<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #ff4500;\">10<\/span><span style=\"color: black;\">&#41;<\/span>:<\/div>\n<\/li>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">&nbsp; &nbsp; squared.<span style=\"color: black;\">append<\/span><span style=\"color: black;\">&#40;<\/span>x**<span style=\"color: #ff4500;\">2<\/span><span style=\"color: black;\">&#41;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\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_plus_wrap\">\n<div class=\"codebox_plus_header\"><strong>Code: <\/strong><a href=\"#\" onclick=\"codebox_plus_select(this, 1); return false;\">[\u5168\u9009]<\/a>&nbsp;<a href=\"#\" onclick=\"codebox_plus_toggle(this, 1); return false;\">[Expand\/Collapse]<\/a><\/div>\n<div>\n<div style=\"display: inline;\">\n<div class=\"python\" style=\"font-family:monospace;\">\n<ol>\n<li style=\"margin-left:20px;\">\n<div style=\"border-bottom: dotted 1px #cccccc;\">squared <span style=\"color: #66cc66;\">=<\/span> <span style=\"color: black;\">&#91;<\/span>x**<span style=\"color: #ff4500;\">2<\/span> <span style=\"color: #ff7700;font-weight:bold;\">for<\/span> x <span style=\"color: #ff7700;font-weight:bold;\">in<\/span> <span style=\"color: #008000;\">range<\/span><span style=\"color: black;\">&#40;<\/span><span style=\"color: #ff4500;\">10<\/span><span style=\"color: black;\">&#41;<\/span><span style=\"color: black;\">&#93;<\/span><\/div>\n<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"codebox_plus_footer\">GeSHi &copy; Codebox Plus Extension<\/div>\n<\/div>\n<p><small>\u539f\u6587\u4f5c\u8005: BG6RSH | <a href=\"https:\/\/bbs.tansoo.cn\/viewtopic.php?t=31\" target=\"_blank\" rel=\"noopener\">\u67e5\u770b\u539f\u5e16<\/a><\/small><\/p>\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 &hellip; <a href=\"https:\/\/www.tansoo.cn\/?p=1471\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[],"class_list":["post-1471","post","type-post","status-publish","format-standard","hentry","category-105"],"_links":{"self":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1471"}],"version-history":[{"count":0,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1471\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}