python对ip地址排序、对列表进行去重

一:使用python对ip地址排序所用代码
示例一:
import socket
iplist =['10.5.11.1','192.168.1.33','10.5.2.4','10.5.1.3','10.5.11.13','10.5.11.12','10.5.1.1','10.5.1.2','10.5.1.11','10.5.1.13']
print(sorted(iplist,key=socket.inet_aton))
for i in sorted(iplist,key=socket.inet_aton):
    print(i)
示例二:
iplist = ['192.168.1.33','10.5.2.4','10.5.1.3','202.98.96.68','133.120.1.1','192.168.1.22']
print(sorted(iplist,key = lambda x: ( int(x.split('.')[0]), int(x.split('.')[1]), int(x.split('.')[2]) )))

ip_l = sorted(iplist,key = lambda x: ( int(x.split('.')[0]), int(x.split('.')[1]), int(x.split('.')[2]) ))
for i in ip_l:
    print(i)

二:使用字典函数对列表进行去重

 示例:

a = [1,2,3,2,2,4,5,2,34,4,562,23,4,4,2,24,2,4,2,24,2,]

b = {}
b = b.fromkeys(a)

print(list(b.keys()))

 

 

结果:

    [1, 2, 3, 4, 5, 34, 562, 23, 24]
posted @ 2019-03-19 17:18  xihuineng  阅读(2167)  评论(1编辑  收藏  举报