Man, deja-vu, I could have sworn I read this thread months ago...<br><br><div class="gmail_quote">On Thu, Mar 4, 2010 at 2:18 PM, nn <span dir="ltr"><<a href="mailto:pruebauno@latinmail.com">pruebauno@latinmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div></div><div class="h5"><br>
<br>
lbolla wrote:<br>
> On Mar 4, 3:57 pm, Sneaky Wombat <<a href="mailto:joe.hr...@gmail.com">joe.hr...@gmail.com</a>> wrote:<br>
> > [ {'vlan_or_intf': 'VLAN2021'},<br>
> >  {'vlan_or_intf': 'Interface'},<br>
> >  {'vlan_or_intf': 'Po1'},<br>
> >  {'vlan_or_intf': 'Po306'},<br>
> >  {'vlan_or_intf': 'VLAN2022'},<br>
> >  {'vlan_or_intf': 'Interface'},<br>
> >  {'vlan_or_intf': 'Gi7/33'},<br>
> >  {'vlan_or_intf': 'Po1'},<br>
> >  {'vlan_or_intf': 'Po306'},<br>
> >  {'vlan_or_intf': 'VLAN2051'},<br>
> >  {'vlan_or_intf': 'Interface'},<br>
> >  {'vlan_or_intf': 'Gi9/6'},<br>
> >  {'vlan_or_intf': 'VLAN2052'},<br>
> >  {'vlan_or_intf': 'Interface'},<br>
> >  {'vlan_or_intf': 'Gi9/6'},]<br>
> ><br>
> > I want it to be converted to:<br>
> ><br>
> > [{'2021':['Po1','Po306']},{'2022':['Gi7/33','Po1','Po306']},etc etc]<br>
> ><br>
> > I was going to write a def to loop through and look for certain pre-<br>
> > compiled regexs, and then put them in a new dictionary and append to a<br>
> > list, but I'm having trouble thinking of a good way to capture each<br>
> > dictionary.  Each dictionary will have a key that is the vlan and the<br>
> > value will be a list of interfaces that participate in that vlan.<br>
> > Each list will be variable, many containing only one interface and<br>
> > some containing many interfaces.<br>
> ><br>
> > I thought about using itertools, but i only use that for fixed data.<br>
> > I don't know of a good way to loop over variably sized data.  I was<br>
> > wondering if anyone had any ideas about a good way to convert this<br>
> > list or dictionary into the right format that I need.  The solution I<br>
> > come up with will most likely be ugly and error prone, so I thought<br>
> > i'd ask this python list while I work.  Hopefully I learn a better way<br>
> > to solve this problem.<br>
> ><br>
> > Thanks!<br>
> ><br>
> > I also have the data in a list,<br>
> ><br>
> > [ 'VLAN4065',<br>
> >  'Interface',<br>
> >  'Gi9/6',<br>
> >  'Po2',<br>
> >  'Po3',<br>
> >  'Po306',<br>
> >  'VLAN4068',<br>
> >  'Interface',<br>
> >  'Gi9/6',<br>
> >  'VLAN4069',<br>
> >  'Interface',<br>
> >  'Gi9/6',]<br>
><br>
><br>
><br>
> ===================================<br>
><br>
> from itertools import groupby<br>
><br>
> data = \<br>
> [ {'vlan_or_intf': 'VLAN2021'},<br>
>  {'vlan_or_intf': 'Interface'},<br>
>  {'vlan_or_intf': 'Po1'},<br>
>  {'vlan_or_intf': 'Po306'},<br>
>  {'vlan_or_intf': 'VLAN2022'},<br>
>  {'vlan_or_intf': 'Interface'},<br>
>  {'vlan_or_intf': 'Gi7/33'},<br>
>  {'vlan_or_intf': 'Po1'},<br>
>  {'vlan_or_intf': 'Po306'},<br>
>  {'vlan_or_intf': 'VLAN2051'},<br>
>  {'vlan_or_intf': 'Interface'},<br>
>  {'vlan_or_intf': 'Gi9/6'},<br>
>  {'vlan_or_intf': 'VLAN2052'},<br>
>  {'vlan_or_intf': 'Interface'},<br>
>  {'vlan_or_intf': 'Gi9/6'},]<br>
><br>
> def clean_up(lst):<br>
>       return [d.values()[0] for d in data if d.values()[0] != 'Interface']<br>
><br>
> out = {}<br>
> for k, g in groupby(clean_up(data) , key=lambda s:<br>
> s.startswith('VLAN')):<br>
>       if k:<br>
>               key = list(g)[0].replace('VLAN','')<br>
>       else:<br>
>               out[key] = list(g)<br>
><br>
> print out<br>
> ===================================<br>
><br>
> hth,<br>
> L.<br>
<br>
</div></div>Good use of groupby. Here is what I ended up coming up:<br>
<br>
from itertools import groupby<br>
laninfo=[ 'VLAN4065',<br>
<div class="im"> 'Interface',<br>
 'Gi9/6',<br>
 'Po2',<br>
 'Po3',<br>
 'Po306',<br>
 'VLAN4068',<br>
 'Interface',<br>
 'Gi9/6',<br>
 'VLAN4069',<br>
 'Interface',<br>
 'Gi9/6',]<br>
<br>
</div>def splitgrp(s, f=[False]):<br>
   f[0]^=s.startswith('VLAN')<br>
   return f[0]<br>
lanlst=(list(g) for k,g in groupby(laninfo,key=splitgrp))<br>
out={item[0][4:]:item[2:] for item in lanlst}<br>
print(out)<br>
<div><div></div><div class="h5">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>