From: Paul Rubin on 5 Mar 2010 04:45 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 5 Mar 2010 08:26 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 5 Mar 2010 11:41 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 5 Mar 2010 12:09 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 5 Mar 2010 12:10 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
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: 15541 - Best, Cheapest Web-Hosting, Domain at $1.99! Next: ANNOUNCE: Exscript 2.0 |