{"id":289369,"date":"2021-01-17T16:18:13","date_gmt":"2021-01-17T08:18:13","guid":{"rendered":"http:\/\/4563.org\/?p=289369"},"modified":"2021-01-17T16:18:13","modified_gmt":"2021-01-17T08:18:13","slug":"%e6%b1%82%e6%95%99-python-enumerate-%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=289369","title":{"rendered":"\u6c42\u6559 Python enumerate \u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6c42\u6559 Python enumerate \u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : KnightNic <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a https:\/\/gist.github.com\/KnightNic\/e9d2ce3321ab78b0824ebc4316cd7102<\/p>\n<p>\u53ef\u4ee5\u6b63\u5e38\u6253\u5370\uff0c\u6bd4\u5982\u53ea\u6709\u4e24\u4e2a\u6587\u4ef6\u65f6\uff0c\u4f1a\u4ee5 0: a.csv, 1: b.csv \u663e\u793a\uff0c\u7ec8\u7aef\u9009\u62e9 0 \u6216 1 \u53ef\u4ee5\u6b63\u5e38\u6267\u884c\u540e\u7eed\u4ee3\u7801\u3002<\/p>\n<p>\u53ef\u662f\u60f3\u8bf7\u6559\u4e00\u4e0b\u4e3a\u4ec0\u4e48\u6307\u5b9a enumerate(files, 1)\u4e4b\u540e\uff0c\u5f53\u7ec8\u7aef\u8f93\u5165\u9009\u62e9 2 \u7684\u65f6\u5019\u5c31\u62a5\u9519\u5462\uff1f index list out of range\uff0cGoogle \u4e86\u5f88\u591a\uff0c\u53ef\u662f\u6ca1\u7814\u7a76\u51fa\u6765<\/p>\n<p>\u4e0d\u662f\u7a0b\u5e8f\u5458\u5de5\u7a0b\u5e08\uff0c\u53ea\u662f\u4e3a\u4e86\u907f\u514d\u5458\u5de5\u624b\u52a8\u5904\u7406\u4e00\u4e9b\u9700\u8981\u5b9a\u671f\u5904\u7406\u7684\u5927\u8868\u683c\uff0c\u51cf\u5c11\u5de5\u4f5c\u91cf\uff5e\u6240\u4ee5\u81ea\u5df1\u7814\u7a76\u4e86\u4e0b python \u548c pandas<\/p>\n<p>\u5927\u5b66\u65f6\u7684 C \u8bed\u8a00\u8fd8\u6b8b\u5b58\u4e00\u4e22\u4e22\u8bb0\u5fc6\u3002\u3002\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>9<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4967236\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : rocksolid <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5b57\u7b26\u4e32 str \u3001\u5217\u8868 list \u3001\u5143\u7ec4 tuple \u8fdb\u884c\u6309\u7d22\u5f15\u53d6\u503c\u7684\u65f6\u5019\uff0c\u9ed8\u8ba4\u8303\u56f4\u4e3a 0 ~ len(\u6709\u5e8f\u5e8f\u5217)-1\uff0c\u8ba1\u6570\u4ece 0 \u5f00\u59cb\uff0c\u800c\u4e0d\u662f\u4ece 1 \u5f00\u59cb\uff0c\u6700\u540e\u4e00\u4f4d\u7d22\u5f15\u5219\u4e3a\u603b\u957f\u5ea6\u51cf\u53bb 1 \u3002<br \/>\u5982\u679c\u8f93\u5165\u7684\u53d6\u503c\u7ed3\u679c\u4e0d\u5728\u8fd9\u4e2a\u8303\u56f4\u5185\uff0c\u5219\u62a5\u8fd9\u4e2a\u9519\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4967237\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Latin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             l = [1,2]<br \/>for n,i in enumerate(l,1):<br \/> print(n,i)<br \/># 1 1<br \/># 2 2<br \/>l[2] # \u6b64\u65f6 index list out of range<br \/>\u56e0\u4e3a\u4f60\u8bbe\u7f6e\u4e86\u8d77\u59cb\u503c\u4e3a 1 \u800c\u5217\u8868\u7684\u7d22\u5f15\u6ca1\u6709\u6539\u4e0b\u6807                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4967238\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : KnightNic <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Latin @rocksolid \u611f\u8c22\u4e8c\u4f4d\uff0c\u5176\u5b9e\u6211\u4e4b\u524d\u8c03\u8bd5\u7684\u65f6\u5019\uff0c\u5c1d\u8bd5\u5728\u4fee\u6539 df = pd.read_csv(files[int(file)-1])\uff0c\u786e\u5b9e\u80fd\u591f\u63a5\u53d7\u7528\u6237 1 \u548c 2 \u7684\u8f93\u5165\u9009\u62e9\uff0c\u53ef\u662f\u5f53\u6211\u8f93\u5165 0 \u7684\u65f6\u5019\uff0c\u8c8c\u4f3c\u9ed8\u8ba4\u8fd4\u56de\u4e86\u5e8f\u53f7 2 \u5bf9\u5e94\u7684\u6587\u4ef6\u3002<br \/>\u53e6\u5916\u60f3\u8bf7\u6559\u4e0b\u5982\u679c\u6211\u60f3\u5728 input \u4e4b\u540e\u52a0\u4e00\u4e2a\u5224\u65ad\uff0c\u6bd4\u5982\u8f93\u5165\u7684\u6570\u5b57\u5728\u7d22\u5f15\u5230\u7684\u6587\u4ef6\u5e8f\u53f7\u4e4b\u5185\uff0c\u5219\u8fdb\u5165\u540e\u7eed\u4ee3\u7801\uff0c\u5426\u5219\u5c31\u8fd4\u56de\u4e00\u6bb5\u6587\u5b57\uff0c\u6bd4\u5982\u201c\u8f93\u5165\u9519\u8bef\u201d\u4e4b\u7c7b\u7684\u3002<\/p>\n<p>\u73b0\u5728\u60f3\u60f3\u7a0b\u5e8f\u5458\u3001\u5de5\u7a0b\u5e08\u786e\u5b9e\u4e0d\u7b80\u5355\uff5e\u65e9\u77e5\u9053\u5927\u5b66\u4e5f\u6df1\u5165\u5b66\u4e00\u4e2a\u7f16\u7a0b\u8bed\u8a00\u4e86                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4967239\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : XIVN1987 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Latin <\/p>\n<p>enumerate \u8fd8\u80fd\u8bbe\u7d22\u5f15\u8d77\u59cb\u503c\uff0c\uff0c\u4ee5\u524d\u6ca1\u6ce8\u610f\u8fc7\uff0c\uff0c\u5b66\u5230\u4e00\u624b                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4967240\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Latin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @KnightNic 0-1 \u662f-1 \u800c-1 \u662f\u5217\u8868\u6700\u540e\u4e00\u4f4d                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4967241\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : maocat <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             enumerate \u70b9\u5f00\u770b\u6e90\u7801\u4f60\u5c31\u4f1a\u53d1\u73b0\u7b2c\u4e8c\u4e2a\u53ef\u53d8\u53c2\u6570\u540d\u5b57\u53eb start                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4967242\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : KnightNic <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Latin \u611f\u8c22\uff0c\u76ee\u524d\u5df2\u7ecf\u5927\u81f4\u89e3\u51b3\u4e86\uff0c\u4e5f\u5b9e\u73b0\u4e86\u4e00\u4e9b input \u65f6\u7684\u5224\u65ad\uff0c\u4e0d\u8fc7\u60f3\u8bf7\u6559\u4e00\u4e0b\u600e\u4e48\u53bb\u4fee\u6539\u5217\u8868\u7684\u7d22\u5f15\u8d77\u59cb\u503c\uff0c\u4ece 1 \u5f00\u59cb\uff0c\u4f7f\u53ea\u8ddf enumerate \uff08 files, 1 \uff09\u4e00\u81f4\u5462\uff1f<br \/>\u6211\u76ee\u524d\u662f\u7528\u4e86\u4e00\u4e2a if \u8bed\u53e5\uff0c\u5f3a\u5236\u5254\u9664\u4e86 input \u4e3a 0 \u7684\u60c5\u51b5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4967243\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : no1xsyzy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @KnightNic \u4e0d\u80fd\u6539\u8d77\u59cb\u503c<br \/>\u4f46\uff0c\u5982\u679c\u4f60\u4e0d\u4ecb\u610f\u7684\u8bdd\uff0c\u6709 dict[int, Any] \u548c list[None, Any] \u4e24\u79cd\u505a\u6cd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4967244\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Latin <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @KnightNic case \u6761\u4ef6 1&lt;input&lt;len(files) \u518d\u5904\u7406 files \u4e3a\u7a7a\u7684\u60c5\u51b5\u4e0d\u5c31\u597d\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6c42\u6559 Python enumera&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/289369"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=289369"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/289369\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=289369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=289369"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=289369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}