[英]Sorting in a List of Dictionaries with Letters & Numbers
I have a list of dictionaries that I'm trying to sort.我有一个要排序的字典列表。 This is what I have so far:这是我到目前为止所拥有的:
output_list = [{'mac': '0123.4567.89ab', 'port': 'Gi1/0/10'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/1'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/5'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/48'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/1'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/6'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/4'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/13'},
{'mac': '0123.4567.89ab', 'port': 'Gi8/0/9'},
{'mac': '0123.4567.89ab', 'port': 'Gi8/0/8'},
{'mac': '0123.4567.89ab', 'port': 'Te1/1/1'}]
mac_list = sorted(output_list, key=lambda d: "/".join([x for x in d['port'].split("/")]))
pprint(mac_list)
However, it's not sorting the dictionaries the way I want.但是,它并没有按照我想要的方式对字典进行排序。
"C:\Program Files\Python310\python.exe" "C:/Scripts/Python/test1.py"
[{'mac': '0123.4567.89ab', 'port': 'Gi1/0/1'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/10'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/48'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/5'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/6'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/1'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/13'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/4'},
{'mac': '0123.4567.89ab', 'port': 'Gi8/0/8'},
{'mac': '0123.4567.89ab', 'port': 'Gi8/0/9'},
{'mac': '0123.4567.89ab', 'port': 'Te1/1/1'}]
Process finished with exit code 0
How can I get it to sort it so it looks like this:我怎样才能让它排序它看起来像这样:
[{'mac': '0123.4567.89ab', 'port': 'Gi1/0/1'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/5'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/6'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/10'},
{'mac': '0123.4567.89ab', 'port': 'Gi1/0/48'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/1'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/4'},
{'mac': '0123.4567.89ab', 'port': 'Gi2/0/13'},
{'mac': '0123.4567.89ab', 'port': 'Gi8/0/8'},
{'mac': '0123.4567.89ab', 'port': 'Gi8/0/9'},
{'mac': '0123.4567.89ab', 'port': 'Te1/1/1'}]
Try:尝试:
out = sorted(
output_list,
key=lambda d: (d["port"][:2], *map(int, d["port"][2:].split("/"))),
)
print(out)
Prints:印刷:
[
{"mac": "0123.4567.89ab", "port": "Gi1/0/1"},
{"mac": "0123.4567.89ab", "port": "Gi1/0/5"},
{"mac": "0123.4567.89ab", "port": "Gi1/0/6"},
{"mac": "0123.4567.89ab", "port": "Gi1/0/10"},
{"mac": "0123.4567.89ab", "port": "Gi1/0/48"},
{"mac": "0123.4567.89ab", "port": "Gi2/0/1"},
{"mac": "0123.4567.89ab", "port": "Gi2/0/4"},
{"mac": "0123.4567.89ab", "port": "Gi2/0/13"},
{"mac": "0123.4567.89ab", "port": "Gi8/0/8"},
{"mac": "0123.4567.89ab", "port": "Gi8/0/9"},
{"mac": "0123.4567.89ab", "port": "Te1/1/1"},
]
Note:笔记:
(d["port"][:2], *map(int, d["port"][2:].split("/")))
will create tuple in form ('Gi', 1, 0, 48)
etc. The sorted()
function will then sort according these tuples. (d["port"][:2], *map(int, d["port"][2:].split("/")))
将以('Gi', 1, 0, 48)
等等。 sorted()
函数将根据这些元组进行排序。
Try this:尝试这个:
mac_list = sorted(output_list, key=lambda d: tuple(int(x) if x.isdigit() else x for x in d['port'].split("/")))
This will convert numeric strings to int
values in the substrings of the port
value produced by split()
and leave non-numeric strings as is, and sorting based on the resulting tuple
values will get you the desired output.这会将数字字符串转换为split()
生成的port
值的子字符串中的int
值,并将非数字字符串保持原样,并且基于结果tuple
值的排序将为您提供所需的输出。
Seems that you're looking for human/natural sorting.似乎您正在寻找人类/自然排序。 This answer covers it well.这个答案很好地涵盖了它。
Using the natural_keys
function from that answer, the port string could then be passed directly to the defined function as follow:使用该答案中的natural_keys
函数,然后可以将端口字符串直接传递给定义的函数,如下所示:
output_list.sort(key=lambda d: natural_keys(d["port"]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.