{"id":41668,"date":"2023-12-11T14:54:14","date_gmt":"2023-12-11T06:54:14","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=41668"},"modified":"2023-12-11T14:54:14","modified_gmt":"2023-12-11T06:54:14","slug":"%e5%88%97%e4%b8%bepython%e5%b8%b8%e8%a7%81%e7%9a%84%e5%86%85%e7%bd%ae%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/%e5%88%97%e4%b8%bepython%e5%b8%b8%e8%a7%81%e7%9a%84%e5%86%85%e7%bd%ae%e5%87%bd%e6%95%b0\/","title":{"rendered":"\u5217\u4e3epython\u5e38\u89c1\u7684\u5185\u7f6e\u51fd\u6570"},"content":{"rendered":"<p>&#8220;`&#8221;<\/p>\n<p>&lt;h4&gt;abs\uff08\uff09&lt;\/h4&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;\u8fd4\u56de\u6570\u5b57\u7684\u7edd\u5bf9\u503c<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u00a0<\/p>\n<p>&lt;h4&gt;map&lt;\/h4&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;\u6839\u636e\u51fd\u6570\u5bf9\u6307\u5b9a\u5e8f\u5217\u505a\u6620\u5c04map()\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u662f\u51fd\u6570\uff0c\u4e00\u4e2a\u662f\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0cmap\u5c06\u4f20\u5165\u7684\u51fd\u6570\u4f9d\u6b21\u4f5c\u7528\u5230\u5e8f\u5217\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u628a\u7ed3\u679c\u4f5c\u4e3a\u65b0\u7684list\u8fd4\u56de\u3002\u8fd4\u56de\u503c\uff1a\u3000\u3000Python2  \u8fd4\u56de\u5217\u8868\u3000\u3000Python3  \u8fd4\u56de\u8fed\u4ee3\u5668\u4f8b\u5b501\uff1a<br \/>\ndef mul(x):<br \/>\n    return x*x<br \/>\nn=[1,2,3,4,5]<br \/>\nres=list(map(mul,n))<br \/>\nprint(res)  #[1, 4, 9, 16, 25]\u4f8b\u5b502\uff1aabs()  \u8fd4\u56de\u6570\u5b57\u7684\u7edd\u5bf9\u503cret = map(abs,[-1,-5,6,-7])print(list(ret))# [1, 5, 6, 7]<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>&lt;h4&gt;filter&lt;\/h4&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;filter()\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u51fd\u6570 f(\u51fd\u6570)\u548c\u4e00\u4e2alist\uff08\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff09\uff0c\u8fd9\u4e2a\u51fd\u6570 f\u7684\u4f5c\u7528\u662f\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u5224\u65ad\uff0c\u8fd4\u56de True\u6216 False\uff0c<br \/>\nfilter()\u6839\u636e\u5224\u65ad\u7ed3\u679c\u81ea\u52a8\u8fc7\u6ee4\u6389\u4e0d\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u8fd4\u56de\u7531\u7b26\u5408\u6761\u4ef6\u5143\u7d20\u7ec4\u6210\u7684\u65b0list\u3002<br \/>\ndef is_odd(x):<br \/>\n    return x % 2 == 1<\/p>\n<p>v=list(filter(is_odd, [1, 4, 6, 7, 9, 12, 17]))<br \/>\nprint(v)  #[1, 7, 9, 17]<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u00a0<\/p>\n<p>&lt;h4&gt;&lt;strong&gt;map\u4e0efilter\u603b\u7ed3&lt;\/strong&gt;&lt;\/h4&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;# filter \u4e0e map \u603b\u7ed3<br \/>\n# \u53c2\u6570: \u90fd\u662f\u4e00\u4e2a\u51fd\u6570\u540d + \u53ef\u8fed\u4ee3\u5bf9\u8c61<br \/>\n# \u8fd4\u56de\u503c: \u90fd\u662f\u8fd4\u56de\u53ef\u8fed\u4ee3\u5bf9\u8c61<br \/>\n# \u533a\u522b:<br \/>\n# filter \u662f\u505a\u7b5b\u9009\u7684\uff0c\u7ed3\u679c\u8fd8\u662f\u539f\u6765\u5c31\u5728\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u9879<br \/>\n# map \u662f\u5bf9\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u6bcf\u4e00\u9879\u505a\u64cd\u4f5c\u7684\uff0c\u7ed3\u679c\u4e0d\u4e00\u5b9a\u662f\u539f\u6765\u5c31\u5728\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u7684\u9879<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u00a0<\/p>\n<p>&lt;h4&gt;&lt;strong&gt;isinstance\\type&lt;\/strong&gt;&lt;\/h4&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;isinstance() \u51fd\u6570\u6765\u5224\u65ad\u4e00\u4e2a\u5bf9\u8c61\u662f\u5426\u662f\u4e00\u4e2a\u5df2\u77e5\u7684\u7c7b\u578b\uff0c\u7c7b\u4f3c type()\u3002<br \/>\nisinstance() \u4e0e type() \u533a\u522b\uff1a<br \/>\ntype() \u4e0d\u4f1a\u8ba4\u4e3a\u5b50\u7c7b\u662f\u4e00\u79cd\u7236\u7c7b\u7c7b\u578b\uff0c\u4e0d\u8003\u8651\u7ee7\u627f\u5173\u7cfb\u3002<br \/>\nisinstance() \u4f1a\u8ba4\u4e3a\u5b50\u7c7b\u662f\u4e00\u79cd\u7236\u7c7b\u7c7b\u578b\uff0c\u8003\u8651\u7ee7\u627f\u5173\u7cfb\u3002<br \/>\n\u5982\u679c\u8981\u5224\u65ad\u4e24\u4e2a\u7c7b\u578b\u662f\u5426\u76f8\u540c\u63a8\u8350\u4f7f\u7528 isinstance()\u3002<br \/>\n# \u4f8b\u4e00a = 2print(isinstance(a,int))   # Trueprint(isinstance(a,str))   # False<\/p>\n<p># type() \u4e0e isinstance() \u533a\u522b<br \/>\nclass A:<br \/>\n    pass<\/p>\n<p>class B(A):<br \/>\n    pass<\/p>\n<p>print(&quot;&quot;isinstance&quot;&quot;,isinstance(A(),A))   # isinstance True<br \/>\nprint(&quot;&quot;type&quot;&quot;,type(A())  == A)    # type True<\/p>\n<p>print(&#039;isinstance&#039;,isinstance(B(),A) )   # isinstance True<br \/>\nprint(&#039;type&#039;,type(B()) == A)     #  type False<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>&lt;h4&gt;zip \u62c9\u94fe\u51fd\u6570&lt;\/h4&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;# zip \u62c9\u94fe\u51fd\u6570\uff0c<br \/>\n# \u5c06\u5bf9\u8c61\u4e2d\u5bf9\u5e94\u7684\u5143\u7d20\u6253\u5305\u6210\u4e00\u4e2a\u4e2a\u5143\u7ec4\uff0c<br \/>\n# \u7136\u540e\u8fd4\u56de\u7531\u8fd9\u4e9b\u5143\u7ec4\u7ec4\u6210\u7684\u5217\u8868\u8fed\u4ee3\u5668\u3002<br \/>\n# \u5982\u679c\u5404\u4e2a\u8fed\u4ee3\u5668\u7684\u5143\u7d20\u4e2a\u6570\u4e0d\u4e00\u81f4\uff0c\u5219\u8fd4\u56de\u5217\u8868\u957f\u5ea6\u4e0e\u6700\u77ed\u7684\u5bf9\u8c61\u76f8\u540c\u3002<br \/>\nprint(list(zip([0,1,3],[5,6,7],[&#039;a&#039;,&#039;b&#039;])))<br \/>\n# [(0, 5, &#039;a&#039;), (1, 6, &#039;b&#039;)]<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code&gt;zip() \u51fd\u6570\u7528\u4e8e\u5c06\u53ef\u8fed\u4ee3\u7684\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5c06\u5bf9\u8c61\u4e2d\u5bf9\u5e94\u7684\u5143\u7d20\u6253\u5305\u6210\u4e00\u4e2a\u4e2a\u5143\u7ec4\uff0c\u7136\u540e\u8fd4\u56de\u7531\u8fd9\u4e9b\u5143\u7ec4\u7ec4\u6210\u7684\u5217\u8868\u3002<br \/>\n&gt;&gt;&gt;a = [1,2,3]<br \/>\n&gt;&gt;&gt; b = [4,5,6]<br \/>\n&gt;&gt;&gt; c = [4,5,6,7,8]<br \/>\n&gt;&gt;&gt; zipped = zip(a,b)     # \u6253\u5305\u4e3a\u5143\u7ec4\u7684\u5217\u8868<br \/>\n[(1, 4), (2, 5), (3, 6)]<br \/>\n&gt;&gt;&gt; zip(a,c)              # \u5143\u7d20\u4e2a\u6570\u4e0e\u6700\u77ed\u7684\u5217\u8868\u4e00\u81f4<br \/>\n[(1, 4), (2, 5), (3, 6)]<br \/>\n&gt;&gt;&gt; zip(*zipped)          # \u4e0e zip \u76f8\u53cd\uff0c\u53ef\u7406\u89e3\u4e3a\u89e3\u538b\uff0c\u8fd4\u56de\u4e8c\u7ef4\u77e9\u9635\u5f0f<br \/>\n[(1, 2, 3), (4, 5, 6)]<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>&lt;strong&gt;reduce&lt;\/strong&gt;<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code&gt;&#039;&#039;&#039;<br \/>\nreduce()  \u51fd\u6570<br \/>\nreduce() \u51fd\u6570\u4f1a\u5bf9\u53c2\u6570\u5e8f\u5217\u4e2d\u5143\u7d20\u8fdb\u884c\u7d2f\u79ef<br \/>\n\u51fd\u6570\u5c06\u4e00\u4e2a\u6570\u636e\u96c6\u5408(\u94fe\u8868\u3001\u5143\u7ec4\u7b49)\u4e2d\u7684\u6240\u6709\u6570\u636e\u8fdb\u884c\u4e0b\u5217\u64cd\u4f5c<br \/>\n&#039;&#039;&#039;<\/p>\n<p>\u6ce8\u610f\uff1a<br \/>\nPython3\u5df2\u7ecf\u5c06reduce() \u51fd\u6570\u4ece\u5168\u5c40\u540d\u5b57\u7a7a\u95f4\u91cc\u79fb\u9664\u4e86\uff0c\u5b83\u73b0\u5728\u88ab\u653e\u7f6e\u5728 fucntools \u6a21\u5757\u91cc\uff0c\u5982\u679c\u60f3\u8981\u4f7f\u7528\u5b83\uff0c\u5219\u9700\u8981\u901a\u8fc7\u5f15\u5165 functools \u6a21\u5757\u6765\u8c03\u7528 reduce() \u51fd\u6570\uff1a<\/p>\n<p>from functools import reduce<br \/>\ndef add(x,y):<br \/>\n    return x + y<\/p>\n<p>print(reduce(add,[1,2,3,4,5]))<br \/>\n#  15<\/p>\n<p>print(reduce(lambda x, y: x+y, [1,2,3,4,5]))  # 15<\/p>\n<p>print(reduce(add,range(1,101)))<br \/>\n#  5050<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; &lt;h4&gt;abs\uff08\uff09&lt;\/h4&gt; &lt;pre&gt;&#038; [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-41668","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/41668","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=41668"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/41668\/revisions"}],"predecessor-version":[{"id":41669,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/41668\/revisions\/41669"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=41668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=41668"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=41668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}