2016-10-28 9 views
1

IPアドレスのリストをパケットサイズで降順にソートする必要があります。これはリストの最後の番号です。たとえば、最初のリストでは、パケットサイズは34です。これを行う方法がわかりません。これは私がこれまで持っているものです。Python 3の最後の要素でリストをソート

ip_list = [['192.0.80.1','208.0.0.1',34], ['192.0.80.1','200.0.255.255',224], 
      ['192.24.8.1','108.0.8.8',304], ['192.0.25.1','228.0.38.1',128]] 

option = input("If you would like to sort the list enter -s, anything esle will quit") 

if option == "-s" : 

    ip_list.sort (reverse = True) 
    print (ip_list) 
else : 
    print ("Okay the list will not be sorted. Gooodbye!!") 

答えて

0
from operator import itemgetter 

ip_list = [['192.0.80.1','208.0.0.1', 34], ['192.0.80.1','200.0.255.255', 224], ['192.24.8.1', '108.0.8.8', 304], ['192.0.25.1', '228.0.38.1', 128]] 

ip_sorted = sorted(ip_list, key=itemgetter(2), reverse=True) 

print(ip_sorted) 

OUTPUT

[['192.24.8.1', '108.0.8.8', 304], ['192.0.80.1', '200.0.255.255', 224], ['192.0.25.1', '228.0.38.1', 128], ['192.0.80.1', '208.0.0.1', 34]] 
関連する問題