[英]Sorting in a List of Dictionaries with Letters & Numbers
我有一個要排序的字典列表。 這是我到目前為止所擁有的:
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)
但是,它並沒有按照我想要的方式對字典進行排序。
"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
我怎樣才能讓它排序它看起來像這樣:
[{'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'}]
嘗試:
out = sorted(
output_list,
key=lambda d: (d["port"][:2], *map(int, d["port"][2:].split("/"))),
)
print(out)
印刷:
[
{"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"},
]
筆記:
(d["port"][:2], *map(int, d["port"][2:].split("/")))
將以('Gi', 1, 0, 48)
等等。 sorted()
函數將根據這些元組進行排序。
嘗試這個:
mac_list = sorted(output_list, key=lambda d: tuple(int(x) if x.isdigit() else x for x in d['port'].split("/")))
這會將數字字符串轉換為split()
生成的port
值的子字符串中的int
值,並將非數字字符串保持原樣,並且基於結果tuple
值的排序將為您提供所需的輸出。
似乎您正在尋找人類/自然排序。 這個答案很好地涵蓋了它。
使用該答案中的natural_keys
函數,然后可以將端口字符串直接傳遞給定義的函數,如下所示:
output_list.sort(key=lambda d: natural_keys(d["port"]))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.