如何按两个值对字典列表进行排序,其中一个值是IP地址

时间:2016-06-01 16:41:37

标签: python list sorting dictionary ip

我在Python中有一个列表,显示了某些机器的网络位置:

[{"address": "54.23.65.111", "role": "logic"}, 
{"address": "51.63.68.115", "role": "logic"},    
{"address": "79.23.65.111", "role": "output"},    
{"address": "52.23.65.111", "role": "logic"}]

我希望这首先按角色排序,然后按地址按数字排序,因此输出应如下所示:

[{"address": "51.63.68.115", "role": "logic"},    
{"address": "52.23.65.111", "role": "logic"},    
{"address": "54.23.65.111", "role": "logic"},     
{"address": "79.23.65.111", "role": "output"}]

到目前为止,我有:

for machine_dict in sorted(machine_list, key=lambda machine_role: (machine_role["role"], machine_role["address"])):

但是这并没有对数字进行排序,例如11.something小于2.something

3 个答案:

答案 0 :(得分:1)

要对分割地址所需的IP进行数字排序,但@sparkandshine的一般方法仍然有效,只需稍微更新密钥:

>>> list_dicts = [{"address": "11.23.65.111", "role": "logic"},
...               {"address": "2.63.68.115", "role": "logic"},
...               {"address": "79.23.65.111", "role": "output"},
...               {"address": "52.23.65.111", "role": "logic"}]
>>> sorted(list_dicts, key=lambda d: (d['role'], tuple(map(int, d['address'].split('.')))))
[{'address': '2.63.68.115', 'role': 'logic'},
 {'address': '11.23.65.111', 'role': 'logic'},
 {'address': '54.23.65.111', 'role': 'logic'},
 {'address': '79.23.65.111', 'role': 'output'}]

答案 1 :(得分:1)

要按您想要的IP地址排序,这可能有效:

import socket
print sorted(machine_list, 
             key = lambda machine_role: (machine_role["role"],
                                         socket.inet_aton(machine_role["address"])))

输入

[
    {'role': 'logic',  'address': '1.2.3.4'}, 
    {'role': 'logic',  'address': '1.11.2.3'}, 
    {'role': 'logic',  'address': '1.1.2.3'}, 
    {'role': 'logic',  'address': '2.3.4.5'}
]

输出

[
    {'role': 'logic', 'address': '1.1.2.3'}, 
    {'role': 'logic', 'address': '1.2.3.4'}, 
    {'role': 'logic', 'address': '1.11.2.3'}, 
    {'role': 'logic', 'address': '2.3.4.5'}
]

答案 2 :(得分:0)

使用<div id="cssmenu" class="align-right mobile-align-center"> <ul> <li><a href="link">Home</a></li> <li><a href="#">About</a> <ul> <li><a href="#">About The Org</a></li> <li><a href="link">Leadership</a></li> <li><a href="link">FAQs</a></li> </ul> </li> <li><a href="#">Divisions</a> <ul> <li><a href="#">Division 1</a> <ul> <li><a href="link">D1, Branch 1</a></li> <li><a href="link">D1, Branch 2</a></li> <li><a href="link">D1, Branch 3</a></li> </ul> </li> <li><a href="#">Division 2</a> <ul> <li><a href="link">D2, Branch 1</a></li> <li><a href="link">D2, Branch 2</a></li> </ul> </li> </ul> </li> <li><a href="link">Contact</a></li> </ul> </div> 将IP地址转换为int的列表,即.split('.')

[int(i) for i in d['address'].split('.')]

或使用Python模块ipaddress

list_dicts = [  {"address": "11.23.65.111", "role": "logic"},
                {"address": "2.63.68.115", "role": "logic"},
                {"address": "79.23.65.111", "role": "output"},
                {"address": "52.23.65.111", "role": "logic"}]


result = sorted(list_dicts, key=lambda d: (d['role'], [int(i) for i in d['address'].split('.')]))

print(result)
# Output
[{'role': 'logic', 'address': '2.63.68.115'}, {'role': 'logic', 'address': '11.23.65.111'}, {'role': 'logic', 'address': '52.23.65.111'}, {'role': 'output', 'address': '79.23.65.111'}]