我有一個要排序的字典串列。這是我到目前為止所擁有的:
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'}]
uj5u.com熱心網友回復:
嘗試:
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()函式將根據這些元組進行排序。
uj5u.com熱心網友回復:
似乎您正在尋找人類/自然排序。這個答案很好地涵蓋了它。
使用該natural_keys答案中的函式,然后可以將埠字串直接傳遞給定義的函式,如下所示:
output_list.sort(key=lambda d: natural_keys(d["port"]))
uj5u.com熱心網友回復:
嘗試這個:
mac_list = sorted(output_list, key=lambda d: tuple(int(x) if x.isdigit() else x for x in d['port'].split("/")))
這會將數字字串轉換為產生int的值的子字串中的port值,split()并保持非數字字串不變,并且基于結果tuple值的排序將為您提供所需的輸出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/495559.html
上一篇:如何從txt檔案中讀取字典串列?
下一篇:使用遞回從嵌套字典中獲取值
