{"id":3126,"date":"2018-12-28T20:38:00","date_gmt":"2018-12-28T12:38:00","guid":{"rendered":"https:\/\/shanlin.info\/?p=3126"},"modified":"2021-04-25T20:39:40","modified_gmt":"2021-04-25T12:39:40","slug":"python%e5%a6%82%e4%bd%95%e4%b8%80%e8%a1%8c%e5%86%99%e5%ae%8cif-elif-else%e5%88%97%e8%a1%a8%e6%8e%a8%e5%af%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/shanlin.info\/?p=3126","title":{"rendered":"Python\u5982\u4f55\u4e00\u884c\u5199\u5b8cif elif else\u5217\u8868\u63a8\u5bfc\u5f0f"},"content":{"rendered":"\n<p>Python\u4e2d\u7684if elif else\u7ed3\u6784\u901a\u5e38\u5982\u4e0b\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if cond1:\n    a=1\nelif cond2:\n    a=2\nelse:\n    a=3\n<\/code><\/pre>\n\n\n\n<p>\u5728\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u628a\u8fd9\u4e2a\u6761\u4ef6\u8bed\u53e5\u5199\u6210\u4e00\u884c\uff0c\u5982\uff08\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\uff09\u3002<br>\u7ed3\u8bba\u5c31\u662f\uff0c\u5c06\u4e0a\u8ff0\u7ed3\u6784\u6539\u4e3a\uff1a<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u975e\u5217\u8868\uff1a    \u7ed3\u679c\u7532 if \u6761\u4ef6\u7532 else \u7ed3\u679c\u4e59 if \u6761\u4ef6\u4e59 else \u7ed3\u679c\u4e19\n\u5217\u8868\u63a8\u5bfc\u5f0f\uff1a    &#91;\u7ed3\u679c\u7532 if \u6761\u4ef6\u7532 else \u7ed3\u679c\u4e59 if \u6761\u4ef6\u4e59 else \u7ed3\u679c\u4e19 for xx in list]\n\u5217\u8868\u63a8\u5bfc\u5f0f\u7b5b\u9009\u5305\u542b\u67d0\u4e2a\u503c\u7684\u6570\u636e\uff1a    &#91;\u7ed3\u679c\u7532 for xx in list if \u6761\u4ef6\u7532]\n<\/code><\/pre>\n\n\n\n<p>\u5982\u4e0b\u4f8b\uff1a<br><strong>\u5e0c\u671b\u5bf9\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u636e\u8fdb\u884c\u5224\u65ad\uff0c\u83b7\u53d6\u4e00\u4e2a\u8be5\u4e8c\u624b\u623f\u4fe1\u606f\u5c5e\u4e8e\u54ea\u79cd\u7c7b\u578b\u7684\u623f\u4ea7<\/strong>\uff1a\u5224\u65ad\u4f9d\u636e\u5c31\u662f\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u542b\u6709\u522b\u5885\u5219\u662f\u522b\u5885\uff0c\u5982\u679c\u542b\u6709\u8f66\u4f4d\u5373\u4e3a\u8f66\u4f4d\uff0c\u5176\u4ed6\u4e3a\u9ad8\u5c42\u3002<em><\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a='\u5927\u9f0e\u4e16\u7eaa\u6ee8\u6c5f | \u8f66\u4f4d | 39.09\u5e73\u7c73 | \u4e1c | \u65e0\u7535\u68af'\nb='\u6052\u5927\u57ce\u4e8c\u671f | 2\u5ba42\u5385 | 71.49\u5e73\u7c73 | \u4e1c\u5357 | \u7cbe\u88c5 | \u6709\u7535\u68af'\nc='\u534f\u4fe1\u5f69\u4e91\u6e561\u53f7 | \u53e0\u62fc\u522b\u5885 | 5\u5ba42\u5385 | 280.49\u5e73\u7c73 | \u5357 | \u7b80\u88c5 | \u65e0\u7535\u68af'\nd=&#91;'\u5927\u9f0e\u4e16\u7eaa\u6ee8\u6c5f | \u8f66\u4f4d | 39.09\u5e73\u7c73 | \u4e1c | \u65e0\u7535\u68af',\n      '\u6052\u5927\u57ce\u4e8c\u671f | 2\u5ba42\u5385 | 71.49\u5e73\u7c73 | \u4e1c\u5357 | \u7cbe\u88c5 | \u6709\u7535\u68af',\n      '\u534f\u4fe1\u5f69\u4e91\u6e561\u53f7 | \u53e0\u62fc\u522b\u5885 | 5\u5ba42\u5385 | 280.49\u5e73\u7c73 | \u5357 | \u7b80\u88c5 | \u65e0\u7535\u68af']\n\n#\u5b9e\u73b0\u4ee3\u7801\ntpye=\"\u522b\u5885\" if \"\u522b\u5885\" in a else \"\u8f66\u4f4d\" if \"\u8f66\u4f4d\" in a else \"\u9ad8\u5c42\"\n\n#\u7ed3\u679c\nIn &#91;177]: \"\u522b\u5885\" if \"\u522b\u5885\" in a else \"\u8f66\u4f4d\" if \"\u8f66\u4f4d\" in a else \"\u9ad8\u5c42\"\nOut&#91;177]: '\u8f66\u4f4d'\n\nIn &#91;178]: \"\u522b\u5885\" if \"\u522b\u5885\" in b else \"\u8f66\u4f4d\" if \"\u8f66\u4f4d\" in b else \"\u9ad8\u5c42\"\nOut&#91;178]: '\u9ad8\u5c42'\n\nIn &#91;179]: \"\u522b\u5885\" if \"\u522b\u5885\" in c else \"\u8f66\u4f4d\" if \"\u8f66\u4f4d\" in c else \"\u9ad8\u5c42\"\nOut&#91;179]: '\u522b\u5885'\n\n#\u5bf9\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\nIn &#91;180]: &#91;\"\u522b\u5885\" if \"\u522b\u5885\" in x else \"\u8f66\u4f4d\" if \"\u8f66\u4f4d\" in x else \"\u9ad8\u5c42\" for x in d]\nOut&#91;180]: &#91;'\u8f66\u4f4d', '\u9ad8\u5c42', '\u522b\u5885']\n\n#\u5bf9\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u7b5b\u9009\u53ea\u5305\u542b\u67d0\u4e2a\u8bcd\u7684\u7684\u6570\u636e\nIn &#91;181]: &#91;\"\u522b\u5885\" for x in d if \"\u522b\u5885\" in x]\nOut&#91;181]: &#91;'\u522b\u5885']<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684if elif else\u7ed3\u6784\u901a\u5e38\u5982\u4e0b\uff1a \u5728\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u628a\u8fd9\u4e2a\u6761\u4ef6\u8bed\u53e5\u5199\u6210\u4e00\u884c\uff0c\u5982\uff08<\/p><\/div>\n<div class=\"blog-btn\"><a href=\"https:\/\/shanlin.info\/?p=3126\" class=\"home-blog-btn\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts\/3126"}],"collection":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3126"}],"version-history":[{"count":1,"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts\/3126\/revisions"}],"predecessor-version":[{"id":3127,"href":"https:\/\/shanlin.info\/index.php?rest_route=\/wp\/v2\/posts\/3126\/revisions\/3127"}],"wp:attachment":[{"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shanlin.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}