How to save a list as numpy array in python?

2020-02-07 14:14发布

I need to know if it is possible to save a python list as a numPy array.

9条回答
姐就是有狂的资本
2楼-- · 2020-02-07 14:56

you mean something like this ?

from numpy  import array
a = array( your_list )
查看更多
老娘就宠你
3楼-- · 2020-02-07 15:02

Yes it is:

a = numpy.array([1,2,3])
查看更多
该账号已被封号
4楼-- · 2020-02-07 15:07

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'))
查看更多
叛逆
5楼-- · 2020-02-07 15:07

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

>>> a = [1, 2]
>>> np.asarray(a)
array([1, 2])
查看更多
Fickle 薄情
6楼-- · 2020-02-07 15:07

maybe:

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

output:

<class 'numpy.ndarray'>
查看更多
forever°为你锁心
7楼-- · 2020-02-07 15:13

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!!

查看更多
登录 后发表回答