Saving arrays as columns with np.savetxt

后端 未结 4 1814
Happy的楠姐
Happy的楠姐 2021-01-29 22:16

I am trying to do something that is probable very simple. I would like to save three arrays to a file as columns using \'np.savetxt\' When I try this

x = [1,2,3         


        
相关标签:
4条回答
  • 2021-01-29 22:30

    Use numpy.c_[]:

    np.savetxt('myfile.txt', np.c_[x,y,z])
    
    0 讨论(0)
  • 2021-01-29 22:30

    Use zip:

    np.savetxt('myfile2.txt', zip(x,y,z), fmt='%.18g')

    For python3 list+zip:

    np.savetxt('myfile.txt', list(zip(x,y,z)), fmt='%.18g')

    To understand the workaround see here: How can I get the "old" zip() in Python3? and https://github.com/numpy/numpy/issues/5951.

    0 讨论(0)
  • 2021-01-29 22:37

    Use numpy.transpose():

    np.savetxt('myfile.txt', np.transpose([x,y,z]))
    

    I find this more intuitive than using np.c_[].

    0 讨论(0)
  • 2021-01-29 22:41

    I find numpy.column_stack() most intuitive:

    np.savetxt('myfile.txt', np.column_stack([x,y,z]))
    
    0 讨论(0)
提交回复
热议问题