簡體   English   中英

Python - 嵌套分隔文件的嵌套列表?

[英]Python - Nested List to Tab Delimited File?

我有一個嵌套列表,包含~30,000個子列表,每個子列表有三個條目,例如,

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

我希望創建一個函數,以便將此數據結構輸出為制表符分隔格式,例如,

x    y    z
a    b    c

任何幫助非常感謝!

謝謝,Seafoid。

>>> nested_list = [['x', 'y', 'z'], ['a', 'b', 'c']]
>>> for line in nested_list:
...   print '\t'.join(line)
... 
x   y   z
a   b   c
>>> 
with open('fname', 'w') as file:
    file.writelines('\t'.join(i) + '\n' for i in nested_list)

在我看來,這是一個簡單的單行:

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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM