{"id":1714,"date":"2014-12-22T13:46:11","date_gmt":"2014-12-22T13:46:11","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=1714"},"modified":"2019-06-29T07:13:37","modified_gmt":"2019-06-28T23:13:37","slug":"python%e4%b8%ad%e7%9a%84%e5%b5%8c%e5%a5%97%e5%ad%97%e5%85%b8%e6%93%8d%e4%bd%9c%e5%b0%8f%e7%bb%93","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/1714.html","title":{"rendered":"Python\u4e2d\u7684\u5d4c\u5957\u5b57\u5178\u64cd\u4f5c\u5c0f\u7ed3"},"content":{"rendered":"<h5>Tips.1 \u904d\u5386\u83b7\u53d6\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u6240\u6709\u503c<\/h5>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/10756427\/loop-through-all-nested-dictionary-values\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/stackoverflow.com\/questions\/10756427\/loop-through-all-nested-dictionary-values<\/a><\/p>\n<p>\u5982\u679c\u5df2\u77e5\u5b57\u5178\u53ea\u67091\u30012\u5c42\u7684\u8bdd\uff0c\u8fd8\u53ef\u4ee5\u624b\u52a8\u5199\u4e2afor\u5faa\u73af\uff0c\u4f46\u662f\u5982\u679c\u5d4c\u5957\u5c42\u6570\u4e0d\u786e\u5b9a\u7684\u8bdd\uff0cfor\u5faa\u73af\u5c31\u4e0d\u9760\u8c31\u4e86\uff0c\u9700\u8981\u4f7f\u7528\u201c\u9012\u5f52\u201d\u7684\u601d\u60f3\u4e86\u3002<\/p>\n<pre class=\"lang:default decode:true\">def myprint(d):\n    for k, v in d.iteritems():\n        if isinstance(v, dict):\n            myprint(v)\n        else:\n            print \"{0} : {1}\".format(k, v)<\/pre>\n<p>\u8fd8\u6709\u5c31\u662f\u5229\u7528Python\u7684\u5df2\u6709module\u6765\u5e2e\u5fd9\u4e86\uff1a<\/p>\n<pre class=\"lang:default decode:true\">import collections\ndef nested_dict_iter(nested):\n    for key, value in nested.iteritems():\n        if isinstance(value, collections.Mapping):\n            for inner_key, inner_value in nested_dict_iter(value):\n                yield inner_key, inner_value\n        else:\n            yield key, value\n\n&gt;&gt;&gt; list(nested_dict_iter({'a':{'b':{'c':1, 'd':2},\n                                'e':{'f':3, 'g':4}},\n                           'h':{'i':5, 'j':6}}))\n[('c', 1), ('d', 2), ('g', 4), ('f', 3), ('i', 5), ('j', 6)]<\/pre>\n<p>\u53e6\uff1a<a href=\"http:\/\/stackoverflow.com\/questions\/14692690\/access-python-nested-dictionary-items-via-a-list-of-keys\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/stackoverflow.com\/questions\/14692690\/access-python-nested-dictionary-items-via-a-list-of-keys<\/a><\/p>\n<h5>Tips.2 Python\u4e2diteritems()\u548citems()\u7684\u533a\u522b<\/h5>\n<p>\u641c\u7d22\uff1a<a href=\"http:\/\/cn.bing.com\/search?q=dict+iteritems+python\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/cn.bing.com\/search?q=dict+iteritems+python<\/a><\/p>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/13998492\/iteritems-in-python\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/stackoverflow.com\/questions\/13998492\/iteritems-in-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/10458437\/what-is-the-difference-between-dict-items-and-dict-iteritems\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/stackoverflow.com\/questions\/10458437\/what-is-the-difference-between-dict-items-and-dict-iteritems<\/a><\/li>\n<li><a href=\"http:\/\/docs.python.org\/library\/stdtypes.html#dict.items\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/docs.python.org\/library\/stdtypes.html#dict.items<\/a><\/li>\n<li><a href=\"http:\/\/docs.python.org\/library\/stdtypes.html#dict.iteritems\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/docs.python.org\/library\/stdtypes.html#dict.iteritems<\/a><\/li>\n<\/ul>\n<p><a href=\"http:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2014\/12\/dict_iter.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1716\" src=\"http:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2014\/12\/dict_iter.png\" alt=\"dict_iter\" width=\"612\" height=\"379\"><\/a><\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u603b\u7684\u6765\u8bf4<\/strong><\/span>\uff1a\u5728Python 2.x\u4e2d\uff0citems()\u8fd4\u56de\u7684\u662f\u5b9e\u5b9e\u5728\u5728\u7684tuple\u5217\u8868\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u4f1a\u8017\u8d39\u5927\u91cf\u5185\u5b58\uff1b\u4f46\u662fiteritems()\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u5728\u5185\u5b58\u6d88\u8017\u4e0a\u9762\u53ef\u80fd\u4f1a\u597d\u70b9\uff1b\u4e0d\u8fc7\uff1aUsing iteritems() while adding or deleting entries in the dictionary may raise a RuntimeError or fail to iterate over all entries.<br \/>\n\u800c\u4e14\u5728Python 3.x\u4e2d\uff0citeritems()\u88ab\u5220\u9664\u4e86\uff0c\u7edf\u4e00\u7528\u6539\u8fdb\u8fc7\u7684items()\u5904\u7406\u76f8\u5173\u95ee\u9898\u3002<\/p>\n<h5>Tips.3 \u5728Python\u4e2d\u521b\u5efa\u5d4c\u5957\u5b57\u5178\u7684\u6700\u4f73\u65b9\u6cd5<\/h5>\n<pre class=\"lang:default decode:true\">conn = {}\ntup1 = (s_addr, source_port, d_addr, dest_port, acknowledgement)\nif tup1 in conn:\n    if sequence in conn[tup1]:\n        conn[tup1][sequence].append(data)\n    else:\n        conn[tup1][sequence] = [data]\n        #conn[tup1] = {sequence:[data]} \u8fd9\u4e2a\u662f\u9519\u8bef\u7684\u65b9\u6cd5\uff0c\u6bcf\u6b21\u90fd\u662f\u91cd\u65b0\u8d4b\u503c\uff01\nelse:\n\tconn[tup1] = {sequence:[data]}<\/pre>\n<ul>\n<li><a href=\"https:\/\/docs.python.org\/2\/library\/stdtypes.html#dict.setdefault\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/docs.python.org\/2\/library\/stdtypes.html#dict.setdefault<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/635483\/what-is-the-best-way-to-implement-nested-dictionaries-in-python\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/stackoverflow.com\/questions\/635483\/what-is-the-best-way-to-implement-nested-dictionaries-in-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/15819428\/how-to-initialize-nested-dictionaries-in-python\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/stackoverflow.com\/questions\/15819428\/how-to-initialize-nested-dictionaries-in-python<\/a><\/li>\n<\/ul>\n<h5>Tips.4 Python\u4e2d\u5d4c\u5957\u5b57\u5178\u5185\u5bb9\u7684\u66f4\u65b0<\/h5>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/4841868\/python-dictionaries-how-to-keep-the-new-value-from-overwriting-the-previous-valu\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/stackoverflow.com\/questions\/4841868\/python-dictionaries-how-to-keep-the-new-value-from-overwriting-the-previous-valu<\/a><\/p>\n<p>\u5c06value\u8bbe\u7f6e\u6210list\u7c7b\u578b\uff0c\u7136\u540e\u5728\u78b0\u5230\u76f8\u540c\u7684key\u65f6\uff0c\u5f80list\u4e2dappend\u5373\u53ef\u3002<\/p>\n<h5>Tips.5 Python\u4e2d[\u5d4c\u5957]\u5b57\u5178\u7684\u6392\u5e8f<\/h5>\n<p>\u65b9\u6cd5\u5c31\u662f\uff1a<br \/>\nsortedList = sorted(dic.items(), key=lambda d:d[1], reverse=True)<\/p>\n<p>\u8981\u6ce8\u610f\u7684\u5c31\u662f\uff1a<br \/>\nsorted()\u8fd4\u56de\u7684\u662f\u4e00\u4e2alist\uff0c\u800c\u4e0d\u662fdict\u3002<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/575819\/sorting-dictionary-keys-in-python\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/stackoverflow.com\/questions\/575819\/sorting-dictionary-keys-in-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/613183\/sort-a-python-dictionary-by-value\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/stackoverflow.com\/questions\/613183\/sort-a-python-dictionary-by-value<\/a><\/li>\n<li><a href=\"http:\/\/ixyzero.com\/blog\/archives\/824.html\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/ixyzero.com\/blog\/archives\/824.html<\/a><\/li>\n<\/ul>\n<hr>\n<h6>\u5728Python\u4e2dlist\u548cdict\u7684\u904d\u5386\u7a0d\u6709\u4e0d\u540c\uff1a<\/h6>\n<p><a href=\"http:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2014\/12\/dict_items.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1715\" src=\"http:\/\/ixyzero.com\/blog\/wp-content\/uploads\/2014\/12\/dict_items.png\" alt=\"dict_items\" width=\"677\" height=\"534\"><\/a><\/p>\n<p>\u5bf9\u4e8elist\u6765\u8bf4\uff0c\u53ea\u9700\u8981\u7b80\u5355\u7684for\u5faa\u73af\u5373\u53ef\uff1b\u4f46\u662f\u5bf9\u4e8edict\u6765\u8bf4\uff0c\u9700\u8981\u6307\u5b9aitems()\u6216iteritems()\u624d\u884c\uff0c\u5426\u5219\u548c\u81ea\u5df1\u9884\u60f3\u7684\u4e0d\u592a\u4e00\u6837\u3002<\/p>\n<p>\u5728Python\u4e2d\u52a0\u8f7d\u5927\u5b57\u5178\u7684\u65b9\u6cd5\uff1a<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/5248958\/fastest-way-to-save-and-load-a-large-dictionary-in-python\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/stackoverflow.com\/questions\/5248958\/fastest-way-to-save-and-load-a-large-dictionary-in-python<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tips.1 \u904d\u5386\u83b7\u53d6\u5d4c\u5957\u5b57\u5178\u4e2d\u7684\u6240\u6709\u503c http:\/\/stackoverflow.com\/questions [&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,422,423,8],"class_list":["post-1714","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-dict","tag-items","tag-iteritems","tag-python"],"views":2209,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1714","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=1714"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1714\/revisions"}],"predecessor-version":[{"id":4516,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/1714\/revisions\/4516"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=1714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=1714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=1714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}