【问题标题】:Use zip function in python在 python 中使用 zip 函数
【发布时间】:2018-06-12 11:27:05
【问题描述】:

我在文件中保存的值是:

[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】:

    这是你要找的吗?

    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
    

    【讨论】:

    • 如果你想在每个点之间多加一条线,只需 out.write("{time} {results}\n\n".format(time=t,results=result))
    • 没有。实际上,我不能修改代码的任何部分(即时间=范围,因为它是一个模块),我只想以所需的格式编写 vales。如果我提供 zip 函数,它会写入值存在的次数。但我只想要一次。
    • 好的,你可以试试这个:stackoverflow.com/questions/26082718/…
    猜你喜欢
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 2017-07-06
    • 1970-01-01
    • 2011-01-26
    • 2011-06-18
    相关资源
    最近更新 更多