Mine værktøjer
Du er her: Forside diverse filer til download mm. tallin.py
Handlinger tilknyttet webside

tallin.py

af Lars Bauer JørgensenSidst opdateret 25/06 2008 11:21

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

Klik her for at hente fil

Størrelse: 1.2 kB - Filtype text/python-source

Indhold af fil-indholdsobjekt

#!/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))
« august 2018 »
søn man tir ons tor fre lør
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Chat Rooms
Dyrk motion

Trim ballerup

 

Powered by Plone, the Open Source Content Management System