我有一个命令列表要发送到一个Juniper路由器。如何根据命令末尾的ip地址对列表进行排序?
由此,使用set_fact和with_items生成
"command_list": [
"show bgp neighbor 1.1.1.1",
"show bgp neighbor 2.2.2.2",
"show bgp neighbor 3.3.3.3",
"show route receive-protocol bgp 1.1.1.1",
"show route receive-protocol bgp 2.2.2.2",
"show route receive-protocol bgp 3.3.3.3",
"show route advertising-protocol bgp 1.1.1.1",
"show route advertising-protocol bgp 2.2.2.2"
"show route advertising-protocol bgp 3.3.3.3"
]对此,由目标IP命令。
"command_list": [
"show bgp neighbor 1.1.1.1",
"show route receive-protocol bgp 1.1.1.1",
"show route advertising-protocol bgp 1.1.1.1",
"show bgp neighbor 2.2.2.2",
"show route receive-protocol bgp 2.2.2.2",
"show route advertising-protocol bgp 2.2.2.2"
"show bgp neighbor 3.3.3.3",
"show route receive-protocol bgp 3.3.3.3",
"show route advertising-protocol bgp 3.3.3.3"
]发布于 2018-02-21 16:54:54
在对sorted进行比较之前,在list上使用list操作,并使用其key参数指定要在每个list元素上调用的函数。
command_list = [
"show bgp neighbor 1.1.1.1",
"show bgp neighbor 2.2.2.2",
"show bgp neighbor 3.3.3.3",
"show route receive-protocol bgp 1.1.1.1",
"show route receive-protocol bgp 2.2.2.2",
"show route receive-protocol bgp 3.3.3.3",
"show route advertising-protocol bgp 1.1.1.1",
"show route advertising-protocol bgp 2.2.2.2",
"show route advertising-protocol bgp 3.3.3.3"
]
def last(a):
for i in reversed(a.split()):
return i
print(sorted(command_list, key=last))输出
['show bgp neighbor 1.1.1.1',
'show route receive-protocol bgp 1.1.1.1',
'show route advertising-protocol bgp 1.1.1.1',
'show bgp neighbor 2.2.2.2',
'show route receive-protocol bgp 2.2.2.2',
'show route advertising-protocol bgp 2.2.2.2',
'show bgp neighbor 3.3.3.3',
'show route receive-protocol bgp 3.3.3.3',
'show route advertising-protocol bgp 3.3.3.3'] https://stackoverflow.com/questions/48911108
复制相似问题