在python中添加列表列表

时间:2012-11-08 12:49:56

标签: python list append

我有一份清单清单。我想将一个数组的元素添加到其中一个子列表中,但我添加它的那个取决于数组的长度。

import numpy as np
import numpy.linalg
from numpy import matrix
from scipy.linalg import inv,det,eig
import random
import matplotlib.pyplot as plt
import pylab
from numpy import vstack
from scipy.optimize import curve_fit
from array import array
import copy

def getstabmat(orign, small, specsize, alln):
    #FIRST MAKE THE MATRIX
    matfound=0
    while (matfound==0):
        n=orign
        A=np.empty((n,n))
        I=np.ones((n))
        for i in range(0,n):
            for j in range(i+1,n):
                A[j,i]=random.random()
        for i in range(0,n):
            for j in range(i+1,n):
                    A[i,j] = A[j,i]
        for i in range(n):
            A[i,i]=1
        #NOW REMOVE NEGATIVE ELEMENTS AND KEEP SOLVING
        allpos=0
        while (allpos !=1): #loop for dealing just with getting it positive
            x = numpy.linalg.solve(A,I)
            if any(tl<small for tl in x): #if any of the solutions in x are negative or small
                p=np.where(x==min(x)) # find the most negative solution, p is the position
                x=np.delete(x, p, 0)
                A=np.delete(A, p, 0)
                A=np.delete(A, p, 1)
                I=np.delete(I, p, 0)
                n=n-1
            else:
                allpos=1
        #now test for stability, only do it once and remove one element before returning to check positiveness.
        J=np.empty((n,n)) # make empty jacobian
        for i in range (0,n):
            for j in range (0,n):
                if (i==j): # if we are looking at the diagonal of the matrix, there is a special formula for species dealing with itself
                    tsum = 0
                    for k in range (0,n): #for the summation part
                        tsum = tsum + A[i][k]*x[k] # x is vector of fixed points obtained before
                    J[i][j] = 1 - A[i][j]*x[i] - tsum
                else:
                    J[i][j] = -A[i][j]*x[i]
                    #now jacobian at fixed point has been constructed
        Jeig, Jvec =  eig(J) # get the eigenvalues and eigenvectors
        #run through eigenvalues and find out if any of them are positive
        if any(tl>0 for tl in Jeig.real): #if any eigenvalues are positive
            matfound=0
        else:
            if ((alln==0 and len(A)==specsize) or alln==1): # if the matrix found has five species
                matfound=1
            else:
                matfound=0
    return A, x

def main():

    mats=3 #number of matrices to find
    orign=15
    alln=1 #if alln=1, that means that all sizes of stable matrix should be returned
    n=5 # the number of different species wanted in each matrix
    small=0.0001 #the fractional size that a species is when it is considered to be extinct
    a=0
    sortedspec=[[]]*10
    specad=[]
    while (a<mats): #while all the mats have not been found
        print a
        A, specfp = getstabmat(orign, small, n, alln) #15 is the original size of matrix to pass to fnc.n is the size that will be returned
        a=a+1
        print specfp
        print len(specfp)
        for i in range (0,len(specfp)):
            (sortedspec[len(specfp)]).append(specfp[i])
    print sortedspec

if __name__ == '__main__':
  main()

所以如果:

specfp = [ 0.78076862  0.79608003  0.50719552]

然后我希望将每个元素添加到列表sortedspec [3]中。但是,我最终将我的数组中的每个元素添加到每个列表元素中。为什么这样做,我可以解决它吗?三江源。

1 个答案:

答案 0 :(得分:3)

sortedspec=[[]]*10

这将使您的列表包含对同一(单个)子列表的10个引用,因此更新其中一个子列表也将反映在其他子列表中:

>>> sortedspec[0].append(1)
>>> sortedspec
[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]

您需要做的是创建10个不同的列表,例如使用列表解析:

sortedspec=[[] for i in range(10)]