{"id":1284,"date":"2014-09-21T09:55:36","date_gmt":"2014-09-21T09:55:36","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1284"},"modified":"2014-09-21T09:55:36","modified_gmt":"2014-09-21T09:55:36","slug":"%e4%b8%80%e4%ba%9bpython%e7%89%87%e6%ae%b5_2","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1284.html","title":{"rendered":"\u4e00\u4e9bPython\u7247\u6bb5_2"},"content":{"rendered":"<h5>\u57fa\u7840\u56de\u987e\uff1a<\/h5>\n<p>\u5173\u4e8ePython\u5185\u7f6e\u6570\u636e\u7ed3\u6784\u548c\u5e93\u7684\u6700\u91cd\u8981\u7684\u529f\u80fd\uff0c\u81ea\u884c\u67e5\u9605\uff1a<a href=\"http:\/\/docs.python.org\" target=\"_blank\">http:\/\/docs.python.org<\/a><\/p>\n<ul>\n<li>Python\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/li>\n<li>\u65e5\u671f\u548c\u65f6\u95f4<\/li>\n<\/ul>\n<h5>\u63a7\u5236\u6d41\uff1a<\/h5>\n<p style=\"padding-left: 30px;\">if-elif-else<br \/>\nfor\u5faa\u73af<br \/>\nwhile\u5faa\u73af<br \/>\n\u5f02\u5e38\u5904\u7406<br \/>\n\u4e09\u5143\u8868\u8fbe\u5f0f<\/p>\n<h5>\u6570\u636e\u7ed3\u6784\u548c\u5e8f\u5217\uff1a<\/h5>\n<h6>\u5143\u7ec4<\/h6>\n<p style=\"padding-left: 30px;\">\u5143\u7ec4\u62c6\u5305<br \/>\n\u5143\u7ec4\u65b9\u6cd5( .count )<\/p>\n<h6>\u5217\u8868<\/h6>\n<p style=\"padding-left: 30px;\">\u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20<br \/>\n\u5408\u5e76\u5217\u8868<br \/>\n\u6392\u5e8f<br \/>\n\u5207\u7247<\/p>\n<h5>\u5185\u7f6e\u7684\u5e8f\u5217\u51fd\u6570<\/h5>\n<p style=\"padding-left: 30px;\">enumerate<br \/>\nsorted<br \/>\nzip<br \/>\nreversed<\/p>\n<h6>\u5b57\u5178(\u4f7f\u7528\u5927\u62ec\u53f7\uff0c\u5e76\u7528\u5192\u53f7\u5206\u9694key\/value)<\/h6>\n<p style=\"padding-left: 30px;\">\u4ece\u5e8f\u5217\u7c7b\u578b\u521b\u5efa\u5b57\u5178(\u5b57\u5178\u63a8\u5bfc\u5f0f)<br \/>\n\u9ed8\u8ba4\u503c( .setdefault\u65b9\u6cd5 &amp; collections.defaultdict )<br \/>\n\u5b57\u5178\u952e\u7684\u6709\u6548\u7c7b\u578b<\/p>\n<h6>\u96c6\u5408<\/h6>\n<p style=\"padding-left: 30px;\">Python\u7684\u96c6\u5408\u8fd0\u7b97(\u4ea4\u3001\u5e76\u3001\u5dee)<\/p>\n<h6>\u63a8\u5bfc\u5f0f<\/h6>\n<p style=\"padding-left: 30px;\">\u5217\u8868\u3001\u96c6\u5408\u4ee5\u53ca\u5b57\u5178\u7684\u63a8\u5bfc\u5f0f<br \/>\n\u5d4c\u5957\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n<pre class=\"lang:default decode:true \">####\ndef get_counts(sequence):\n    counts = {}\n    for x in sequence:\n        if x in counts:\n            counts[x] += 1\n        else:\n            counts[x] = 1\n    return counts\n####\nfrom collections import defaultdict\ndef get_counts2(sequence):\n    counts = defaultdict(int)\n    for x in sequence:\n        counts[x] += 1\n    return counts\n####\n\n'''\n1.Python\u8bed\u8a00\u7cbe\u8981\n\u5173\u4e8ePython\u5185\u7f6e\u6570\u636e\u7ed3\u6784\u548c\u5e93\u7684\u6700\u91cd\u8981\u7684\u529f\u80fd http:\/\/docs.python.org\n\n\u53d8\u91cf\u548c\u6309\u5f15\u7528\u4f20\u9012\n\u4e8c\u5143\u8fd0\u7b97\u7b26\u548c\u6bd4\u8f83\u8fd0\u7b97\u7b26(is\u5173\u952e\u5b57--\u5224\u65ad\u4e24\u4e2a\u5f15\u7528\u662f\u5426\u6307\u5411\u540c\u4e00\u5bf9\u8c61)\nPython\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\n\u65e5\u671f\u548c\u65f6\u95f4\n\n\u63a7\u5236\u6d41\n    if-elif-else\n    for\u5faa\u73af\n    while\u5faa\u73af\n    \u5f02\u5e38\u5904\u7406\n    \u4e09\u5143\u8868\u8fbe\u5f0f\n\n\u6570\u636e\u7ed3\u6784\u548c\u5e8f\u5217\n\u5143\u7ec4\n    \u5143\u7ec4\u62c6\u5305\n    \u5143\u7ec4\u65b9\u6cd5( .count )\n\u5217\u8868\n    \u6dfb\u52a0\u548c\u5220\u9664\u5143\u7d20\n    \u5408\u5e76\u5217\u8868\n    \u6392\u5e8f\n    \u5207\u7247\n\u5185\u7f6e\u7684\u5e8f\u5217\u51fd\u6570\n    enumerate\n    sorted\n    zip\n    reversed\n\n\u5b57\u5178(\u4f7f\u7528\u5927\u62ec\u53f7\uff0c\u5e76\u7528\u5192\u53f7\u5206\u9694key\/value)\n    \u4ece\u5e8f\u5217\u7c7b\u578b\u521b\u5efa\u5b57\u5178(\u5b57\u5178\u63a8\u5bfc\u5f0f)\n    \u9ed8\u8ba4\u503c( .setdefault\u65b9\u6cd5 &amp; collections.defaultdict )\n    \u5b57\u5178\u952e\u7684\u6709\u6548\u7c7b\u578b\n\u96c6\u5408\n    Python\u7684\u96c6\u5408\u8fd0\u7b97(\u4ea4\u3001\u5e76\u3001\u5dee)\n\n\u5217\u8868\u3001\u96c6\u5408\u4ee5\u53ca\u5b57\u5178\u7684\u63a8\u5bfc\u5f0f\n    \u5d4c\u5957\u5217\u8868\u63a8\u5bfc\u5f0f\n\n\n\u4f7f\u7528\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c\n\u51fd\u6570\u4ea6\u4e3a\u5bf9\u8c61\n\u751f\u6210\u5668(\u751f\u6210\u5668\u8868\u8fbe\u5f0f)\n\n'''\npath = 'C:test.txt'\nlines = [x.rstrip() for x in open(path)]\n\nf = open(path)\nfor line in f:\n    pass\n\n\n####\u751f\u6210\u5668####\ndef make_change(amount, coins=[1, 5, 10, 25], hand=None):\n\thand = [] if hand is None else hand\n\tif amount == 0:\n\t\tyield hand\n\tfor coin in coins:\n\t\tif coin &gt; amount or (len(hand) &gt; 0 and hand[-1] &lt; coin):\n\t\t\tcontinue\n\t\tfor result in make_change(amount-coin, coins=coins, hand=hand+[coin]):\n\t\t\tyield result\n\nfor way in make_change(100, coins=[10, 25, 50]):\n\tprint way\n####\n\n####\u51fd\u6570\u4ea6\u4e3a\u5bf9\u8c61####\nstates = ['  Alabama ', 'Georgia!', 'Georgia', 'georgia', 'south    carolina##', ' West virginia?', 'New York .', 'China.', 'Beijing...']\n\nimport re\ndef clean_stings(strings):\n    result = []\n    for value in strings:\n        value = value.strip()\n        value = re.sub('[!#?]', '', value)\n        value = value.title()\n        result.append(value)\n    return result\nclean_stings(states)\n\ndef remove_punctuation(value):\n    return re.sub('[!?#]', '', value)\nclean_ops = [str.strip, remove_punctuation, str.title]\ndef clean_stings2(strings, ops):\n    result = []\n    for value in strings:\n        for function in ops:\n            value = function(value)\n        result.append(value)\n    return result\nclean_stings2(states, clean_ops)\n\nmap(remove_punctuation, states) #\u5185\u7f6e\u7684map\u51fd\u6570\u6709\u70b9\u725b\u903c\u554a!\n\n####\n\ndef f():\n    a = 5\n    b = 6\n    c = 7\n    return a, b, c\nx, y, z = f()\n\n\n####\u63a8\u5bfc\u5f0f####\n\n#\u5217\u8868\u63a8\u5bfc\u5f0f\nstrings = ['a', 'as', 'a', 'bad', 'boy', 'Python']\n[x.upper() for x in strings]\n[x.upper() for x in strings if len(x)&gt;2]\n\n#\u5b57\u5178\u63a8\u5bfc\u5f0f\ndict_map = {key:value for key, value in enumerate(strings)}\ndict_map = dict((value, key) for key, value in enumerate(strings))\n\n#\u96c6\u5408\u63a8\u5bfc\u5f0f\nset_map = {key for key in strings}\n\n#\u5d4c\u5957\u5217\u8868\u63a8\u5bfc\u5f0f\nall_data = [['Tom', 'Jerry', 'Lily', 'Lucy', 'Hello,world', 'Jefferson', 'Steven', 'Joe', 'Bill'], ['Susie', 'Cookie', 'Qunar', 'Baidu', 'Notepad', 'Apple', 'Alibaba']]\nnames_of_interset = []\nfor names in all_data:\n    enough = [name for name in names if name.count('e')&gt;2]\n    names_of_interset.extend(enough)\n\nnames2 = [name for names in all_data for name in names if name.count('e') &gt; 2]\nnames3 = [name for names in all_data for name in names]\n\n\n#zip\nseq1 = ['a', 'b', 'c']\nseq2 = ['one', 'two', 'three']\nzip(seq1, seq2)\n#zip\u53ef\u4ee5\u63a5\u53d7\u4efb\u610f\u6570\u91cf\u7684\u5e8f\u5217\uff0c\u6700\u7ec8\u5f97\u5230\u7684\u5143\u7ec4\u6570\u91cf\u7531\u6700\u77ed\u7684\u5e8f\u5217\u51b3\u5b9a\nseq3 = ['True', 'False']\nzip(seq1, seq2, seq3)\n\n\n#\u63a7\u5236\u6d41\nif x &lt; 0:\n\tprint 'It's negative'\nelif x == 0:\n\tprint 'Equal to zero'\nelif 0 &lt; x &lt; 5:\n\tprint 'Between 0 and 5'\nelse:\n\tprint 'Positive and larger than 5'\n\n####\nsequence = [1, 2, None, 4, None, 5]\ntotal = 0\nfor value in sequence:\n\tif value is None:\n\t\tcontinue\n\ttotal += value\n####\n\nsequence = [1, 2, 0, 4, 6, 5, 2, 1]\ntotal_until_5 = 0\nfor value in sequence:\n\tif value == 5:\n\t\tbreak\n\ttotal_until_5 += value\n####\n\n#Python\u4e09\u5143\u8868\u8fbe\u5f0f\nvalue = true_expr if condition else false_expr<\/pre>\n<p>&nbsp;<\/p>\n<h5>\u7528Python\u4e0b\u8f7d\u8c46\u74e3\u4e0a\u7684\u97f3\u4e50<\/h5>\n<pre class=\"lang:default decode:true \">#!\/usr\/bin\/env python\n# -- coding:utf-8 --\n\nimport urllib, sys, gevent, re\nfrom gevent import monkey\n\nreload(sys)\nsys.setdefaultencoding('utf8')\nmonkey.patch_all()\n\ndef encode(s):\n    return s.decode('utf-8').encode(sys.stdout.encoding, 'ignore')\n\ndef worker(reg, url):\n    response = urllib.urlopen(url)\n    text = response.read()\n    groups = re.finditer(reg, text)\n    m_arr = []\n    for g in groups:\n        name = encode(g.group(1).strip()) + \".mp3\"\n        path = g.group(2).replace('\\', '')\n        #print encode(name), path; sys.exit()\n        m_arr.append((name, path))\n    return m_arr\n\ndef grun(path, name):\n    urllib.urlretrieve(path, name)\n\nif __name__  == '__main__':\n    reg = re.compile('{\"name\":\"(.+?)\".+?\"rawUrl\":\"(.+?)\",.+?}', re.M)\n    musicArray = worker(reg, \"http:\/\/site.douban.com\/huazhou\/\")\n    jobs = []\n    for (name, path) in musicArray:\n        #print name, path\n        jobs.append(gevent.spawn(grun, path, name))\n    gevent.joinall(jobs)<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>\u4ee3\u7801\u53c2\u8003\u5730\u5740<\/strong><\/span>\uff1a<a href=\"http:\/\/www.cnblogs.com\/descusr\/archive\/2012\/11\/20\/2778442.html\" target=\"_blank\">\u4f7f\u7528gevent\u591a\u7ebf\u7a0b\u4e0b\u8f7d\u8c46\u74e3\u97f3\u4e50<\/a><\/p>\n<h5>\u5b57\u7b26\u4e32\u9006\u5e8f<\/h5>\n<pre class=\"lang:default decode:true \">def reverseWords(s):\n    wordlist = s.split()\n    return ' '.join(wordlist[::-1])\nprint reverseWords(\"Haha, just do it\")<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u7840\u56de\u987e\uff1a \u5173\u4e8ePython\u5185\u7f6e\u6570\u636e\u7ed3\u6784\u548c\u5e93\u7684\u6700\u91cd\u8981\u7684\u529f\u80fd\uff0c\u81ea\u884c\u67e5\u9605\uff1ahttp:\/\/docs.python.o [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,12],"tags":[341,342,8],"class_list":["post-1284","post","type-post","status-publish","format-standard","hentry","category-programing","category-tools","tag-gevent","tag-map","tag-python"],"views":3903,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1284","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=1284"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1284\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}