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 15:14

If you look here, it might 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.

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

Or from a sequence of sequences.

import numpy as np
a = np.array( [[2,3,4], [3,4,5]] )
查看更多
Animai°情兽
3楼-- · 2020-02-07 15:16

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.

查看更多
祖国的老花朵
4楼-- · 2020-02-07 15:16
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

查看更多
登录 后发表回答