{"id":7104,"date":"2015-01-20T23:31:51","date_gmt":"2015-01-20T15:31:51","guid":{"rendered":"http:\/\/jpuyy.com\/?p=7104"},"modified":"2015-01-20T23:34:32","modified_gmt":"2015-01-20T15:34:32","slug":"python-ip-sort","status":"publish","type":"post","link":"https:\/\/jpuyy.com\/?p=7104","title":{"rendered":"python ip\u5730\u5740\u6392\u5e8f"},"content":{"rendered":"<p>\u65b9\u6cd5\u4e00\uff1a<\/p>\n<pre>\r\nip_list = ['192.168.1.100', '192.168.10.3', '192.168.8.1']\r\nip_list.sort(lambda x,y: cmp(''.join( [ i.rjust(3, '0') for i in x.split('.')] ), ''.join( [ i.rjust(3, '0') for i in y.split('.')] ) ) )\r\n<\/pre>\n<p>\u7ed3\u679c<\/p>\n<pre>['192.168.1.100', '192.168.8.1', '192.168.10.3']<\/pre>\n<p>\u65b9\u6cd5\u4e8c\uff1a<\/p>\n<p>\u8f6c\u6362\u6210 int \u578b\uff0c\u7528 int \u6765\u6bd4\u8f83<\/p>\n<pre>\r\nimport struct\r\nimport socket\r\n\r\ndef ip2int(addr):\r\n    return struct.unpack(\"!I\", socket.inet_aton(addr))[0]\r\n\r\ndef int2ip(addr):\r\n    return socket.inet_ntoa(struct.pack(\"!I\", addr))\r\n\r\nip_list = ['192.168.1.100', '192.168.10.3', '192.168.8.1']\r\n# \u6784\u5efa int_ip:ip \u5f62\u5f0f\u7684 key:value, \u5e76\u5bf9 key \u6392\u5e8f\r\n\r\nip_unsorted_dict = {}\r\nfor ip in ip_list:\r\n    int_ip = ip2int(ip)\r\n    ip_unsorted_dict[int_ip] = ip\r\n\r\nkeys = ip_unsorted_dict.keys()\r\nkeys.sort()\r\nip_sorted_list=[]\r\nfor key in keys:\r\n    ip_sorted_list.append(ip_unsorted_dict[key])\r\n\r\nprint ip_sorted_list<\/pre>\n<p>\u7ed3\u679c\uff1a<\/p>\n<pre>['192.168.1.100', '192.168.8.1', '192.168.10.3']<\/pre>\n<p>\u53c2\u8003\uff1a<br \/>\nhttp:\/\/blog.csdn.net\/hong201\/article\/details\/3119519<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65b9\u6cd5\u4e00\uff1a ip_list = [&#8216;192.168.1.100&#8217;, &#8216;192.168.10.3&#8217;, &#8216;192.168.8.1&#8217;] ip_list.sort(lambda x,y: cmp(&#8221;.join( [ i.rjust(3, &#8216;0&#8217;) for i in x.split(&#8216;.&#8217;)] ), &#8221;.join( [ i.rjust(3, &#8216;0&#8217;) for i in y.split(&#8216;.&#8217;)] ) ) ) \u7ed3\u679c [&#8216;192.168.1.100&#8217;, &#8216;192.168.8.1&#8217;, &#8216;192.168.10.3&#8217;] \u65b9\u6cd5\u4e8c\uff1a \u8f6c\u6362\u6210 int \u578b\uff0c\u7528 int \u6765\u6bd4\u8f83 import struct import socket def ip2int(addr): return struct.unpack(&#8220;!I&#8221;, socket.inet_aton(addr))[0] def int2ip(addr): return socket.inet_ntoa(struct.pack(&#8220;!I&#8221;, addr)) ip_list = [&#8216;192.168.1.100&#8217;, [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[],"class_list":["post-7104","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/7104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7104"}],"version-history":[{"count":4,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/7104\/revisions"}],"predecessor-version":[{"id":7108,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/7104\/revisions\/7108"}],"wp:attachment":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}