3 ms·
My first suggestion would be to add a line break after format( , remove the [ ] and rename the loop variable to a single letter. output.write("\t\t{0}\n".f
by chronial 9y ago
My first suggestion would be to add a line break after format( , remove the [ ] and rename the loop variable to a single letter.
output.write("\t\t{0}\n".format(
" ".join(str(x) for x in flatten(vertex))))
But it looks like your core problem is that try you force another language's idioms onto python.
Your flatten function could be a lot simpler (and without quadratic runtime):
import itertools
def flat(l):
return itertools.chain.from_iterable(
flat(e) if isinstance(e, (list, tuple)) else [e]
for e in l
)
But that is still not what you actually want – you only wrote this function because you have it in ruby. What you actually want is:
def format_vertex(lst):
return " ".join(
format_vertex(e) if isinstance(e, (list, tuple)) else str(e)
for e in lst
)
output.write("\t\t{}\n".format(format_vertex(vertex))