stv wrote:
> I considered several brute-force solutions, but I persevered and came
> to, what I think, is a more Pythonic solution. What do you think?

Looks pretty sweet to me :-)
> 
> import string
> 
> def expand_tree(filetree):
>   indent = '\t'
>   stack = []
>   for f in filetree:
>     indents = f.count(indent)
>     while len(stack) > indents: stack.pop()
>     stack.append(f.strip())
>     yield string.join(stack,'')
>     if not f.endswith('/'): stack.pop()
> 
> lines = [line.rstrip() for line in file('test.txt')]  
> for i in expand_tree(lines): print i
> 
> Questions:
> 
> Is the list comprehension the right idiom for getting a file into a
> list, without the EOL chars? I'm hard-pressed to see how it could be
> more concise, but this is Python :) and the rtrim() feels a little
> off.

The list comp is fine but I don't think you need it at all, since you 
strip() the string before you add it to stack.
> 
> Is string.join() preferred to ''.join? Does importing all of string
> outweigh the readability of string.join()?

string.join() is deprecated, ''.join() is preferred.

Kent

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to