69077


xy対応したtxt書き込みを行いたい。  
以下のコードだと、`[x=1,2,3~][y=10,20,30~]`になって見づらい。

[code]

    from scipy.special import kv
    import matplotlib.pyplot as plt
    from scipy.integrate import quad
    import numpy as np
    
    xs = np.arange(0.0,10,0.1)
    f = lambda z: kv(5/3,z)
    F = [quad(f,x,np.inf)[0]*x for x in xs]
    
    with open("test4.txt","w") as f:
            print(xs,F,file=f)
    
[/code]


CC BY-SA 4.0
2020-07-28T20:50:47.897
69075
2020-07-29T10:28:52.883
2020-07-29T01:57:04.147
3060
41194
question
python jupyter-notebook
txt出力を [x=1,ｙ＝10][x=2,y=20] みたいにきれいにしたい


[zip](https://docs.python.org/ja/3/library/functions.html#zip)関数でまとめるのがオーソドックスな方法だと思いますがいかがでしょうか。

サンプルコード:

[code]

    from scipy.special import kv
    import matplotlib.pyplot as plt
    from scipy.integrate import quad
    import numpy as np
    
    xs = np.arange(0.0,10,0.1)
    f = lambda z: kv(5/3,z)
    F = [quad(f,x,np.inf)[0]*x for x in xs]
    
    with open("test4.txt","w") as f:
        for x, y in zip(xs, F):
            print("[x=%.1f, y=%f]" % (x, y), file=f)
    
[/code]

出力結果:

[code]

    [x=0.0, y=-0.000000]
    [x=0.1, y=0.818186]
    [x=0.2, y=0.903386]
    [x=0.3, y=0.917705]
    [x=0.4, y=0.901937]
    [x=0.5, y=0.870819]
    (以下略)
    
[/code]


CC BY-SA 4.0
2020-07-29T01:54:20.357
69077
2020-07-29T01:54:20.357
9820
69075
answer


以下は [f-string(PEP 498: Literal String
Interpolation)](https://www.python.org/dev/peps/pep-0498/) を使う場合です。

[code]

    with open('test4.txt', 'w') as f:
      f.write(''.join([f'[x={xs[i]:.1f}, y={F[i]:.6f}]\n' for i in range(len(xs))]))
    
[/code]


CC BY-SA 4.0
2020-07-29T10:28:52.883
69096
2020-07-29T10:28:52.883
69075
answer