문제

I have a nested list comprising ~30,000 sub-lists, each with three entries, e.g.,

nested_list = [['x', 'y', 'z'], ['a', 'b', 'c']].

I wish to create a function in order to output this data construct into a tab delimited format, e.g.,

x    y    z
a    b    c

Any help greatly appreciated!

Thanks in advance, Seafoid.

도움이 되었습니까?

해결책

with open('fname', 'w') as file:
    file.writelines('\t'.join(i) + '\n' for i in nested_list)

다른 팁

>>> nested_list = [['x', 'y', 'z'], ['a', 'b', 'c']]
>>> for line in nested_list:
...   print '\t'.join(line)
... 
x   y   z
a   b   c
>>> 

In my view, it's a simple one-liner:

print '\n'.join(['\t'.join(l) for l in nested_list])
>>> print '\n'.join(map('\t'.join,nested_list))
x       y       z
a       b       c
>>>
out = file("yourfile", "w")
for line in nested_list:
    print >> out, "\t".join(line)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top