gpt4 book ai didi

python - 将列表写入列

转载 作者:太空宇宙 更新时间:2023-11-03 12:19:17 27 4
gpt4 key购买 nike

我有一个 Python 数据列表:

[1,2,3,4,5]

我想通过以下方式将此数据作为列读入文件:

1
2
3
4
5

然后我想将我的下一个列表 ([6,7,8,9,10]) 添加到其中(使用制表符):

1 6  
2 7
3 8
4 9
5 10

等等。

谁能帮我解决这个问题?

最佳答案

使用内置函数zip() :

with open('data.txt', 'w') as f:
lst = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
for x in zip(*lst):
f.write("{0}\t{1}\t{2}\n".format(*x))

输出:

1   6   11

2 7 12

3 8 13

4 9 14

5 10 15

关于python - 将列表写入列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12292970/

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