{"id":2238,"date":"2022-03-31T08:25:13","date_gmt":"2022-03-31T00:25:13","guid":{"rendered":"http:\/\/cnliutz.uicp.io\/?p=2238"},"modified":"2022-03-31T08:25:13","modified_gmt":"2022-03-31T00:25:13","slug":"10%e4%b8%aa%e5%b0%8f%e6%8a%80%e5%b7%a7%ef%bc%8c%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:\/\/xc.ipyingshe.net:5347\/?p=2238","title":{"rendered":"10\u4e2a\u5c0f\u6280\u5de7\uff0c\u8ba9\u4f60\u7684 Python \u4ee3\u7801\u66f4\u52a0\u4f18\u96c5"},"content":{"rendered":"\n<p>10\u4e2a\u5c0f\u6280\u5de7\uff0c\u8ba9\u4f60\u7684 Python \u4ee3\u7801\u66f4\u52a0\u4f18\u96c5 Python\u53ef\u4e5001-04 \u00b7 \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\u30021. \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\u79c1\u4fe1\u5c0f\u7f1601\u5373\u53ef\u83b7\u53d6\u5927\u91cfPython\u5b66\u4e60\u8d44\u6e90data = [1, 3, -5, 7, 9, -11]for i in range(len(data)): if data[i] &lt; 0: print(f&#8221;\u7d22\u5f15\uff1a{   i}\uff0c\u5143\u7d20\uff1a{   data[i]}&#8221;)\u8f93\u51fa\uff1a\u7d22\u5f15\uff1a2\uff0c\u5143\u7d20\uff1a-5\u7d22\u5f15\uff1a5\uff0c\u5143\u7d20\uff1a-11\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\uff1adata = [1, 3, -5, 7, 9, -11]for i, v, in enumerate(data): if v &lt; 0: print(f&#8221;\u7d22\u5f15\uff1a{   i}\uff0c\u5143\u7d20\uff1a{   v}&#8221;)\u8f93\u51fa\uff1a\u7d22\u5f15\uff1a2\uff0c\u5143\u7d20\uff1a-5\u7d22\u5f15\uff1a5\uff0c\u5143\u7d20\uff1a-112. \u6df1\u5165\u7406\u89e3list\uff0c\u66ff\u4ee3for\u5faa\u73af\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\u3002squares = []for i in range(10): squares.append(i ** 2)print(squares)\u8f93\u51fa\uff1a[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\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\u3002squares = [i ** 2 for i in range(10)]print(squares)\u8f93\u51fa\uff1a[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]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>10\u4e2a\u5c0f\u6280\u5de7\uff0c\u8ba9\u4f60\u7684 Python \u4ee3\u7801\u66f4\u52a0\u4f18\u96c5 Python\u53ef\u4e5001-04 \u00b7 <span class=\"readmore\"><a href=\"http:\/\/xc.ipyingshe.net:5347\/?p=2238\">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":[10],"tags":[],"class_list":["post-2238","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/2238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2238"}],"version-history":[{"count":0,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/2238\/revisions"}],"wp:attachment":[{"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2238"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xc.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}