How to save a list as a numpy array in python?

I need to know if python list can be saved as a numPy array.

+112
python list numpy
May 10 '11 at 13:51
source share
8 answers

If you look here, he can tell you what you need to know.

http://www.scipy.org/Tentative_NumPy_Tutorial#head-d3f8e5fe9b903f3c3b2a5c0dfceb60d71602cf93

Basically, you can create an array from a sequence.

from numpy import array a = array( [2,3,4] ) 

Or from a sequence of sequences.

 from numpy import array a = array( [[2,3,4], [3,4,5]] ) 
+143
May 10 '11 at
source share

do you mean something like this?

 from numpy import array a = array( your_list ) 
+35
May 10 '11 at 1:56 pm
source share

Yes it:

 a = numpy.array([1,2,3]) 
+17
May 10 '11 at 13:55
source share

Do 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')) 
+16
May 10 '11 at 14:10
source share

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

 >>> a = [1, 2] >>> np.asarray(a) array([1, 2]) 
+7
Feb 24 '17 at 14:38
source share

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

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

gives you as an array of list b in the form specified in the change form.

+4
Nov 14 '14 at 8:47
source share

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
Sep 12 '17 at 23:16
source share
 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 might be useful: https://numpy.org/devdocs/user/basics.creation.html

0
Aug 08 '19 at 13:43 on
source share



All Articles