{"id":6565,"date":"2018-05-06T15:56:23","date_gmt":"2018-05-06T07:56:23","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=6565"},"modified":"2018-05-06T16:12:02","modified_gmt":"2018-05-06T08:12:02","slug":"%e4%b8%80%e4%b8%aa%e5%9c%a8%e5%be%aa%e7%8e%af%e4%b8%ad%e4%bf%ae%e6%94%b9%e5%ad%97%e5%85%b8%e7%9a%84%e5%a5%87%e6%80%aa%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/6565.html","title":{"rendered":"\u4e00\u4e2a\u5728\u5faa\u73af\u4e2d\u4fee\u6539\u5b57\u5178\u7684\u5947\u602a\u95ee\u9898"},"content":{"rendered":"<p>\u6211\u4eec\u77e5\u9053\u5728\u5faa\u73af\u4f53\u4e2d\u53bb\u6539\u53d8\u4e00\u4e2a list\uff0c\u662f\u4e0d\u5b89\u5168\u7684\u4f5c\u6cd5\uff0c\u53ef\u80fd\u5bfc\u81f4\u9519\u8bef\u7684\u7ed3\u679c\u3002\u4f8b\u5982\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570a\u548cb\uff0c\u529f\u80fd\u662f\u5728\u5217\u8868a\u4e2d\u5220\u9664\u6240\u6709b\u4e2d\u51fa\u73b0\u7684\u5143\u7d20\u3002<\/p>\n<p>\u4ee5\u4e0b\u8fd9\u79cd\u5199\u6cd5\u5c31\u4e0d\u6b63\u786e\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef remove_el(org, dest):\r\n    for i, v in enumerate(org):\r\n        if v in dest:\r\n            org.pop(i)\r\n<\/pre>\n<p>\u8f93\u5165 a = [1, 3, 3, 4, 4, 5]\uff0c b = [3, 4]\uff0c \u7ed3\u679ca\u53d8\u6210\u4e86 [1, 3, 4, 5]\uff0c\u800c\u4e0d\u662f\u9884\u671f\u7684 [1, 5]\u3002<\/p>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/05\/\u5c4f\u5e55\u5feb\u7167-2018-05-06-\u4e0b\u53482.50.20.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6572\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/05\/\u5c4f\u5e55\u5feb\u7167-2018-05-06-\u4e0b\u53482.50.20.png\" alt=\"\" width=\"377\" height=\"315\" \/><\/a><\/p>\n<p>\u7c7b\u4f3c\u7684\uff0c\u5728\u5faa\u73af\u8bed\u53e5\u4e2d\u53bb\u4fee\u6539\u4e00\u4e2a\u5b57\u5178\uff0c\u4e5f\u662f\u4e0d\u5b89\u5168\u7684\u3002\u4e3e\u4e2a\u5b9e\u9645\u4f8b\u5b50\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nIn &#x5B;1]: a = {'a':0}\r\n\r\nIn &#x5B;2]: for k in a:\r\n   ...:     a&#x5B;'0.'+k] = a.pop(k)\r\n   ...: \r\n\r\nIn &#x5B;3]: a\r\nOut&#x5B;3]: {'0.a': 0}\r\n\r\nIn &#x5B;4]: a = {'a':0}\r\n\r\nIn &#x5B;5]: for k in a:\r\n   ...:     a&#x5B;'0'+k] = a.pop(k)\r\n   ...:     \r\n\r\nIn &#x5B;6]: a\r\nOut&#x5B;6]: {'00a': 0}\r\n<\/pre>\n<p>\u4e3a\u4ec0\u4e48\u540c\u6837\u7684\u903b\u8f91\uff0c\u4e0a\u9762\u4ee3\u7801\u7247\u65ad\u5c45\u7136\u6709\u4e24\u79cd\u4e0d\u540c\u7684\u8f93\u51fa\u7ed3\u679c\uff1f<\/p>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/05\/\u5c4f\u5e55\u5feb\u7167-2018-05-06-\u4e0b\u53482.57.33.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-6573\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/05\/\u5c4f\u5e55\u5feb\u7167-2018-05-06-\u4e0b\u53482.57.33.png\" alt=\"\" width=\"300\" height=\"316\" \/><\/a><\/p>\n<p>\u8981\u7406\u89e3python\u7684\u8fd9\u79cd\u884c\u4e3a\uff0c\u5f97\u4ece\u5b83\u7684dict\u7c7b\u578b\u5185\u90e8\u5b9e\u73b0\u8bf4\u8d77\u3002<\/p>\n<p>python\u7684\u5b57\u5178\u91c7\u7528\u54c8\u5e0c\u8868\u5b9e\u73b0\uff0c\u5728\u521d\u59cb\u5316\u5b57\u5178a\u7684\u65f6\u5019\uff0c\u4f1a\u7ed9\u5b83\u5206\u914d\u4e00\u4e2a\u521d\u59cb\u5316\u957f\u5ea6\u4e3a8\u7684\u6570\u7ec4\uff0c\u63a5\u7740\u7ed9a\u8bbe\u7f6e\u503c\u7684\u65f6\u5019\uff0c\u901a\u8fc7hash\u7b97\u6cd5\u7b97\u51fakey\u7684\u4e00\u4e2a\u6574\u6570\u503c\uff0c\u518d\u901a\u8fc7\u8fd0\u7b97\u5c06\u5176\u6620\u5c04\u5230\u5bf9\u5e94\u6570\u7ec4\u4e2d\u7684\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u5728\u8fd9\u4e2a\u4f4d\u7f6e\u4e0a\u653e\u5165\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<p>\u4f8b\u5982\u6211\u4eec\u6267\u884c a = {&#8216;a&#8217;:0} \u7684\u65f6\u5019\uff0c\u4f1a\u8ba1\u7b97 a \u8fd9\u4e2akey\u7684hash\u503c\u5bf9\u5e94\u7684\u6570\u7ec4\u7d22\u5f15<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt; hash('a') &amp; 7\r\n0\r\n<\/pre>\n<p>\u8fd9\u6837a\u5143\u7d20\u5bf9\u5e94\u7684\u503c\u5c31\u88ab\u653e\u5230\u5b57\u5178\u4e2d\u7684\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u4e86\uff0c\u8fd9\u65f6\u7684\u5b57\u5178\u5927\u6982\u662f\u8fd9\u4e2a\u6837\u5b50\u7684\uff1a<\/p>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/05\/1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6566\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/05\/1.png\" alt=\"\" width=\"547\" height=\"406\" \/><\/a><\/p>\n<p>\u5728\u6267\u884c\u4e0a\u9762\u7b2c\u4e00\u4e2afor\u5faa\u73af\u65f6\uff0c\u6ce8\u610f for k in a \u8fd9\u79cd\u8bed\u6cd5\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u8fed\u4ee3\u6307\u9488\u6700\u5f00\u59cb\u6307\u5411\u6570\u7ec4\u4e2d\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u7136\u540e\u5c06\u5176 pop \u51fa\u6765\uff0c\u8fd9\u65f6\u5b57\u5178\u4e3a\u7a7a\u4e86\uff0c\u518d\u8ba1\u7b97 0.a \u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt; hash('0.a') &amp; 7\r\n0\r\n<\/pre>\n<p>\u8ba1\u7b97\u51fa\u6765\u7684\u8fd8\u662f\u4f4d\u7f6e0\uff0c\u800c\u8fd9\u4e2a\u65f6\u5019\u4f4d\u7f6e0\u7684\u539f\u672c\u503c\u5df2\u7ecf\u88ab\u8bed\u53e5 a.pop(k) \u7ed9\u5220\u9664\u6389\u4e86\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u8bbe\u7f6e\u503c\uff0c\u5b8c\u6210\u540e\u5b57\u5178\u6210\u4e3a\u8fd9\u6837\uff1a<\/p>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/05\/2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6567\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/05\/2.png\" alt=\"\" width=\"547\" height=\"406\" \/><\/a><\/p>\n<p>\u518d\u63a5\u7740 for \u5faa\u73af\u63a5\u7740\u5411\u6570\u7ec4\u5f80\u540e\u79fb\uff0c\u4f46\u7531\u4e8e\u5b57\u5178\u5185\u6570\u7ec4\u540e\u9762\u7684\u503c\u5168\u4e3a\u7a7a\uff0c\u6240\u4ee5for\u4e5f\u5c31\u4e0d\u8d77\u4f5c\u7528\u4e86\uff0c\u6700\u540e\u76f4\u63a5\u9000\u51fa\uff0c\u5b8c\u6210\u540ea\u5b57\u5178\u5c31\u5269\u4e0a\u9762\u8fd9\u4e2a\u56fe\u8fd9\u6837\u7684\u4e86\uff0c\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<p>\u4e0e\u7b2c\u4e00\u4e2afor\u5faa\u73af\u4e0d\u4e00\u6837\u7684\u662f\uff0c\u540e\u9762\u7b2c\u4e8c\u6b21 for k in a \u6267\u884c\u7684\u65f6\u5019\uff0c\u8bbe\u7f6e\u7684\u952e\u4e3a 0a \uff0c\u8ba1\u7b97\u51fa\u6765\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\u662f 3\uff0c\u4e5f\u5c31\u662f\u7b2c4\u4e2a\u4f4d\u7f6e<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt; hash('0a') &amp; 7\r\n3\r\n<\/pre>\n<p>\u8fd9\u6837\u7684\u8bdd\uff0c\u8fd9\u4e2afor\u5faa\u73af\u6267\u884c\u5b8c\u7b2c\u4e00\u6b21\u540e\uff0c\u53d8\u6210\u4e86\u8fd9\u4e2a\u6837\u5b50\uff1a<\/p>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/05\/3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6568\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/05\/3.png\" alt=\"\" width=\"724\" height=\"406\" \/><\/a><\/p>\n<p>\u63a5\u7740for\u5faa\u73af\u7684\u6e38\u6807\u7ee7\u7eed\u5411\u4e0b\u8d70\uff0c\u9047\u5230\u4e86\u6211\u4eec\u521a\u8bbe\u7f6e\u8fc7\u7684\u503c 0a\uff0c\u518d\u6267\u884c a.pop(k)\uff0c\u628a 0a\u5220\u9664\uff0c\u5e76\u8ba1\u7b97 00a \u7684\u4f4d\u7f6e\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt; hash('00a') &amp; 7\r\n2\r\n<\/pre>\n<p>\u63a5\u7740\u4fbf\u5728\u7b2c3\u4e2a\u4f4d\u7f6e\u4e0a\u8bbe\u7f6e\u65b0\u7684\u503c\uff0c\u8fd9\u65f6\u5b57\u5178\u957f\u8fd9\u4e2a\u6837\u5b50\u4e86\uff1a<\/p>\n<p><a href=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/05\/4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-6569\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2018\/05\/4.png\" alt=\"\" width=\"712\" height=\"406\" \/><\/a><\/p>\n<p>\u7136\u540efor\u5faa\u73af\u63a5\u7740\u5f80\u4e0b\u6267\u884c\uff0c\u5269\u4e0b\u5b57\u5178\u7684\u6570\u7ec4\u6ca1\u6709\u503c\u4e86\uff0c\u6240\u4ee5for\u9000\u51fa\uff0c\u5b8c\u6210\u540e\u5b57\u5178\u7684\u503c\u5c31\u662f\u4e0a\u9762\u7684\u8fd9\u6837\u5b50\u3002<\/p>\n<p>\u6240\u4ee5\u7406\u89e3\u8fd9\u4e2a\u95ee\u9898\u7684\u6838\u5fc3\u4e3b\u8981\u6709\u4e24\u70b9\uff1a<\/p>\n<ul>\n<li>for\u8bed\u6cd5\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u8fed\u4ee3\u5bf9\u8c61<\/li>\n<li>\u5b57\u5178\u8bbe\u7f6e\u952e\u503c\u65f6\uff0c\u6839\u636e\u7684hash\u503c\u6765\u8ba1\u7b97\u5bf9\u5e94\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c\u6539\u6210 for k in a.keys() \uff0c\u5c31\u6bd4\u8f83\u5b89\u5168\uff0c\u4e0d\u4f1a\u51fa\u73b0\u4e24\u6b21\u6267\u884c\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002<\/p>\n<p>\u53c2\u8003\uff1a<a href=\"http:\/\/python.jobbole.com\/85040\/\">\u300a\u6df1\u5165 Python \u5b57\u5178\u7684\u5185\u90e8\u5b9e\u73b0\u300b<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u77e5\u9053\u5728\u5faa\u73af\u4f53\u4e2d\u53bb\u6539\u53d8\u4e00\u4e2a list\uff0c\u662f\u4e0d\u5b89\u5168\u7684\u4f5c\u6cd5\uff0c\u53ef\u80fd\u5bfc\u81f4\u9519\u8bef\u7684\u7ed3\u679c\u3002\u4f8b\u5982\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570a\u548cb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-6565","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=6565"}],"version-history":[{"count":4,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6565\/revisions"}],"predecessor-version":[{"id":6576,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6565\/revisions\/6576"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=6565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=6565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=6565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}