{"id":1525,"date":"2014-05-06T11:53:42","date_gmt":"2014-05-06T15:53:42","guid":{"rendered":"http:\/\/www.a2life.info\/w\/?p=1525"},"modified":"2014-05-08T19:33:43","modified_gmt":"2014-05-08T23:33:43","slug":"python%e3%80%81comprehension-%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%9f%e4%b8%80%e6%8b%ac%e5%87%a6%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.a2life.info\/w\/?p=1525","title":{"rendered":"Python\u3001comprehension \u3092\u4f7f\u3063\u305f\u4e00\u62ec\u51e6\u7406"},"content":{"rendered":"<p>Python\u306b\u306f&#8221; \u2026 for \u2026 in&#8221;\u3068\u3044\u3046\u69cb\u6587\u3092\u4f7f\u3063\u305f\u3001\u4e00\u62ec\u3067\u30e1\u30f3\u30d0\u30fc\u8981\u7d20\u3092\u51e6\u7406\u3059\u308b\u6a5f\u80fd\u304c\u3042\u308b\u3002Comprehension\u3068\u547c\u3070\u308c\u3001\u30ea\u30b9\u30c8\u3001\u30bb\u30c3\u30c8\u3001\u8f9e\u66f8\u306b\u4f7f\u3048\u308b\u3002<br \/>\n\u30ea\u30b9\u30c8\u306e\u4f7f\u7528\u4f8b<\/p>\n<pre>\r\n>>> a_list =list(range(10))\r\n>>> a_list\r\n[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\r\n>>> a_list = [x**2 for x in a_list]\u3000\u3000--List comprehension\r\n>>> a_list\r\n[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\r\n<\/pre>\n<p>If \u3092\u4f7f\u3044\u3001\u8981\u7d20\u306eFiltering\u3092\u3057\u3066\u304b\u3089\u51e6\u7406\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u308b\u3002\u4e0b\u306f\u30ea\u30b9\u30c8\u4e2d\u3001\u5076\u6570\u3060\u3051\u3092\u51e6\u7406\u3057\u3066\u65b0\u3057\u3044\u30ea\u30b9\u30c8\u3092\u4f5c\u6210\u3057\u305f\u4f8b\u3002\u3000<\/p>\n<pre>\r\n>>> a_list = list(range(10))\r\n>>> a_list = [x**2 for x in a_list if (x%2 ==0)]\r\n>>> a_list\r\n[0, 4, 16, 36, 64]\r\n<\/pre>\n<p>Python Dictionary \u306e\u3000\u30ad\u30fc\u3068\u5024\u3092\u30b9\u30ef\u30c3\u30d7\u3057\u3066\u65b0\u3057\u3044\u8f9e\u66f8\u3092\u4f5c\u308b\u3002<\/p>\n<pre>\r\n>>> week_dict={'Mon':'\u6708','Tue':'\u706b','Wed':'\u6c34','Thu':'\u6728','Fri':'\u91d1','Sat':'\u571f','Sun':'\u65e5'}\r\n>>> youbi_dict={value:key for key,value in week_dict.items()}\r\n>>> youbi_dict\r\n{'\u6728': 'Thu', '\u571f': 'Sat', '\u6708': 'Mon', '\u706b': 'Tue', '\u6c34': 'Wed', '\u65e5': 'Sun', '\u91d1': 'Fri'}\r\n>>> week_dict\r\n{'Mon': '\u6708', 'Fri': '\u91d1', 'Tue': '\u706b', 'Sat': '\u571f', 'Sun': '\u65e5', 'Wed': '\u6c34', 'Thu': '\u6728'}\r\n>>> week_dict['Fri']\r\n'\u91d1'\r\n>>> youbi_dict['\u65e5']\r\n'Sun'\r\n<\/pre>\n<p>\u8f9e\u66f8\u306f\u9806\u756a\u306f\u30ad\u30fc\u30d7\u3057\u306a\u3044\u306e\u3060\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u306b\u306f&#8221; \u2026 for \u2026 in&#8221;\u3068\u3044\u3046\u69cb\u6587\u3092\u4f7f\u3063\u305f\u3001\u4e00\u62ec\u3067\u30e1\u30f3\u30d0\u30fc\u8981\u7d20\u3092\u51e6\u7406\u3059\u308b\u6a5f\u80fd\u304c\u3042\u308b\u3002Comprehension\u3068\u547c\u3070\u308c\u3001\u30ea\u30b9\u30c8\u3001\u30bb\u30c3\u30c8\u3001\u8f9e\u66f8\u306b\u4f7f\u3048\u308b\u3002 \u30ea\u30b9\u30c8\u306e\u4f7f\u7528\u4f8b >>> a_list =list(range(10)) >>> a_list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> a_list = [x**2 for x in a_list]\u3000\u3000&#8211;List comprehension >>> a_list [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] If \u3092\u4f7f\u3044\u3001\u8981\u7d20\u306eFiltering\u3092\u3057\u3066\u304b\u3089\u51e6\u7406\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u308b\u3002\u4e0b\u306f\u30ea\u30b9\u30c8\u4e2d\u3001\u5076\u6570\u3060\u3051\u3092\u51e6\u7406\u3057\u3066\u65b0\u3057\u3044\u30ea\u30b9\u30c8\u3092\u4f5c\u6210\u3057\u305f\u4f8b\u3002\u3000 >>> a_list = list(range(10)) >>> a_list = [x**2 for x in a_list if (x%2 ==0)] >>> a_list [0, 4, 16, 36, 64] Python Dictionary \u306e\u3000\u30ad\u30fc\u3068\u5024\u3092\u30b9\u30ef\u30c3\u30d7\u3057\u3066\u65b0\u3057\u3044\u8f9e\u66f8\u3092\u4f5c\u308b\u3002 >>> week_dict={&#8216;Mon&#8217;:&#8217;\u6708&#8217;,&#8217;Tue&#8217;:&#8217;\u706b&#8217;,&#8217;Wed&#8217;:&#8217;\u6c34&#8217;,&#8217;Thu&#8217;:&#8217;\u6728&#8217;,&#8217;Fri&#8217;:&#8217;\u91d1&#8217;,&#8217;Sat&#8217;:&#8217;\u571f&#8217;,&#8217;Sun&#8217;:&#8217;\u65e5&#8217;} >>> youbi_dict={value:key for key,value in week_dict.items()} >>> youbi_dict {&#8216;\u6728&#8217;: &#8216;Thu&#8217;, &#8216;\u571f&#8217;: &#8216;Sat&#8217;, &#8216;\u6708&#8217;: &#8216;Mon&#8217;, &#8216;\u706b&#8217;: &#8216;Tue&#8217;, &#8216;\u6c34&#8217;: &#8216;Wed&#8217;, &#8216;\u65e5&#8217;: &#8216;Sun&#8217;, &#8216;\u91d1&#8217;: &#8216;Fri&#8217;} >>> week_dict {&#8216;Mon&#8217;: &#8216;\u6708&#8217;, &#8216;Fri&#8217;: &#8216;\u91d1&#8217;, &#8216;Tue&#8217;: &#8216;\u706b&#8217;, &#8216;Sat&#8217;: &#8216;\u571f&#8217;, &#8216;Sun&#8217;: &#8216;\u65e5&#8217;, &#8216;Wed&#8217;: &#8216;\u6c34&#8217;, &#8216;Thu&#8217;: &#8216;\u6728&#8217;} >>> &hellip; <a href=\"https:\/\/www.a2life.info\/w\/?p=1525\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[41,49],"class_list":["post-1525","post","type-post","status-publish","format-standard","hentry","category-programming","tag-python","tag-python3"],"_links":{"self":[{"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/posts\/1525","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1525"}],"version-history":[{"count":4,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/posts\/1525\/revisions"}],"predecessor-version":[{"id":1529,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/posts\/1525\/revisions\/1529"}],"wp:attachment":[{"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}