2017-08-11 59 views
0

我在Python 2.7中有一個查詢。所有作品都沒有問題;不過,我需要根據網絡CIDR對結果進行排序。下面是代碼和結果副本,我正在得到我一直試圖得到的結果。嘗試所有類型的排序,似乎無法得到任何排序實際工作。如何對包含Python 2.7測試的結果的CIDR部分進行排序?

query = """ 
    SELECT Address, 
     CIDR, FriendlyName 
    FROM IPAM.GroupNode 
    """ 
    results = swis.query(query) 
    for row in results['results']: 
     print("{FriendlyName} is using CIDR - Address}/{CIDR}".format(**row)) 

返回結果如下圖:

Some Customer is using CIDR - 10.200.104.0/22 
Some Others is using CIDR - 10.200.86.0/22 
Customer1 in CO is using CIDR - 10.200.0.0/20 
Customer2 is using CIDR - 10.200.64.0/20 
Another Cust is using CIDR - 10.200.32.0/22 
Another Cust2 is using CIDR - 10.200.128.0/20 
Another Cust3 is using CIDR - 10.200.112.0/22 

,我需要做的是擁有它,而不是返回類似:

Customer1 in CO is using CIDR - 10.200.0.0/20 
Another Cust is using CIDR - 10.200.32.0/22 
Customer2 is using CIDR - 10.200.64.0/20 
Some Others is using CIDR - 10.200.86.0/22 
Some Customer is using CIDR - 10.200.104.0/22 
Another Cust3 is using CIDR - 10.200.112.0/22 
Another Cust2 is using CIDR - 10.200.128.0/20 
+0

我想你很可能把所有的這些在一個列表,排序列表使用'key = lambda'參數 – Mangohero1

回答

0

for循環之前做一個列表:

cidrs = [] 

.append()更換print

cidrs.append("{FriendlyName} is using CIDR - Address}/{CIDR}".format(**row)) 

排序列表由CDIR塊(又名,最後兩位數字):

sorted(cidrs, key=lambda x: x[-2:]) 
相關問題