loop over list and process into groups

lbolla lbolla at gmail.com
Thu Mar 4 12:59:21 EST 2010


On Mar 4, 3:57 pm, Sneaky Wombat <joe.hr... at gmail.com> wrote:
> [ {'vlan_or_intf': 'VLAN2021'},
>  {'vlan_or_intf': 'Interface'},
>  {'vlan_or_intf': 'Po1'},
>  {'vlan_or_intf': 'Po306'},
>  {'vlan_or_intf': 'VLAN2022'},
>  {'vlan_or_intf': 'Interface'},
>  {'vlan_or_intf': 'Gi7/33'},
>  {'vlan_or_intf': 'Po1'},
>  {'vlan_or_intf': 'Po306'},
>  {'vlan_or_intf': 'VLAN2051'},
>  {'vlan_or_intf': 'Interface'},
>  {'vlan_or_intf': 'Gi9/6'},
>  {'vlan_or_intf': 'VLAN2052'},
>  {'vlan_or_intf': 'Interface'},
>  {'vlan_or_intf': 'Gi9/6'},]
>
> I want it to be converted to:
>
> [{'2021':['Po1','Po306']},{'2022':['Gi7/33','Po1','Po306']},etc etc]
>
> I was going to write a def to loop through and look for certain pre-
> compiled regexs, and then put them in a new dictionary and append to a
> list, but I'm having trouble thinking of a good way to capture each
> dictionary.  Each dictionary will have a key that is the vlan and the
> value will be a list of interfaces that participate in that vlan.
> Each list will be variable, many containing only one interface and
> some containing many interfaces.
>
> I thought about using itertools, but i only use that for fixed data.
> I don't know of a good way to loop over variably sized data.  I was
> wondering if anyone had any ideas about a good way to convert this
> list or dictionary into the right format that I need.  The solution I
> come up with will most likely be ugly and error prone, so I thought
> i'd ask this python list while I work.  Hopefully I learn a better way
> to solve this problem.
>
> Thanks!
>
> I also have the data in a list,
>
> [ 'VLAN4065',
>  'Interface',
>  'Gi9/6',
>  'Po2',
>  'Po3',
>  'Po306',
>  'VLAN4068',
>  'Interface',
>  'Gi9/6',
>  'VLAN4069',
>  'Interface',
>  'Gi9/6',]



===================================

from itertools import groupby

data = \
[ {'vlan_or_intf': 'VLAN2021'},
 {'vlan_or_intf': 'Interface'},
 {'vlan_or_intf': 'Po1'},
 {'vlan_or_intf': 'Po306'},
 {'vlan_or_intf': 'VLAN2022'},
 {'vlan_or_intf': 'Interface'},
 {'vlan_or_intf': 'Gi7/33'},
 {'vlan_or_intf': 'Po1'},
 {'vlan_or_intf': 'Po306'},
 {'vlan_or_intf': 'VLAN2051'},
 {'vlan_or_intf': 'Interface'},
 {'vlan_or_intf': 'Gi9/6'},
 {'vlan_or_intf': 'VLAN2052'},
 {'vlan_or_intf': 'Interface'},
 {'vlan_or_intf': 'Gi9/6'},]

def clean_up(lst):
	return [d.values()[0] for d in data if d.values()[0] != 'Interface']

out = {}
for k, g in groupby(clean_up(data) , key=lambda s:
s.startswith('VLAN')):
	if k:
		key = list(g)[0].replace('VLAN','')
	else:
		out[key] = list(g)

print out
===================================

hth,
L.



More information about the Python-list mailing list