{"id":1981,"date":"2015-03-07T21:37:56","date_gmt":"2015-03-07T13:37:56","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1981"},"modified":"2015-03-07T21:37:56","modified_gmt":"2015-03-07T13:37:56","slug":"python%e7%9a%84%e4%b8%80%e4%ba%9b%e5%b0%8f%e7%9f%a5%e8%af%86%e7%82%b9_8","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1981.html","title":{"rendered":"Python\u7684\u4e00\u4e9b\u5c0f\u77e5\u8bc6\u70b9_8"},"content":{"rendered":"<p>\u597d\u4e45\u6ca1\u6709\u66f4\u65b0\u5173\u4e8ePython\u7684\u5c0f\u77e5\u8bc6\u70b9\u7684\u6587\u7ae0\u4e86\uff0c\u4e00\u65b9\u9762\u662f\u56e0\u4e3a\u8fc7\u5e74\u671f\u95f4\u4e8b\u60c5\u591a\uff0c\u53e6\u4e00\u65b9\u9762\u5c31\u662f\u597d\u50cf\u6ca1\u600e\u4e48\u603b\u7ed3\u4e86\uff0c\u6240\u4ee5\u4e0d\u77e5\u9053\u8be5\u5199\u4e9b\u4ec0\u4e48\uff0c\u6700\u8fd1\u78b0\u5230\u4e86\u4e9b\u5c31\u9a6c\u4e0a\u8bb0\u4e0b\u6765\u4e86\uff0c\u5426\u5219\u4ee5\u540e\u78b0\u5230\u4e86\u8fd8\u662f\u4e0d\u77e5\u9053\u2026\u2026<\/p>\n<h5>1.Python\u4e2d\u5706\u62ec\u53f7\u3001\u65b9\u62ec\u53f7\u548c\u5927\u62ec\u53f7\u7684\u533a\u522b<\/h5>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p><a href=\"http:\/\/search.aol.com\/aol\/search?q=Python+%27square+bracket%27+brace\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=Python+%27square+bracket%27+brace<\/a><\/p>\n<h6>\u5b9e\u9645\u6d4b\u8bd5\uff08\u4f7f\u7528IPython\uff09\uff1a<\/h6>\n<p><a href=\"http:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2015\/03\/tuple_set.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1982\" src=\"http:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2015\/03\/tuple_set.png\" alt=\"tuple_set\" width=\"226\" height=\"321\" \/><\/a><\/p>\n<h6>\u7ed3\u8bba\uff1a<\/h6>\n<ul>\n<li>()\u00a0\u00a0#\u5143\u7ec4\uff08\u4f46\u5728\u5355\u4e00\u5143\u7d20\u7684\u60c5\u51b5\u4e0b\u8bb0\u5f97\u5728\u6700\u540e\u6dfb\u52a0\u4e00\u4e2a\u9017\u53f7\u2019,\u2019\uff0c\u5426\u5219\u4f1a\u51fa\u9519\uff09<\/li>\n<li>[]\u00a0\u00a0#\u5217\u8868<\/li>\n<li>{}\u00a0\u00a0#\u5b57\u5178\u3001\u96c6\u5408<\/li>\n<\/ul>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/22391419\/what-is-the-difference-between-curly-brace-and-square-bracket-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/22391419\/what-is-the-difference-between-curly-brace-and-square-bracket-in-python<\/a><\/li>\n<\/ul>\n<h5>2.Python\u4e2d\u7684in\u5173\u952e\u5b57\u7684\u6548\u7387<\/h5>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p><a href=\"http:\/\/search.aol.com\/aol\/search?q=python+in+dict+efficiency\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=python+in+dict+efficiency<\/a><\/p>\n<h6>\u53c2\u8003\u7ed3\u8bba\uff1a<\/h6>\n<pre class=\"lang:default decode:true\">Sets:\nOperation     | Example      | Complexity         | Notes\n--------------+--------------+------------+-----------------------\nContainment   | x in\/not in s| O(1)\t   | compare to list\/tuple - O(N)<\/pre>\n<h6><span style=\"color: #ff0000;\">\u4f7f\u7528dict\u6216set\u800c\u4e0d\u662flist\/tuple<\/span>\uff1b<\/h6>\n<p>\u5373\uff08\u4f7f\u7528\u5927\u62ec\u53f7\u800c\u4e0d\u662f\u5706\u62ec\u53f7\uff09\uff1a<\/p>\n<p>\u4f7f\u7528\uff1aif mac_addr in {eth_addr(packet[0:6]), eth_addr(packet[6:12])}:<\/p>\n<p>\u800c\u4e0d\u662f\uff1aif mac_addr in (eth_addr(packet[0:6]), eth_addr(packet[6:12])):<\/p>\n<p>\u539f\u56e0\u53c2\u8003\u7b2c1\u70b9\u3002<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/513882\/python-list-vs-dict-for-look-up-table\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/513882\/python-list-vs-dict-for-look-up-table<\/a><\/li>\n<li><a href=\"https:\/\/wiki.python.org\/moin\/PythonSpeed\/PerformanceTips\" target=\"_blank\">https:\/\/wiki.python.org\/moin\/PythonSpeed\/PerformanceTips<\/a><\/li>\n<li><a href=\"https:\/\/www.ics.uci.edu\/~pattis\/ICS-33\/lectures\/complexitypython.txt\" target=\"_blank\">https:\/\/www.ics.uci.edu\/~pattis\/ICS-33\/lectures\/complexitypython.txt<\/a><\/li>\n<\/ul>\n<h5>3.\u5728Python\u4e2d\u521b\u5efa\u76ee\u5f55<\/h5>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p><a href=\"http:\/\/search.aol.com\/aol\/search?s_it=topsearchbox.search&amp;s_chn=prt_aol20-ns&amp;v_t=comsearch&amp;q=Python+%E5%88%9B%E5%BB%BA%E7%9B%AE%E5%BD%95\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=Python+%E5%88%9B%E5%BB%BA%E7%9B%AE%E5%BD%95<\/a><\/p>\n<h6>\u53c2\u8003\u65b9\u6cd5\uff1a<\/h6>\n<pre class=\"lang:default decode:true\">import os, errno\ndef mkdir_p(path):\n    try:\n        os.makedirs(path)\n    except OSError as exc:  # Python &gt;2.5 (except OSError, exc: for Python &lt;2.5)\n        if exc.errno == errno.EEXIST and os.path.isdir(path):\n            pass\n        else\n            raise<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/bookshadow.com\/weblog\/2014\/10\/02\/python-mkdir-p\/\" target=\"_blank\">http:\/\/bookshadow.com\/weblog\/2014\/10\/02\/python-mkdir-p\/<\/a><\/li>\n<li><a href=\"http:\/\/www.cnblogs.com\/zhoujie\/archive\/2013\/04\/10\/python7.html\" target=\"_blank\">http:\/\/www.cnblogs.com\/zhoujie\/archive\/2013\/04\/10\/python7.html<\/a><\/li>\n<\/ul>\n<h5>4.\u5728Python\u4e2d\u5220\u9664dict\u4e2d\u7684\u67d0\u4e2a\u5143\u7d20<\/h5>\n<h6>\u7ed3\u8bba\uff1a<\/h6>\n<pre class=\"lang:default decode:true\">del dict['item_name']\n\u6216\ndict.pop(\"key\", None)<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/5844672\/delete-an-element-from-a-dictionary\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/5844672\/delete-an-element-from-a-dictionary<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/11277432\/how-to-remove-a-key-from-dictionary\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/11277432\/how-to-remove-a-key-from-dictionary<\/a><\/li>\n<li><a href=\"http:\/\/docs.python.org\/reference\/simple_stmts.html#grammar-token-del_stmt\" target=\"_blank\">http:\/\/docs.python.org\/reference\/simple_stmts.html#grammar-token-del_stmt<\/a><\/li>\n<\/ul>\n<h5>5.\u83b7\u53d6Python\u4e2d\u67d0\u4e2a\u5bf9\u8c61\u7684\u5927\u5c0f\u4ee5\u53ca\u5360\u7528\u7a7a\u95f4\u5927\u5c0f<\/h5>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p><a href=\"http:\/\/search.aol.com\/aol\/search?q=python+get+dict+size\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=python+get+dict+size<\/a><\/p>\n<p>=<\/p>\n<p>There&#8217;s:<\/p>\n<blockquote><p>&gt;&gt;&gt; import sys<br \/>\n&gt;&gt;&gt; sys.getsizeof([1,2, 3])<br \/>\n96<br \/>\n&gt;&gt;&gt; a = []<br \/>\n&gt;&gt;&gt; sys.getsizeof(a)<br \/>\n72<br \/>\n&gt;&gt;&gt; a = [1]<br \/>\n&gt;&gt;&gt; sys.getsizeof(a)<br \/>\n80<\/p><\/blockquote>\n<p>But I wouldn&#8217;t say it&#8217;s that reliable, as Python has overhead for each object, and there are objects that contain nothing but references to other objects, so it&#8217;s not quite the same as in C and other languages.<br \/>\nHave a read of the docs on <a href=\"http:\/\/docs.python.org\/dev\/library\/sys#sys.getsizeof\" target=\"_blank\">sys.getsizeof<\/a> and go from there I guess.<\/p>\n<p>=<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/13530762\/how-to-know-bytes-size-of-python-object-like-arrays-and-dictionaries-the-simp\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/13530762\/how-to-know-bytes-size-of-python-object-like-arrays-and-dictionaries-the-simp<\/a><\/p>\n<h5>6.\u7528Python\u83b7\u53d6CPU\u3001\u5185\u5b58\u7684\u4f7f\u7528\u60c5\u51b5<\/h5>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p><a href=\"http:\/\/search.aol.com\/aol\/search?q=linux+get+a+running+python+script+mem+detail\" target=\"_blank\">http:\/\/search.aol.com\/aol\/search?q=linux+get+a+running+python+script+mem+detail<\/a><\/p>\n<h6>\u7ed3\u8bba\uff1a<\/h6>\n<p>\u63a8\u8350\u4f7f\u7528psutil\u6a21\u5757<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/276052\/how-to-get-current-cpu-and-ram-usage-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/276052\/how-to-get-current-cpu-and-ram-usage-in-python<\/a><\/li>\n<li><a href=\"http:\/\/fa.bianp.net\/blog\/2013\/different-ways-to-get-memory-consumption-or-lessons-learned-from-memory_profiler\/\" target=\"_blank\">http:\/\/fa.bianp.net\/blog\/2013\/different-ways-to-get-memory-consumption-or-lessons-learned-from-memory_profiler\/<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/938733\/total-memory-used-by-python-process\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/938733\/total-memory-used-by-python-process<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u597d\u4e45\u6ca1\u6709\u66f4\u65b0\u5173\u4e8ePython\u7684\u5c0f\u77e5\u8bc6\u70b9\u7684\u6587\u7ae0\u4e86\uff0c\u4e00\u65b9\u9762\u662f\u56e0\u4e3a\u8fc7\u5e74\u671f\u95f4\u4e8b\u60c5\u591a\uff0c\u53e6\u4e00\u65b9\u9762\u5c31\u662f\u597d\u50cf\u6ca1\u600e\u4e48\u603b\u7ed3\u4e86\uff0c\u6240\u4ee5 [&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,7],"tags":[220,8],"class_list":["post-1981","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-dict","tag-python"],"views":2095,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1981","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=1981"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1981\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}