python同时用好list与np.array

a = [1,2] #type()后是list

a = [1 2] #error

***************************************************************

想定义一个array对象(array类是numpy模块里的成员类,但本质是以列表list/元组tuple/字典dict类为基础)怎么办?

 

import numpy as np

 

 

a=np.array([1,2])  # 

a=array([1,2])                     # NameError: name 'array' is not defined

a=np.array([1 2]) # SyntaxError: invalid syntax
a = [1,2]                   #type()后是list

****************************************************************

array可以以列表为基础定义各种矩阵

d = np.array([1+2j,(2,3)],[3,4]],dtype=complex)
d

array([[1.+2.j, 2.+0.j],
       [3.+0.j, 4.+0.j]])

用numpy成员函数生成array

a_array = print np.random.randint(0,3,(2,3))

用array类成员函数改写array页行列,成新array

print np.arange(24).reshape(2,3,4)

 

********************************************************************

import numpy as np


class tyl(object):




def tyl(all):
    all0=[]
    for a in all:
        all0.append(type(a))
    return all0

def dtyl(arrays):
    all0=[]
    for a in arrays:
        all0.append(a.dtype)
    return all0

def tarray(lists):
    all0=[]
    for a in lists:
        all0.append(np.array(a))
    return all0


t=1,2
np.arange(12)
a=[[1,2],(1,2),t,[[2,3],[4,5]],np.arange(12)]
[<class 'list'>, <class 'tuple'>, <class 'tuple'>, <class 'list'>, <class 'numpy.ndarray'>]



b=tarray(a)
b=[array([1, 2]), array([1, 2]), array([1, 2]), array([[2, 3],
       [4, 5]]), array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])]
[<class 'numpy.ndarray'>, <class 'numpy.ndarray'>, <class 'numpy.ndarray'>, <class 'numpy.ndarray'>, <class 'numpy.ndarray'>]
#
dtyl(b)
[dtype('int64'), dtype('int64'), dtype('int64'), dtype('int64'), dtype('int64')]
###array才能dtype


d=np.array([1],[1,2])#error
d=np.array([[1],[1,2]])#pass


e=np.array(['a','b'])#pass
e.dtype
dtype('<U1')
np.array(['aa','b']).dtype
dtype('<U2')
np.array(['aasasass','bs',1]).dtype
dtype('<U8')
np.array(['aasasass','bs',13445]).dtype
dtype('<U8')

>>> np.arange(24).reshape(2,2,2,3)
array([[[[ 0,  1,  2],
         [ 3,  4,  5]],

        [[ 6,  7,  8],
         [ 9, 10, 11]]],


       [[[12, 13, 14],
         [15, 16, 17]],

        [[18, 19, 20],
         [21, 22, 23]]]])

 

参考:python中数组(numpy.array)的基本操作

https://blog.csdn.net/fu6543210/article/details/83240024

 

posted @ 2020-02-10 11:47  Lu_STEEL  阅读(437)  评论(0编辑  收藏  举报