From: Paul Rubin on
lbolla <lbolla(a)gmail.com> writes:
> for k, g in groupby(clean_up(data) , key=lambda s: s.startswith('VLAN')):
> if k:
> key = list(g)[0].replace('VLAN','')

This is the nicest solution, I think. Mine was more cumbersome.
From: mk on
Sneaky Wombat wrote:
> [ 'VLAN4065',
> 'Interface',
> 'Gi9/6',
> 'Po2',
> 'Po3',
> 'Po306',
> 'VLAN4068',
> 'Interface',
> 'Gi9/6',
> 'VLAN4069',
> 'Interface',
> 'Gi9/6',]

Hey, I just invented a cute ;-) two-liner using list comprehensions:

# alist = list above

tmp, dk = [], {}
[(x.startswith('VLAN') and (dk.setdefault(x,[]) or tmp.append(x))) or
(not x.startswith('VLAN') and dk[tmp[-1]].append(x)) for x in alist
if x != 'Interface']

No need to use a nuke like itertools to kill a fly. ;-)

Regards,
mk

From: nn on


mk wrote:
> Sneaky Wombat wrote:
> > [ 'VLAN4065',
> > 'Interface',
> > 'Gi9/6',
> > 'Po2',
> > 'Po3',
> > 'Po306',
> > 'VLAN4068',
> > 'Interface',
> > 'Gi9/6',
> > 'VLAN4069',
> > 'Interface',
> > 'Gi9/6',]
>
> Hey, I just invented a cute ;-) two-liner using list comprehensions:
>
> # alist = list above
>
> tmp, dk = [], {}
> [(x.startswith('VLAN') and (dk.setdefault(x,[]) or tmp.append(x))) or
> (not x.startswith('VLAN') and dk[tmp[-1]].append(x)) for x in alist
> if x != 'Interface']
>
> No need to use a nuke like itertools to kill a fly. ;-)
>
> Regards,
> mk

Oh my! You could have at least used some "if else" to make it a little
bit easier on the eyes :-)

[(dk.setdefault(x,[]) or tmp.append(x))
if x.startswith('VLAN')
else dk[tmp[-1]].append(x)
for x in alist
if x != 'Interface']
From: lbolla on
On Mar 5, 1:26 pm, mk <mrk...(a)gmail.com> wrote:
> Sneaky Wombat wrote:
> > [ 'VLAN4065',
> >  'Interface',
> >  'Gi9/6',
> >  'Po2',
> >  'Po3',
> >  'Po306',
> >  'VLAN4068',
> >  'Interface',
> >  'Gi9/6',
> >  'VLAN4069',
> >  'Interface',
> >  'Gi9/6',]
>
> Hey, I just invented a cute ;-) two-liner using list comprehensions:
>
> # alist = list above
>
> tmp, dk = [], {}
> [(x.startswith('VLAN') and (dk.setdefault(x,[]) or tmp.append(x))) or
> (not x.startswith('VLAN') and dk[tmp[-1]].append(x))    for x in alist
> if x != 'Interface']
>
> No need to use a nuke like itertools to kill a fly. ;-)
>
> Regards,
> mk

It looks like Perl ;-)
From: mk on
nn wrote:

> Oh my! You could have at least used some "if else" to make it a little
> bit easier on the eyes :-)

That's my entry into """'Obfuscated' "Python" '"''code''"'
'"contest"'""" and I'm proud of it. ;-)

Regards,
mk