python - simpler implementation of this string splitting loop -
i have function receives string dot delimited. want loop through value building , running code each level. here implementation:
def example(name): module = [] in name.split('.'): module.append(i) print '.'.join(module) #do stuff here
output
>>> example('a.b.c.d') a.b a.b.c a.b.c.d
but feels long winded. i'm looking simpler, cleaner or shorter implementation.
split once, slice it:
s = 'a.b.c.d' items = s.split('.') print [items[:i] in xrange(1, len(items) + 1)] # [['a'], ['a', 'b'], ['a', 'b', 'c'], ['a', 'b', 'c', 'd']]
Comments
Post a Comment