パイソン - ネストされたリストタブ区切りファイルに?
-
22-09-2019 - |
質問
私は、ネストされたリストからなる〜3つのエントリ、例えば30,000サブリスト、それぞれを持っている。
nested_list = [['x', 'y', 'z'], ['a', 'b', 'c']].
私は、タブ区切り形式に出力するために、このデータ構造体を関数を作成したい例えば。、
x y z
a b c
すべてのヘルプは大歓迎!
事前におかげで、 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
>>>
私の見解では、それは簡単なワンライナーです:
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)
所属していません StackOverflow