{"id":2237,"date":"2023-05-07T07:31:03","date_gmt":"2023-05-06T23:31:03","guid":{"rendered":"http:\/\/cnliutz.uicp.io\/?p=2237"},"modified":"2023-05-07T07:43:09","modified_gmt":"2023-05-06T23:43:09","slug":"%e8%ae%a9%e4%bd%a0%e7%9a%84-python-%e4%bb%a3%e7%a0%81%e6%9b%b4%e5%8a%a0%e4%bc%98%e9%9b%85","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=2237","title":{"rendered":"\u8ba9\u4f60\u7684 Python \u4ee3\u7801\u66f4\u52a0\u4f18\u96c5"},"content":{"rendered":"\n<p> \u4f18\u8d28\u6559\u80b2\u9886\u57df\u521b\u4f5c\u800510\u4e2a\u5c0f\u6280\u5de7\u6539\u8fdb\u7684 Python \u4ee3\u7801\uff0c\u8ba9\u4f60\u7684\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u66f4\u52a0 Python \u5316\u3002<br>1. \u7528enumerate\u4ee3\u66ffrange\u5982\u679c\u4f60\u9700\u8981\u904d\u5386\u4e00\u4e2a\u5217\u8868\uff0c\u5e76\u4e14\u9700\u8981\u540c\u65f6\u83b7\u53d6\u7d22\u5f15\u548c\u5143\u7d20\uff0c\u5927\u591a\u6570\u60c5\u51b5\u53ef\u80fd\u4f1a\u4f7f\u7528 range(len(data)) \u7684\u8bed\u6cd5\u3002\u6bd4\u5982\u6211\u4eec\u8981\u904d\u5386\u4e00\u4e2a\u5217\u8868\uff0c\u627e\u5230\u6240\u6709\u7684\u8d1f\u6570\uff1a<br>data = [1, 3, -5, 7, 9, -11]   <br>for i in range(len(data)): <br>        if data[i] &lt; 0: <br>                print(f&#8221;\u7d22\u5f15\uff1a{   i}\uff0c\u5143\u7d20\uff1a{   data[i]}&#8221;)<br>\u8f93\u51fa\uff1a\u7d22\u5f15\uff1a2\uff0c\u5143\u7d20\uff1a-5\u7d22\u5f15\uff1a5\uff0c\u5143\u7d20\uff1a-11<br>\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u6709\u6548\uff0c\u4f46\u5982\u679c\u4f7f\u7528 Python \u5185\u7f6e\u7684 enumerate \u51fd\u6570\u4f1a\u66f4\u597d\uff0c\u8fd9\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de\u904d\u5386\u8fc7\u7a0b\u4e2d\u5f53\u524d\u7d22\u5f15\u548c\u5f53\u524d\u5143\u7d20\u503c\u4f5c\u4e3a\u4e00\u4e2a\u5143\u7ec4\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u8f93\u51fa\u7d22\u5f15\u548c\u503c\uff1a<br>data = [1, 3, -5, 7, 9, -11] <br>for i, v, in enumerate(data): <br>      if v &lt; 0:<br>              print(f&#8221;\u7d22\u5f15\uff1a{   i}\uff0c\u5143\u7d20\uff1a{   v}&#8221;)<br>\u8f93\u51fa\uff1a\u7d22\u5f15\uff1a2\uff0c\u5143\u7d20\uff1a-5\u7d22\u5f15\uff1a5\uff0c\u5143\u7d20\uff1a-11<br>2. \u6df1\u5165\u7406\u89e3list\uff0c\u66ff\u4ee3for\u5faa\u73af\uff0c\u5047\u5982\u6211\u4eec\u8981\u521b\u5efa\u4e00\u4e2a\u4ece0~9\u7684\u5e73\u65b9\u6570\u7684\u5217\u8868\uff0c\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u5148\u521b\u5efa\u4e00\u4e2a list \uff0c\u7136\u540e\u901a\u8fc7 for \u5faa\u73af\u6bcf\u6b21\u5c06\u4e00\u4e2a\u6570\u7684\u5e73\u65b9 append \u5230\u5217\u8868\u7684\u7ed3\u5c3e\u3002<br>squares = []  <br>for i in range(10):  <br>        squares.append(i ** 2)   <br>        print(squares)<br>\u8f93\u51fa\uff1a[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]<br>\u4f46\u5176\u5b9e\u901a\u8fc7\u5217\u8868\u751f\u6210\u5f0f\uff0c\u6211\u4eec\u6709\u4e00\u79cd\u66f4\u5feb\u6377\u7684\u65b9\u6cd5\uff0c\u53ea\u9700\u8981\u4e00\u884c\u4ee3\u7801\u5c31\u80fd\u591f\u5b9e\u73b0\u76f8\u540c\u7684\u529f\u80fd\u3002<br>squares = [i ** 2 for i in range(10)]<br>print(squares)<br>\u8f93\u51fa\uff1a[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]<br>3. \u901a\u8fc7 Python \u5185\u7f6e\u7684sorted()\u65b9\u6cd5\u5bf9\u590d\u6742\u7684\u5bf9\u8c61\u8fdb\u884c\u6392\u5e8f\u6392\u5e8f\u7684\u9700\u6c42\u5728\u5f88\u591a\u573a\u666f\u4e2d\u90fd\u5f88\u5e38\u89c1\uff0cPython\u4e2d\u7684\u53ef\u8fed\u4ee3\u5bf9<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f18\u8d28\u6559\u80b2\u9886\u57df\u521b\u4f5c\u800510\u4e2a\u5c0f\u6280\u5de7\u6539\u8fdb\u7684 Python \u4ee3\u7801\uff0c\u8ba9\u4f60\u7684\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u66f4 <span class=\"readmore\"><a href=\"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=2237\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,10,1],"tags":[],"class_list":["post-2237","post","type-post","status-publish","format-standard","hentry","category-2","category-python","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/2237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2237"}],"version-history":[{"count":2,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/2237\/revisions"}],"predecessor-version":[{"id":3361,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/2237\/revisions\/3361"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2237"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}