tallin.py
i have a list like this: [['10', '68', '12', '13'], ['10', '68', '14', '22'], ...] ... in that list, i want to insert .0 and .255 every time the third octet changes. in this example i would like to insert 10.68.12.0 before the first entry and 10.68.12.255 after the first entry... then similar to the next entry since its 3rd octet has gone from 12 to 14. any idea how? i can't make it work
Size 1.2 kB - File type text/python-sourceFile contents
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
"""
i have a list like this: [['10', '68', '12', '13'], ['10', '68', '14', '22'], ...]
in that list, i want to insert .0 and .255 every time the third octet changes.
in this example i would like to insert 10.68.12.0 before the first
entry and 10.68.12.255 after the first entry... then similar to the
next entry since its 3rd octet has gone from 12 to 14. any idea how?
i can't make it work
"""
Input = r"""
010.068.016.001
010.068.016.007
010.068.016.010
010.068.016.012
010.068.016.014
010.068.016.015
010.068.010.005
010.068.010.010
010.068.010.002
010.068.017.012
010.068.017.020
010.068.017.240
"""
List= [ map(int,ip.split('.')) for ip in Input.splitlines() if ip.strip() ]
OldKey=""
OldData=[]
NewList = []
for ip in List:
Key = str(ip[:3])
Data = ip[:3]
if Key == OldKey:
NewList.extend([ip])
else:
Front = ip[:3]
End = OldData
Front.append(0)
End.append(255)
if OldKey:
NewList.extend([End, Front, ip])
else:
NewList.extend([Front, ip])
OldKey = Key
OldData = Data
OldData.append(255)
NewList.extend([OldData])
# NewList.sort()
print "\n".join(map(str,NewList))
Click here to get the file