{"id":431,"date":"2019-01-05T20:09:54","date_gmt":"2019-01-05T11:09:54","guid":{"rendered":"https:\/\/www.nblog09.com\/w\/?p=431"},"modified":"2021-06-12T17:51:37","modified_gmt":"2021-06-12T08:51:37","slug":"python-lambda","status":"publish","type":"post","link":"https:\/\/www.nblog09.com\/w\/2019\/01\/05\/python-lambda\/","title":{"rendered":"Python\u306e\u9ad8\u968e\u95a2\u6570\u3068lambda\u5f0f\u306e\u4f7f\u3044\u65b9\u3092\u307e\u3068\u3081\u307e\u3057\u305f"},"content":{"rendered":"<h2 class=\"title-border\"><strong>\u9ad8\u968e\u95a2\u6570\u306e\u4f7f\u3044\u65b9<\/strong><\/h2>\n<p>python\u3067\u306f\u3001\u95a2\u6570\u3082\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e1\u3064\u306b\u3067\u3042\u308b\u305f\u3081\u5909\u6570\u3068\u3057\u3066\u6271\u3046\u3053\u3068\u3082\u3067\u304d\u308b\u3002<br \/>\n\u307e\u305f\u3001\u95a2\u6570\u3092\u4ed6\u306e\u95a2\u6570\u306e\u5f15\u6570\u3068\u3057\u3066\u6e21\u3057\u305f\u308a\u3001\u8fd4\u308a\u5024\u3068\u3057\u3066\u6271\u3046\u3053\u3068\u3082\u3067\u304d\u308b\u3002<\/p>\n<p>\u95a2\u6570\u3092\u5f15\u6570\u306b\u3057\u305f\u308a\u3001\u8fd4\u308a\u5024\u306b\u3059\u308b\u95a2\u6570\u3092\u9ad8\u968e\u95a2\u6570\u3068\u8a00\u3046\u3002<\/p>\n<div class=\"programming-gray\">\n<ul>\n<li>\u95a2\u6570\u3092\u5909\u6570\u306e\u3088\u3046\u306b\u6271\u3046\u4f8b<\/li>\n<\/ul>\n<div class=\"programming-inside\">\ndef print_hello():<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;print(\"hello\") <\/p>\n<p>var = print_hello <strong># \u5909\u6570var\u306b\u95a2\u6570\u3092\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u4ee3\u5165<\/strong><br \/>\nvar() <strong># \u3053\u306e\u3088\u3046\u306b\u5909\u6570\u306b()\u3092\u4ed8\u3051\u308b\u3068\u95a2\u6570\u304c\u547c\u3073\u51fa\u3055\u308c\u3001\u300chello\u300d\u3068\u8868\u793a\u3055\u308c\u308b<\/strong><\/p>\n<p>var2 = ['apple', 'orange', print_hello] <strong># \u914d\u5217\u306e3\u756a\u76ee\uff08\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3060\u30682\uff09\u306bprint_hello\u3092\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u3001\u5165\u308c\u308b<\/strong><br \/>\nvar2[2]() <strong>#\u2191\u914d\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b92\u306eprint_hello\u3092\u547c\u3073\u51fa\u3059\u3068\u3001\u300chello\u300d\u3068\u8868\u793a\u3055\u308c\u308b<\/strong>\n<\/div>\n<ul>\n<li>\u5f15\u6570\u3001\u8fd4\u308a\u5024\u306b\u95a2\u6570\u3092\u5229\u7528\u3059\u308b\u65b9\u6cd5<\/li>\n<\/ul>\n<div class=\"programming-inside\">\ndef print_world(msg):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;print(\"{} world\".format(msg))<\/p>\n<p>def print_konnichiwa():<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;print(\"\u3053\u3093\u306b\u3061\u306f\")<\/p>\n<p>def print_hello(func):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"codehighlight\">func(\"hello\")<\/span> <strong>#\u5f15\u6570func\u3092\u95a2\u6570\u306e\u3088\u3046\u306b\u5229\u7528<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"codehighlight\">return print_konnichiwa<\/span> <strong># \u95a2\u6570print_konnichiwa\u3092\u8fd4\u3059<\/strong><\/p>\n<p>var = print_world <strong># \u5909\u6570var\u306b\u95a2\u6570print_world\u3092\u4ee3\u5165<\/strong> <\/p>\n<p>a = print_hello(var)<br \/>\n<strong># \u95a2\u6570print_hello\u306e\u5f15\u6570\u3092print_world\u306b\u3057\u3066\u3001\u547c\u3073\u51fa\u3059<br \/>\n# func(\"hello\")\u304cprint_world(\"hello\")\u306b\u306a\u308a\u3001\u95a2\u6570\u5185\u3067\u300c\"hello world\"\u300d\u3068\u8868\u793a<br \/>\n# a\u306b\u3001\u95a2\u6570print_konnichiwa\u3092\u683c\u7d0d<\/strong><\/p>\n<p>a() <strong># \u95a2\u6570print_konnichiwa()\u304c\u5b9f\u884c\u3055\u308c\u3001\"\u3053\u3093\u306b\u3061\u306f\"\u3068\u8868\u793a<\/strong>\n<\/div>\n<\/div>\n<h2 class=\"title-border\"><strong>lambda\u5f0f\u306e\u4f7f\u3044\u65b9<\/strong><\/h2>\n<p>python\u306e\u30671\u884c\u3067\u7d42\u308f\u308b\u3088\u3046\u306a\u95a2\u6570\u3092\u5b9a\u7fa9\u3059\u308b\u5834\u5408\u306b\u3001lambda\u5f0f\u3092\u7528\u3044\u308b\u3053\u3068\u304c\u591a\u3044\u3002<br \/>\nlambda\u5f0f\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3059\u308b\u3002<\/p>\n<p class=\"pink_span\">lambda \u5f15\u6570: \u8fd4\u308a\u5024<\/p>\n<div class=\"programming-gray\">\n<ul>\n<li>lambda\u306e\u8a18\u8ff0\u65b9\u6cd5<\/li>\n<\/ul>\n<div class=\"programming-inside\">\na = lambda x: x * x <strong># a\u306b\u7121\u540d\u95a2\u6570\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4ee3\u5165(\u5f15\u6570x\u3067\u8fd4\u308a\u5024x*x\u306e\u95a2\u6570)<\/strong><\/p>\n<p>res = a(12) <strong># \u5f15\u6570\u309212\u3068\u3057\u3066\u3001\u7121\u540d\u95a2\u6570\u3092\u5b9f\u884c\u3002\u623b\u308a\u5024\u306f\u3001x * x\u306e144<\/strong><\/p>\n<p>print(res) <strong># 144<\/strong><\/p>\n<p>b = lambda x, y, z=5: x * y *z <strong># lambda\u5f0f\u3067\u3001\u8907\u6570\u306e\u5f15\u6570\u3092\u53d6\u308a\u3001\u4e00\u90e8\u306e\u5f15\u6570\u306b\u30c7\u30d5\u30a9\u30eb\u30c8\u5024\u3092\u5165\u308c\u308b<\/strong><\/p>\n<p>print(b(3,4)) <strong># x=3,y=4\u3068\u3057\u3066\u95a2\u6570\u304c\u5b9f\u884c\u3055\u308c\u300160\u3068\u8868\u793a<\/strong>\n<\/div>\n<ul>\n<li>lambda\u3067\u6761\u4ef6\u5f0f\u3092\u8a18\u8ff0\u3059\u308b\u65b9\u6cd5<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nc = lambda x, y: y if x &lt; y else x<br \/>\n<strong># x, y\u3092\u5f15\u6570\u3068\u3057\u3066\u3001x &lt; y\u306e\u3068\u304dy\u3001\u305d\u308c\u4ee5\u5916\u3067\u306f\u3001x\u3092\u8fd4\u3059<\/strong>\n<\/div>\n<\/div>\n<h2 class=\"title-border\"><strong>map\u95a2\u6570\u3001sort\u95a2\u6570\u3068\u4f75\u7528\u3057\u3066lambda\u5f0f\u3092\u5229\u7528\u3059\u308b<\/strong><\/h2>\n<div class=\"programming-gray\">\n<ul>\n<li>map, sort\u3068lambda\u3092\u5229\u7528\u3059\u308b\u65b9\u6cd5<\/li>\n<\/ul>\n<div class=\"programming-inside\">\na = [1, 2, 3, 4]<\/p>\n<p>b = map(lambda x: x * x, a) <strong># b\u306b\u306f\u3001a\u3092a*a\u3057\u305f\u5024\u3001(1,4,9,16)\u306eMapObject\u304c\u683c\u7d0d\u3055\u308c\u308b<\/strong><\/p>\n<p>c = [(\"apple\", \"200yen\"), (\"grape\", \"400yen\"), (\"banana\", \"100yen\"), (\"lemon\", \"50yen\")] <\/p>\n<p>c.sort(key=lambda x: x[1]) <strong># c\u30922\u756a\u76ee\u306e\u5024\u3092\u30ad\u30fc\u306b\u3057\u3066\u30bd\u30fc\u30c8<\/strong><\/p>\n<p>c.sort(key=lambda x: x[0][0] + x[1][0]) <strong># c\u306e1\u756a\u76ee\u306e1\u756a\u76ee\u306e\u5024+2\u756a\u76ee\u306e1\u756a\u76ee\u306e\u5024a2,g4,b1,l5\u3067\u3001sort<\/strong>\n<\/div>\n<\/div>\n<div class=\"customized-adsense\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u9ad8\u968e\u95a2\u6570\u306e\u4f7f\u3044\u65b9 python\u3067\u306f\u3001\u95a2\u6570\u3082\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e1\u3064\u306b\u3067\u3042\u308b\u305f\u3081\u5909\u6570\u3068\u3057\u3066\u6271\u3046\u3053\u3068\u3082\u3067\u304d\u308b\u3002 \u307e\u305f\u3001\u95a2\u6570\u3092\u4ed6\u306e\u95a2\u6570\u306e\u5f15\u6570\u3068\u3057\u3066\u6e21\u3057\u305f\u308a\u3001\u8fd4\u308a\u5024\u3068\u3057\u3066\u6271\u3046\u3053\u3068\u3082\u3067\u304d\u308b\u3002 \u95a2\u6570\u3092\u5f15\u6570\u306b\u3057\u305f\u308a\u3001\u8fd4\u308a\u5024\u306b\u3059\u308b\u95a2\u6570\u3092\u9ad8\u968e\u95a2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":432,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_uag_custom_page_level_css":"","_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[9,219],"tags":[96,14,97],"class_list":{"0":"post-431","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python","8":"category-it-programming","9":"tag-lambda","10":"tag-python","11":"tag-97","12":"entry"},"uagb_featured_image_src":{"full":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/plumbing-840835__480.jpg",720,480,false],"thumbnail":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/plumbing-840835__480-150x150.jpg",150,150,true],"medium":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/plumbing-840835__480-300x200.jpg",300,200,true],"medium_large":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/plumbing-840835__480.jpg",720,480,false],"large":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/plumbing-840835__480.jpg",720,480,false],"1536x1536":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/plumbing-840835__480.jpg",720,480,false],"2048x2048":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/plumbing-840835__480.jpg",720,480,false],"home-middle":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/plumbing-840835__480.jpg",525,350,false],"home-top":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/plumbing-840835__480.jpg",630,420,false],"sidebar-thumbnail":["https:\/\/www.nblog09.com\/w\/wp-content\/uploads\/2019\/01\/plumbing-840835__480.jpg",100,67,false]},"uagb_author_info":{"display_name":"9nmatsumoto","author_link":"https:\/\/www.nblog09.com\/w\/author\/9nmatsumoto\/"},"uagb_comment_info":0,"uagb_excerpt":"\u9ad8\u968e\u95a2\u6570\u306e\u4f7f\u3044\u65b9 python\u3067\u306f\u3001\u95a2\u6570\u3082\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e1\u3064\u306b\u3067\u3042\u308b\u305f\u3081\u5909\u6570\u3068\u3057\u3066\u6271\u3046\u3053\u3068\u3082\u3067\u304d\u308b\u3002 \u307e\u305f\u3001\u95a2&hellip;","_links":{"self":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/comments?post=431"}],"version-history":[{"count":16,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/431\/revisions"}],"predecessor-version":[{"id":2873,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/431\/revisions\/2873"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/media\/432"}],"wp:attachment":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/media?parent=431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/categories?post=431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/tags?post=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}