gpt4 book ai didi

python - 使用 numpy 连接两列

转载 作者:太空宇宙 更新时间:2023-11-04 01:20:01 24 4
gpt4 key购买 nike

我有两个文本文件 ra.txtdec.txt,每个文件包含一列。我想将这两列加入到具有一列的第三个文件中,如下所示:

ra.txt

115.07433  
114.75551
114.85937
111.18574
192.78617
189.71056
0.47592
2.22369
2.97497
5.44140
0.29367
308.83178

dec.txt

-88.84627
-88.68575
-88.68499
-88.63843
-88.70691
-88.69978
-87.12709
-87.12417
-87.11521
-86.95877
-86.94902
-87.04636

我希望输出文件是:

115.07433-88.84627
114.75551-88.68575
114.85937-88.68499
111.18574-88.63843
192.78617-88.70691
189.71056-88.69978
0.47592-87.12709
2.22369-87.12417
2.97497-87.11521
5.44140-86.95877
0.29367-86.94902
308.83178-87.04636

你能帮我做一下吗?

最佳答案

您可以使用 numpy.loadtxt , numpy.column_stacknumpy.savetxt为此:

>>> import numpy as np
>>> a1 = np.loadtxt('ra.txt')
>>> a2 = np.loadtxt('dec.txt')
>>> np.savetxt('out.txt', np.column_stack((a1, a2)), delimiter=' ', fmt='%.5f')
>>> !cat out.txt
115.07433 -88.84627
114.75551 -88.68575
114.85937 -88.68499
111.18574 -88.63843
192.78617 -88.70691
189.71056 -88.69978
0.47592 -87.12709
2.22369 -87.12417
2.97497 -87.11521
5.44140 -86.95877
0.29367 -86.94902
308.83178 -87.04636

关于python - 使用 numpy 连接两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21852892/

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