2个类似的方法
merge_ip_list 是 @
leavic 的方法
########################################
#-*- coding:utf-8 -*-
from sets import Set
def merge_ip_list(ip_array):
ip_list = ip_array.split('|')
full_list = Set([])
for ip in ip_list:
ip_L = ip.split('.')
ip_L[3] = '0'
ip_R = ip.split('.')
ip_R[3] = '255'
full_list.add('%s - %s' % ('.'.join(ip_L), '.'.join(ip_R)))
return full_list
def merge_ip_list_2(ip_array):
full_list = Set([])
ip_list_splited = [x.split('.') for x in ip_array.split('|')]
for ip in ip_list_splited:
ip_head = '%s.%s.%s' % (ip[0], ip[1], ip[2])
full_list.add('%s.0 - %s.255' % (ip_head, ip_head))
return full_list
#########################################
ip_array = '192.17.23.3|192.16.80.209|192.16.80.200'
print merge_ip_list(ip_array)
print merge_ip_list_2(ip_array)
2个方法皆返回:
Set(['192.16.80.0 - 192.16.80.255', '192.17.23.0 - 192.17.23.255'])