在python中使用zip函数

问题描述 投票:0回答:1

我将保存在文件中的值保存为:

[0.        0.5       1.        1.5       2.        2.5000002 .......] [1.00000000e+00 5.32289624e-01 3.31494868e-01 2.17292413e-01 1.46866933e-01 1.01670586e-01......]

但我希望保存的值为:

0.0    1.00000000e+00 

0.5    5.32289624e-01

1.0    3.31494868e-01

1.5    2.17292413e-01

2.0    1.46866933e-01

2.5    1.01670586e-01

因此它很容易在图中绘制。

这是我的python代码的一部分:

time = hb_ac.solution['time']
results = hb_ac.solution['results']
tau = hb_ac.solution['tau']
fit=hb_ac.solution['fit']
estimate = hb_ac.solution['estimate']
with open('CaOw2.4dat', 'w') as out:
    out.write("{time} {results}".format(time=time,results=results))

有关如何使用zip函数的任何建议,以获得所需格式的输出。

python zip
1个回答
0
投票

这是你想要的?

time = range(10)
results = range(10,20)

with open('CaOw2.4dat', 'w') as out:
    for t, result in zip(time, results):
        out.write("{time} {results}\n".format(time=t,results=result))

文件内容:

0 10
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
© www.soinside.com 2019 - 2024. All rights reserved.