Skip to content

Instantly share code, notes, and snippets.

@TkTech
Created April 10, 2012 07:48
Show Gist options
  • Save TkTech/2349131 to your computer and use it in GitHub Desktop.
Save TkTech/2349131 to your computer and use it in GitHub Desktop.
> % python silly_ip.py
There are 1 items in the 74 bucket.
(74, 125, 232, 201) 1
(74, 125, 232, 211) 1
(74, 125, 232, 206) 1
There are 1 items in the 79 bucket.
(79, 115, 74, 141) 1
#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
dummy_data = [
'74.125.232.201',
'74.125.232.206',
'74.125.232.211',
'79.115.74.141'
]
def main(argv):
root = {}
for raw_ip in dummy_data:
ip = [int(i) for i in raw_ip.split('.')]
tmp = root.setdefault(ip[0], {})
tmp = tmp.setdefault(ip[1], {})
tmp = tmp.setdefault(ip[2], {})
tmp[ip[3]] = tmp.get(ip[3], 0) + 1
# Found how many there are on each level.
for ip1, v1 in root.items():
print('There are %d items in the %d bucket.' % (len(v1), ip1))
for ip2, v2 in v1.items():
for ip3, v3 in v2.items():
for ip4, count in v3.items():
print (ip1, ip2, ip3, ip4), count
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment