Python: Write two lists into two column text file

后端 未结 6 727
我寻月下人不归
我寻月下人不归 2020-12-13 07:23

Say I have two lists: a=[1,2,3] b=[4,5,6] I want to write them into a text file such that I obtain a two column text file:



        
6条回答
  •  暖寄归人
    2020-12-13 07:54

    You can write two lists into a text file that contains two columns.

    a=[1,2,3]
    b=[4,5,6] 
    c = [a, b] 
    with open("list1.txt", "w") as file:
        for x in zip(*c):
            file.write("{0}\t{1}\n".format(*x))
    

    Output in the text file:

    1   4
    2   5
    3   6
    

提交回复
热议问题