V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  mike163  ›  全部回复第 2 页 / 共 2 页
回复总数  23
1  2  
2015-07-24 22:00:43 +08:00
回复了 gock 创建的主题 Python 求助,怎样把大量的 ip 地址转换为 ip 段
再来:
s='192.168.2.1|192.168.2.9|192.168.3.1'

s.split('|').map {|h| ts=h.scan(/[0-9]+\.[0-9]+\.[0-9]+\./)[0]; ts+'0-'+ts+'255' }.uniq.each {|x| p x}

结果:
"192.168.2.0-192.168.2.255"
"192.168.3.0-192.168.3.255"
2015-07-23 17:36:30 +08:00
回复了 gock 创建的主题 Python 求助,怎样把大量的 ip 地址转换为 ip 段
完整的代码:
def merge_ip_list(s)

ta = s.split('|')

nta = ta.map do |h|
na = (h.scan /[0-9]+\./)
na[0]+na[1]+na[2]+'0-'+ na[0]+na[1]+na[2]+'255'
end

nta.uniq!
nta.each {|x| p x}

end

-----------------
测试:
merge_ip_list('192.168.2.1|192.168.2.9|192.168.3.1')
"192.168.2.0-192.168.2.255"
"192.168.3.0-192.168.3.255"
2015-07-22 20:51:52 +08:00
回复了 gock 创建的主题 Python 求助,怎样把大量的 ip 地址转换为 ip 段
刚用ruby 写了一个 ta是一个ip addr list


def merge_ip_list(ta)
nta = ta.map do |h|
na = (h[:ip].scan /[0-9]+\./)
na[0]+na[1]+na[2]+'0/24'
end

nta.uniq!

ra=nta.map do |ip|
ind = ip.index('0/24')
s1 = ip[0..ind-1]
rep_times = ta.count{|h| h[:ip].index(s1)!=nil}
final = (rep_times>1) ? ip : (ta.find{|h| h[:ip].index(s1)!=nil})[:ip]
i_counter = (ta.select{|h| h[:ip].index(s1)!=nil}).inject(0) { |mem, var| mem + var[:counter] }
{:ip=>final,:counter=>i_counter}
end

return ra
end
1  2  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2252 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 14ms · UTC 08:54 · PVG 16:54 · LAX 01:54 · JFK 04:54
Developed with CodeLauncher
♥ Do have faith in what you're doing.