简体   繁体   中英

Sorting MAC address in python

I have a list of dict of mac addresses,I want to sort them, I have tried the following code

a = a=[{"from":"00:25:B5:11:C1:00"},{"from":"00:25:B5:00:0A:08"},{"from":"00:25:B5:00:0B:08"},{"from":"00:25:B5:99:00:00"},{"from":"00:25:B5:99:00:00"},{"from":"00:25:B5:12:34:00"}]

b = sorted(a, key=lambda x: [int(y) for y in x['from'].split(':')])
print(b)

but it gives the following error

invalid literal for int() with base10:B5

Not sure how to proceed. Any help is appreciated

Thank You

you can try this

a = a=[{"from":"00:25:B5:11:C1:00"},{"from":"00:25:B5:00:0A:08"},{"from":"00:25:B5:00:0B:08"},{"from":"00:25:B5:99:00:00"},{"from":"00:25:B5:99:00:00"},{"from":"00:25:B5:12:34:00"}]

b = sorted(a, key=lambda x: [y for y in x['from'].split(':')])
print(b)

you will get result like this

[{'from': '00:25:B5:00:0A:08'}, {'from': '00:25:B5:00:0B:08'}, {'from': '00:25:B5:11:C1:00'}, {'from': '00:25:B5:12:34:00'}, {'from': '00:25:B5:99:00:00'}, {'from': '00:25:B5:99:00:00'}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM