How to copy data from a numpy array to another

前端 未结 7 1703
逝去的感伤
逝去的感伤 2020-11-28 03:07

What is the fastest way to copy data from array b to array a, without modifying the address of array a. I need this because an external library (PyFFTW) uses a pointer to my

7条回答
  •  悲哀的现实
    2020-11-28 03:36

    you can easy use:

    b = 1*a
    

    this is the fastest way, but also have some problems. If you don't define directly the dtype of a and also doesn't check the dtype of b you can get into trouble. For example:

    a = np.arange(10)        # dtype = int64
    b = 1*a                  # dtype = int64
    
    a = np.arange(10.)       # dtype = float64
    b = 1*a                  # dtype = float64
    
    a = np.arange(10)        # dtype = int64
    b = 1. * a               # dtype = float64
    

    I hope, I could make the point clear. Sometimes you will have a data type change with just one little operation.

提交回复
热议问题