Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sorted()のkeyに指定すると、ネットワーク機器のインターフェイス名 + インターフェイス番号を数値としてソートできる(はず)
import re
def sort_by_interface_and_number(interface_name):
if re.search(r'[0-9]', interface_name):
interface_num_start = re.search(r'[0-9]', interface_name).start()
interface_type = interface_name[:interface_num_start]
interface_raw_number = interface_name[interface_num_start:]
interface_numbers = []
for i in range(len(interface_raw_number)):
s = interface_raw_number[i]
if s.isdecimal():
if i == 0 or not interface_raw_number[i-1].isdecimal():
interface_numbers.append(s)
else:
interface_numbers[-1] = interface_numbers[-1] + s
return (interface_type, [int(s) for s in interface_numbers])
else:
return (interface_name, None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.