{"id":678,"date":"2014-07-21T14:17:42","date_gmt":"2014-07-21T14:17:42","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=678"},"modified":"2014-07-21T14:17:42","modified_gmt":"2014-07-21T14:17:42","slug":"python-enumerate-%e5%ad%a6%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/678.html","title":{"rendered":"Python enumerate \u5b66\u4e60"},"content":{"rendered":"<h5>\u5728for\u5faa\u73af\u4e2d\u5f97\u5230\u8ba1\u6570<\/h5>\n<p>\u53c2\u6570\u4e3a\u53ef\u904d\u5386\u7684\u53d8\u91cf\uff0c\u5982\uff1a\u5b57\u7b26\u4e32\uff0c\u5217\u8868\u7b49\uff1b<span style=\"color: rgb(255, 0, 0);\"><strong>\u8fd4\u56de\u503c\u4e3aenumerate\u7c7b<\/strong><\/span>\uff1a<\/p>\n<pre class=\"lang:default decode:true\">import string\ns = string.ascii_lowercase\ne = enumerate(s)\nprint s\nprint list(e)<\/pre>\n<p>\u8f93\u51fa\u4e3a\uff1a<\/p>\n<p>abcdefghijklmnopqrstuvwxyz<br \/>\n[(0, &#8216;a&#8217;), (1, &#8216;b&#8217;), (2, &#8216;c&#8217;), (3, &#8216;d&#8217;), (4, &#8216;e&#8217;), (5, &#8216;f&#8217;), (6, &#8216;g&#8217;), (7, &#8216;h&#8217;), (8, &#8216;i&#8217;), (9, &#8216;j&#8217;)\u2026\u2026(25, &#8216;z&#8217;)]<\/p>\n<p><span style=\"color: rgb(255, 0, 0);\"><strong>\u5728\u540c\u65f6\u9700\u8981index\u548cvalue\u503c\u7684\u65f6\u5019\u53ef\u4ee5\u4f7f\u7528 enumerate<\/strong><\/span>\u3002<\/p>\n<h5>enumerate \u5b9e\u6218<\/h5>\n<p>line \u662f\u4e2a string \uff0c\u5305\u542b 0 \u548c 1\uff0c\u8981\u628a1\u90fd\u627e\u51fa\u6765\uff1a<\/p>\n<h6>#\u65b9\u6cd5\u4e00<\/h6>\n<pre class=\"lang:default decode:true \">def read_line(line):\n    sample = {}\n    n = len(line)\n    for i in range(n):\n        if line[i]!='0':\n            sample[i] = int(line[i])\n    return sample<\/pre>\n<h6>#\u65b9\u6cd5\u4e8c<\/h6>\n<pre class=\"lang:default decode:true \">def xread_line(line):\n    return((idx,int(val)) for idx, val in enumerate(line) if val != '0')\nprint read_line('0001110101')\nprint list(xread_line('0001110101'))<\/pre>\n<p>\u4e00\u822c\u60c5\u51b5\u4e0b\u5bf9\u4e00\u4e2a\u5217\u8868\u6216\u6570\u7ec4<strong><span style=\"color: #ff0000;\">\u65e2\u8981\u904d\u5386\u7d22\u5f15\u53c8\u8981\u904d\u5386\u5143\u7d20<\/span><\/strong>\u65f6\uff0c\u4f1a\u8fd9\u6837\u5199\uff1a<\/p>\n<pre class=\"lang:default decode:true \">for i in range(0, len(list)):\n    print i ,list[i]<\/pre>\n<p>\u4f46\u662f\u8fd9\u79cd\u65b9\u6cd5\u6709\u4e9b\u7d2f\u8d58\uff0c\u4f7f\u7528\u5185\u7f6eenumerrate\u51fd\u6570\u4f1a\u6709\u66f4\u52a0\u76f4\u63a5\uff0c\u4f18\u7f8e\u7684\u505a\u6cd5\uff0c\u5148\u770b\u770benumerate\u7684\u5b9a\u4e49\uff1a<\/p>\n<pre class=\"lang:default decode:true\">def enumerate(collection):\n    'Generates an indexed series:  (0,coll[0]), (1,coll[1]) ...'\n    i = 0\n    it = iter(collection)\n    while 1:\n        yield(i, it.next())\n    i += 1<\/pre>\n<p>enumerate\u4f1a\u5c06\u6570\u7ec4\u6216\u5217\u8868\u7ec4\u6210\u4e00\u4e2a\u7d22\u5f15\u5e8f\u5217\u3002\u4f7f\u6211\u4eec\u518d\u83b7\u53d6\u7d22\u5f15\u548c\u7d22\u5f15\u5185\u5bb9\u7684\u65f6\u5019\u66f4\u52a0\u65b9\u4fbf\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true\">for index\uff0ctext in enumerate(list)):\n    print index ,text<\/pre>\n<p>\u5728cookbook\u91cc\u4ecb\u7ecd\uff0c\u5982\u679c\u4f60\u8981\u8ba1\u7b97\u6587\u4ef6\u7684\u884c\u6570\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<p>count = len(open(thefilepath,\u2018rU\u2019).readlines())<\/p>\n<p>\u524d\u9762\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\uff0c\u4f46\u662f\u53ef\u80fd\u6bd4\u8f83\u6162\uff0c\u5f53\u6587\u4ef6\u6bd4\u8f83\u5927\u65f6\u751a\u81f3\u4e0d\u80fd\u5de5\u4f5c\uff0c\u4e0b\u9762\u8fd9\u79cd\u5faa\u73af\u8bfb\u53d6\u7684\u65b9\u6cd5\u66f4\u5408\u9002\u4e9b\uff1a<\/p>\n<pre class=\"lang:default decode:true\">Count = -1\nfor count,line in enumerate(open(thefilepath,'rU'))\uff1a\n    pass\nCount += 1<\/pre>\n<p>\u8fd8\u6709\u4e00\u79cd\u8ba1\u7b97\u6587\u4ef6\u884c\u6570\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"lang:default decode:true \">def linecount(filename):\n    '''\u8fd4\u56de\u6587\u4ef6\u7684\u884c\u6570'''\n    fp = open(filename)\n    lc = sum(1 for line in fp)\n    fp.close()\n    return lc<\/pre>\n<p>-EOF-<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728for\u5faa\u73af\u4e2d\u5f97\u5230\u8ba1\u6570 \u53c2\u6570\u4e3a\u53ef\u904d\u5386\u7684\u53d8\u91cf\uff0c\u5982\uff1a\u5b57\u7b26\u4e32\uff0c\u5217\u8868\u7b49\uff1b\u8fd4\u56de\u503c\u4e3aenumerate\u7c7b\uff1a import  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[174,8],"class_list":["post-678","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","tag-enumerate","tag-python"],"views":1873,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=678"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/678\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}