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:
sortedlistprint(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]

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。