{"id":175908,"date":"2020-04-16T11:00:24","date_gmt":"2020-04-16T03:00:24","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=175908"},"modified":"2020-04-01T09:12:20","modified_gmt":"2020-04-01T01:12:20","slug":"interesting-functions-in-python","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/interesting-functions-in-python.html","title":{"rendered":"\u65b0\u77e5\u8bc6\u70b9\uff1aPython\u91cc\u9762\u6709\u51e0\u4e2a\u6709\u8da3\u7684\u51fd\u6570"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4eca\u5929\u4e3b\u8981\u662f\u5206\u4eab5\u4e2apython\u5185\u7f6e\u7684\u5c0f\u51fd\u6570\uff0c\u90fd\u662f\u4e2a\u4eba\u89c9\u5f97\u5f3a\u5927\u800c\u7cbe\u81f4\u7684\u51e0\u4e2a\u51fd\u6570\u3002\u4e0d\u662f\u9ad8\u6df1\u7684\u77e5\u8bc6\uff0c\u7eaf\u7cb9\u56e0\u4e3a\u89c9\u5f97\u597d\u7528\u800c\u5206\u4eab\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u4f17\u6240\u5468\u77e5\uff0cpython\u529f\u80fd\u5f3a\u5927\u3001\u8bed\u6cd5\u7075\u6d3b\uff0c\u8fd9\u4e9b\u5f97\u76ca\u4e8e\u5176\u4e30\u5bcc\u800c\u5f3a\u5927\u7684\u5e93\u3002\u9664\u4e86\u4f17\u591a\u7b2c\u4e09\u65b9\u5e93\u548c\u65b9\u6cd5\u51fd\u6570\uff0cpython\u81ea\u5e26\u7684\u5f88\u591a\u51fd\u6570\u4e5f\u975e\u5e38\u6709\u8da3\uff0c\u7528\u8d77\u6765\u79f0\u5f97\u4e0a\u4f18\u96c5\u3002<\/p>\n

\"\"<\/p>\n

\u4eca\u5929\u4e3b\u8981\u662f\u5206\u4eab5\u4e2apython\u5185\u7f6e\u7684\u5c0f\u51fd\u6570\uff0c\u90fd\u662f\u4e2a\u4eba\u89c9\u5f97\u5f3a\u5927\u800c\u7cbe\u81f4\u7684\u51e0\u4e2a\u51fd\u6570\u3002\u4e0d\u662f\u9ad8\u6df1\u7684\u77e5\u8bc6\uff0c\u7eaf\u7cb9\u56e0\u4e3a\u89c9\u5f97\u597d\u7528\u800c\u5206\u4eab\u3002<\/p>\n

01 \u4e00\u7528\u800c\u8fc7\uff1alambda<\/strong><\/div>\n

\u5f88\u591a\u8bed\u8a00\u90fd\u6709\u533f\u540d\u51fd\u6570\uff0cpython\u7684\u533f\u540d\u51fd\u6570\u5199\u4f5clambda\uff0c\u5f53\u9700\u8981\u5b9e\u73b0\u4e00\u5b9a\u529f\u80fd\u800c\u53c8\u4e0d\u60f3\u201c\u5927\u5f20\u65d7\u9f13\u201d\u7684def\u4e00\u4e2a\u51fd\u6570\u65f6\uff0clambda\u5c31\u662f\u6700\u4f18\u7684\u9009\u62e9\u3002<\/p>\n

\u5176\u8bed\u6cd5\u683c\u5f0f\u4e00\u822c\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n

lambda x:x**2 \r\n# (x)> \r\n<\/pre>\n

\u4e5f\u53ef\u4ee5\u5c06\u5b83\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u7531\u4e8epython\u4e2d\u4e00\u5207\u7686\u5bf9\u8c61\uff0c\u6240\u4ee5\u540e\u7eed\u7a0b\u5e8f\u4e2d\u5c31\u53ef\u4ee5\u7528\u8be5\u53d8\u91cf\u8c03\u7528\u8fd9\u4e2a\u533f\u540d\u51fd\u6570\u3002<\/p>\n

f = lambda x:x**2 \r\nf(2) \r\n# 4 \r\n<\/pre>\n

\u5f53\u7136\uff0c\u8fd9\u91cc\u5176\u5b9e\u6ca1\u5fc5\u8981\u5e94\u7528lambda\u6765\u5b9e\u73b0\uff0c\u56e0\u4e3a\u65e2\u7136\u8981\u663e\u5f0f\u8c03\u7528\uff0c\u8fd8\u4e0d\u5982\u5e72\u8106\u76f4\u63a5\u5199\u4e2a\u660e\u786e\u7684\u51fd\u6570\u7f62\u4e86\u3002lambda\u51fd\u6570\u66f4\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\u5728\u4e8e\u8be5\u533f\u540d\u51fd\u6570\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\u65f6\uff0c\u5e94\u7528\u5c31\u6bd4\u8f83\u5408\u9002\u4e86\uff0c\u4f8b\u5982\uff0c\u5c06lambda\u4f5c\u4e3asort()\u51fd\u6570\u7684key\u53c2\u6570\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\u7684\u6392\u5e8f\u3002<\/p>\n

dyct = {'a':2, 'b':1, 'c':5} \r\nsorted(dyct.items(), key = lambda x:x[1]) \r\n# [('b', 1), ('a', 2), ('c', 5)] \r\n<\/pre>\n
02 \u667a\u80fd\u89e3\u538b\uff1azip<\/strong><\/div>\n

zip\u51fd\u6570\u4eba\u5982\u5176\u540d\uff0c\u662f\u6253\u5305\u6216\u8005\u89e3\u5305\u7684\u51fd\u6570\uff0c\u63a5\u53d72\u4e2a\u4ee5\u4e0a\u53ef\u8fed\u4ee3\u53d8\u91cf\uff0c\u8f93\u51fa\u5bf9\u5e94\u4f4d\u7f6e\u7ec4\u6210\u5143\u7ec4\u540e\u7684\u8fed\u4ee3\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n

a = ['a', 'b', 'c'] \r\nb = (4, 5, 6) \r\nzip(a,b) \r\n#  \r\nlist(zip(a,b)) \r\n# [('a', 4), ('b', 5), ('c', 6)] \r\ntuple(zip(a,b)) \r\n# (('a', 4), ('b', 5), ('c', 6)) \r\n<\/pre>\n

\u4e5f\u53ef\u4ee5\u63a5\u53d7\u591a\u4e8e2\u4e2a\u8f93\u5165\u53ef\u8fed\u4ee3\u53d8\u91cf\uff0c\u800c\u4e14\u5982\u679c\u5404\u8fed\u4ee3\u53d8\u91cf\u957f\u5ea6\u4e0d\u4e00\u81f4\u4e5f\u4e0d\u4f1a\u62a5\u9519\uff0c\u53ea\u662f\u6b64\u65f6\u8fd4\u56de\u8fed\u4ee3\u53d8\u91cf\u53d6\u51b3\u4e8e\u8f93\u5165\u603b\u957f\u5ea6\u6700\u77ed\u7684\u4e00\u4e2a\u3002\u4f8b\u5982\uff1a<\/p>\n

a = ['a', 'b', 'c', 'd', 'e'] \r\nb = (4, 5, 6, 7) \r\nc = [True, False, True] \r\nlist(zip(a,b,c)) \r\n# (('a', 4, True), ('b', 5, False), ('c', 6, True)) \r\n<\/pre>\n

\u4e0ezip\u6253\u5305\u76f8\u5bf9\u5e94\u7684\u7528\u6cd5\u662f\u89e3\u5305\uff0c\u5373\u5bf9\u4e00\u4e2a\u6253\u5305\u5f62\u5f0f\u7684\u5143\u7d20\u8fdb\u884c\u4f9d\u6b21\u89e3\u5305\uff0c\u5e76\u8fd4\u56de\u591a\u4e2a\u65b0\u7684\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n

aZip = (('a', 4, True), ('b', 5, False), ('c', 6, True)) \r\na, b, c = zip(*aZip) \r\n# a:('a', 'b', 'c') \r\n# b:(4, 5, 6) \r\n# c:(True, False, True) \r\n<\/pre>\n
03 \u4e00\u4e00\u6620\u5c04\uff1amap<\/strong><\/div>\n

map\u51fd\u6570\u4e5f\u6b63\u5982\u5176\u53d6\u540d\u4e00\u6837\uff0c\u662f\u4e00\u4e2a\u5c06\u63a5\u53d7\u7684\u8fed\u4ee3\u53d8\u91cf\u4f9d\u6b21\u7ecf\u8fc7\u67d0\u79cd\u6620\u5c04\uff0c\u5e76\u8f93\u51fa\u6620\u5c04\u540e\u7684\u8fed\u4ee3\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5bf9\u5217\u8868\u4e2d\u7684\u67d0\u4e2a\u53d8\u91cf\u4f9d\u6b21\u5b8c\u6210\u6c42\u503c\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5217\u8868\uff0c\u5219\u53ef\u4ee5\u5e94\u7528map\uff1a<\/p>\n

a = [1, 2, 3, 4] \r\nmap(str, a) \r\n# \r\n\r\n\r\n \r\nlist(map(str, a)) \r\n# ['1', '2', '3', '4'] \r\n<\/map><\/pre>\n

 <\/p>\n

\u8fd9\u662fmap\u51fd\u6570\u7684\u4e00\u4e2a\u5178\u578b\u7528\u6cd5\uff1a\u63a5\u53d72\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570(\u4e0a\u4f8b\u4e2d\u662fstr()\u51fd\u6570)\u662f\u4e00\u4e2a\u8981\u4f5c\u7528\u7684\u51fd\u6570\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u53ef\u8fed\u4ee3\u53d8\u91cf\u3002<\/p>\n

\u5f53\u7b2c\u4e00\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u662f\u662f\u4e00\u4e2a\u591a\u53d8\u91cf\u51fd\u6570\u65f6\uff0cmap\u4e5f\u53ef\u4ee5\u63a5\u53d7\u66f4\u591a\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n

a = [1, 2, 3, 4] \r\nb = [2, 2, 3, 3] \r\nlist(map(lambda x, y:x**y, a, b)) \r\n# [1, 4, 27, 64] \r\n<\/pre>\n

\u4e0ezip\u51fd\u6570\u4e2d\u7c7b\u4f3c\uff0c\u5f53map\u91cc\u7684\u51fd\u6570\u53c2\u6570\u957f\u5ea6\u4e0d\u5339\u914d\u65f6\u5e76\u4e0d\u4f1a\u62a5\u9519\uff0c\u53ea\u662f\u8f93\u51fa\u7ed3\u679c\u5c06\u7531\u6700\u77ed\u7684\u51b3\u5b9a\uff1a<\/p>\n

a = [1, 2, 3, 4] \r\nb = [2, 2] \r\nlist(map(lambda x, y:x**y, a, b)) \r\n# [1, 4] \r\n<\/pre>\n
04 \u4e00\u592b\u5f53\u5173\uff1afilter<\/strong><\/div>\n

\u4e0emap\u51fd\u6570\u7c7b\u4f3c\uff0cfilter\u51fd\u6570\u4e5f\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u53ca\u5176\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\uff0c\u53ea\u662f\u8981\u6c42\u8fd9\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u7ed3\u679c\u662fbool\u578b\uff0c\u5e76\u7528\u8fd9\u4e2abool\u7684\u7ed3\u679c\u51b3\u5b9a\u8f93\u51fa\u7684\u53d6\u820d\u95ee\u9898\u3002\u4f8b\u5982\u9700\u8981\u5bf9\u4e00\u4e2a\u8f93\u5165\u5217\u8868\u8fc7\u6ee4\uff0c\u8981\u6c42\u4fdd\u75593\u7684\u500d\u6570\uff1a<\/p>\n

a = range(10) \r\nfilter(lambda x:x%3==0, a) \r\n#  \r\nlist(filter(lambda x:x%3==0, a)) \r\n# [0, 3, 6, 9] \r\n<\/pre>\n

\u8fd9\u91cc\u9700\u6ce8\u610f\uff0c\u5f53filter\u7684\u7b2c\u4e00\u4e2a\u51fd\u6570\u8fd4\u56de\u503c\u4e0d\u662fbool\u578b\u65f6\u4e0d\u4f1a\u62a5\u9519\uff0c\u53ea\u662f\u5b83\u4f1a\u8f6c\u5316\u4e3abool\u578b\u5224\u65ad\uff0c\u5982\u679c\u5224\u65ad\u7ed3\u679c\u4e0d\u662fFalse(python\u4e2d\u4f1a\u5224\u4e3aFalse\u7684\u53d8\u91cf\u5305\u62ec0\u3001None\u3001[]\u7b49\u7b49)\uff0c\u5219\u4f1a\u5c06\u5176\u8f93\u51fa\uff0c\u5426\u5219\u8fc7\u6ee4\u6389\uff1a<\/p>\n

a = range(10) \r\nlist(filter(lambda x:x%3, a)) \r\n# [1, 2, 4, 5, 7, 8] \r\n<\/pre>\n
05 \u4e07\u5251\u5f52\u5b97\uff1areduce<\/strong><\/div>\n

map\u548cfilter\u51fd\u6570\u90fd\u662f\u591a\u5165\u591a\u51fa\u578b\uff0c\u5b9e\u8d28\u4e0a\u662f\u5b8c\u6210\u4e86\u7279\u5b9a\u7684\u53d8\u6362\u6216\u7b5b\u9009\u3002reduce\u5219\u662f\u5f52\u7ea6\u51fd\u6570\uff0c\u5c06\u4e00\u7cfb\u5217\u8f93\u5165\u53d8\u91cf\u7ecf\u8fc7\u7279\u5b9a\u7684\u51fd\u6570\u540e\u8f6c\u5316\u4e3a\u4e00\u4e2a\u7ed3\u679c\u8f93\u51fa\u3002\u4e0d\u8fc7\u53ef\u80fd\u662f\u7531\u4e8e\u5e94\u7528\u573a\u666f\u6709\u9650\u7684\u539f\u56e0\uff0creduce\u5728python3\u4e2d\u5df2\u4e0d\u518d\u662f\u5168\u5c40\u8c03\u7528\u51fd\u6570\uff0c\u5fc5\u987b\u8981\u4ecefunctools\u5305\u4e2d\u5bfc\u5165\u65b9\u53ef\u4f7f\u7528\uff1a<\/p>\n

from functools import reduce \r\na = range(5) \r\nreduce(lambda x, y: x+y, a) \r\n# 10 \r\n<\/pre>\n

reduce\u51fd\u6570\u8fd8\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u53ef\u9009\u7684\u521d\u59cb\u503c\u4f5c\u4e3a\u53c2\u6570\u3002\u5e94\u7528reduce\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u5f88\u591a\u5c0ftrick\uff0c\u5c31\u770b\u80fd\u4e0d\u80fd\u60f3\u7684\u5230\u7528\u7684\u51fa\uff1a<\/p>\n

from functools import reduce \r\ns = 'abcdefg' \r\nreduce(lambda x, y: y+x, s, 'AA') \r\n# 'gfedcbaAA' \r\n<\/pre>\n
06 \u6700\u540e<\/strong><\/div>\n

\u5f53\u7136\uff0c\u5c3d\u7ba1\u51e0\u4e2a\u51fd\u6570\u7528\u6cd5\u4e0d\u53ef\u8c13\u4e0d\u4f18\u7f8e\u3001\u529f\u80fd\u4e0d\u53ef\u8c13\u4e0d\u5f3a\u5927\uff0c\u4f46\u90fd\u4e0d\u662f\u5fc5\u987b\u7684\uff0c\u751a\u81f3\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u90fd\u53ef\u4ee5\u7528\u5176\u4ed6\u5f62\u5f0f\u52a0\u4ee5\u66ff\u4ee3\uff0c\u4f8b\u5982map\u548cfilter\u51fd\u6570\u90fd\u53ef\u4ee5\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u7b80\u5355\u5b9e\u73b0\uff0creduce\u51fd\u6570\u529f\u80fd\u4e5f\u9876\u591a\u7528\u4e00\u4e2afor\u5faa\u73af\u8fed\u4ee3\u5c31\u80fd\u89e3\u51b3\u3002<\/p>\n

\u4f46\u5728\u81f4\u529b\u4e8e\u4f7fpython\u7a0b\u5e8f\u53d8\u5f97\u66f4\u52a0\u7b80\u6d01\u4f18\u96c5\u4e43\u81f3\u800d\u79c0\u7684\u8def\u4e0a\uff0c\u8fd9\u4e9b\u51fd\u6570\u4e5f\u786e\u6709\u5176\u7528\u6b66\u4e4b\u5730\uff0c\u672a\u5c1d\u4e0d\u503c\u5f97\u4e00\u8bd5!<\/p>\n","protected":false},"excerpt":{"rendered":"

\u4f17\u6240\u5468\u77e5\uff0cpython\u529f\u80fd\u5f3a\u5927\u3001\u8bed\u6cd5\u7075\u6d3b\uff0c\u8fd9\u4e9b\u5f97\u76ca\u4e8e\u5176\u4e30\u5bcc\u800c\u5f3a\u5927\u7684\u5e93\u3002\u9664\u4e86\u4f17\u591a\u7b2c\u4e09\u65b9\u5e93\u548c\u65b9\u6cd5\u51fd\u6570\uff0cpython […]<\/p>\n","protected":false},"author":1898,"featured_media":175911,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[153],"class_list":["post-175908","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/175908","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=175908"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/175908\/revisions"}],"predecessor-version":[{"id":175913,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/175908\/revisions\/175913"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/175911"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=175908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=175908"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=175908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}