在Python

时间:2016-04-05 10:43:52

标签: python arrays list numpy

我一直在寻找这个,但我找不到任何有帮助的东西。我制作一个我想要的大小的数组时遇到了一些麻烦。

到目前为止,我已使用此代码创建了一个列表列表:

n=4
def matrixA(k):
    A=[]
    for m in range(0,k):
        row=[]
        #A.append([])
        for n in range(0,k):
            if (n==(m+1)) or (n==(m-1)):
                row.append(-deltaX/(deltaX)**2)
            if (n==m):
                row.append(2*deltaX/(deltaX)**2)
            else:
                row.append(0)
        A.append(row)
    return A
pprint.pprint(matrixA(n))
print len(matrixA(n))

我得到了这个输出。

[[128.0, -64.0, 0, 0, 0],
 [-64.0, 0, 128.0, -64.0, 0, 0],
 [0, -64.0, 0, 128.0, -64.0, 0],
 [0, 0, -64.0, 0, 128.0]]
4

现在,我想让它成为一个大小(4,4)的数组。我的问题是,当我执行以下操作(将列表转换为数组并尝试对其进行整形)时:

A=numpy.array(matrixA(n))
print A
print "This is its shape:",A.shape
A.shape=(n,n)

我获得:

[[128.0, -64.0, 0, 0, 0] [-64.0, 0, 128.0, -64.0, 0, 0]
 [0, -64.0, 0, 128.0, -64.0, 0] [0, 0, -64.0, 0, 128.0]]
This is its shape: (4,)

然后是错误:

ValueError: total size of new array must be unchanged

我怎么能从这里得到一个大小(4,4)的数组呢?

2 个答案:

答案 0 :(得分:2)

欢迎来到numpy world。

似乎你想要:

NSString * key1 =@"name";
NSString * obj1 =@"Hayagreeva";
NSString * key2 =@"age";
NSNumber * obj2 =[NSNumber numberWithInt:3];

//1.Empty dictionary. @{} dictionary representation and it is not for  NSMuableDictionary
NSDictionary * dict1 =[NSDictionary dictionary];
NSDictionary * dict2 =[[NSDictionary alloc] init];
NSDictionary * dict3 = @{};

//2.Dictionary with Single Object and Key
NSDictionary * dict4 =[NSDictionary dictionaryWithObject:obj1 forKey:key1];
NSDictionary * dict5 =@{key1:obj1};

//3.Dictionary with Multiple Object and Keys
//Objects and keys are sequentially terminated with nil
NSDictionary * dict6 = [NSDictionary
                        dictionaryWithObjectsAndKeys:obj1,key1,obj2,key2,nil];

NSDictionary * dict7 =[[NSDictionary alloc]
                       initWithObjectsAndKeys:obj1,key1,obj2,key2,nil] ;

//3.Dictionary with Multiple Object and Keys
NSDictionary *dict8 =[NSDictionary
                      dictionaryWithObjects:@[obj1,obj2]
                      forKeys:@[key1,key2]];
NSDictionary *dict9 =[[NSDictionary alloc]
                      initWithObjects:@[obj1,obj2]
                      forKeys:@[key1,key2]];
NSDictionary * dict10 =@{key1:obj1,key2:obj2};

//@[obj1,obj2] is the array representation.  dict6,dict7,dict8 are same.
NSDictionary *dict11 =[[NSDictionary alloc]
                       initWithObjects:[NSArray arrayWithObjects:obj1,obj2,nil]
                       forKeys:[NSArray arrayWithObjects:key1,key2,nil]];

//4.Dictionary with another Dictionary
//@[obj1,obj2] is the array representation.  dict6,dict7,dict8 are same.
NSDictionary *dict12=[NSDictionary dictionaryWithDictionary:dict8];
NSDictionary *dict13=[[NSDictionary alloc] initWithDictionary:dict8];

在构建列表列表时,很难考虑行和列索引。在numpy中你首先塑造,然后填充,这通常更容易。

一步一步:

array([[ 128.,  -64.,    0.,    0.],
       [ -64.,  128.,  -64.,    0.],
       [   0.,  -64.,  128.,  -64.],
       [   0.,    0.,  -64.,  128.]])

所以你想要的是:

In [37]: from numpy import eye,diag,ones # some useful functions

In [38]: eye(4) # identity matrix
Out[38]: 
array([[ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])

In [39]: eye(4)*128
Out[39]: 
array([[ 128.,    0.,    0.,    0.],
       [   0.,  128.,    0.,    0.],
       [   0.,    0.,  128.,    0.],
       [   0.,    0.,    0.,  128.]])

In [40]: ones(3)
Out[40]: array([ 1.,  1.,  1.])

In [41]: diag(ones(3),1)  # see help(diag)
Out[41]: 
array([[ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.],
       [ 0.,  0.,  0.,  0.]])

In [42]: diag(ones(3),1).T   # transpose
Out[42]: 
array([[ 0.,  0.,  0.,  0.],
       [ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.]])

并运行:

def arrayA(n,deltaX):
    A=eye(n)  # id matrix of size n 
    B= diag(ones(n-1),1) # just ahead
    return (2*A-B-B.T)*(deltaX/deltaX**2)

对于大型矩阵,速度更快:

In [45]: arrayA(4,1/64)
Out[45]: 
array([[ 128.,  -64.,    0.,    0.],
       [ -64.,  128.,  -64.,    0.],
       [   0.,  -64.,  128.,  -64.],
       [   0.,    0.,  -64.,  128.]])

答案 1 :(得分:0)

将嵌套列表转换为数组非常简单,而且您正确地执行了此操作:

numpy.array(matrixA(n))

它不起作用的原因是你生成的嵌套列表实际上有不规则数量的"列"

有问题的部分是因为您错过了elif

if (n==(m+1)) or (n==(m-1)):
    row.append(-deltaX/(deltaX)**2)
if (n==m):
    row.append(2*deltaX/(deltaX)**2)

将您的循环更改为:

for m in range(0,k-1):
    row=[]
    #A.append([])
    for n in range(0,k-1):
        if (n==(m+1)) or (n==(m-1)):
            row.append(-deltaX/(deltaX)**2)
        elif (n==m):
            row.append(2*deltaX/(deltaX)**2)
        else:
            row.append(0)
    A.append(row)