How to save a list as numpy array in python?

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

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

相关标签:
9条回答
  • 2020-12-04 09:02
    import numpy as np 
    
    ... ## other code
    

    some list comprehension

    t=[nodel[ nodenext[i][j] ] for j in idx]
                #for each link, find the node lables 
                #t is the list of node labels 
    

    Convert the list to a numpy array using the array method specified in the numpy library.

    t=np.array(t)
    

    This may be helpful: https://numpy.org/devdocs/user/basics.creation.html

    0 讨论(0)
  • 2020-12-04 09:06

    Yes it is:

    a = numpy.array([1,2,3])
    
    0 讨论(0)
  • 2020-12-04 09:08

    Here is a more complete example:

    import csv
    import numpy as np
    
    with open('filename','rb') as csvfile:
         cdl = list( csv.reader(csvfile,delimiter='\t'))
         print "Number of records = " + str(len(cdl))
    
    #then later
    
    npcdl = np.array(cdl)
    

    Hope this helps!!

    0 讨论(0)
提交回复
热议问题