How to save a list as numpy array in python?

前端 未结 9 885
心在旅途
心在旅途 2020-12-04 08:27

Is possible to construct a NumPy array from a python list?

相关标签:
9条回答
  • 2020-12-04 08:46

    you mean something like this ?

    from numpy  import array
    a = array( your_list )
    
    0 讨论(0)
  • 2020-12-04 08:47

    First of all, I'd recommend you to go through NumPy's Quickstart tutorial, which will probably help with these basic questions.

    You can directly create an array from a list as:

    import numpy as np
    a = np.array( [2,3,4] )
    

    Or from a from a nested list in the same way:

    import numpy as np
    a = np.array( [[2,3,4], [3,4,5]] )
    
    0 讨论(0)
  • 2020-12-04 08:47

    You can use numpy.asarray, for example to convert a list into an array:

    >>> a = [1, 2]
    >>> np.asarray(a)
    array([1, 2])
    
    0 讨论(0)
  • 2020-12-04 08:47

    I suppose, you mean converting a list into a numpy array? Then,

    import numpy as np
    
    # b is some list, then ...    
    a = np.array(b).reshape(lengthDim0, lengthDim1);
    

    gives you a as an array of list b in the shape given in reshape.

    0 讨论(0)
  • 2020-12-04 08:58

    maybe:

    import numpy as np
    a=[[1,1],[2,2]]
    b=np.asarray(a)
    print(type(b))
    

    output:

    <class 'numpy.ndarray'>
    
    0 讨论(0)
  • 2020-12-04 09:01

    You want to save it as a file?

    import numpy as np
    
    myList = [1, 2, 3]
    
    np.array(myList).dump(open('array.npy', 'wb'))
    

    ... and then read:

    myArray = np.load(open('array.npy', 'rb'))
    
    0 讨论(0)
提交回复
热议问题