gpt4 book ai didi

list - python : Write two lists into two column text file

转载 作者:行者123 更新时间:2023-12-02 23:00:29 32 4
gpt4 key购买 nike

假设我有两个列表:a=[1,2,3]b=[4,5,6]我想将它们写入一个文本文件,以便获得一个两列文本文件:

1 4
2 5
3 6

最佳答案

只需 zip 列表,并将它们写入 csv 文件,并以制表符作为分隔符:

>>> a=[1,2,3]
>>> b=[4,5,6]
>>> zip(a,b)
[(1, 4), (2, 5), (3, 6)]
>>> import csv
>>> with open('text.csv', 'w') as f:
... writer = csv.writer(f, delimiter='\t')
... writer.writerows(zip(a,b))
...
>>> quit()
$ cat text.csv
1 4
2 5
3 6

关于list - python : Write two lists into two column text file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26082718/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com