簡體   English   中英

在python中添加列表列表

[英]Appending a list of lists in python

我有一個清單清單。 我想將一個數組的元素添加到一個子列表中,但是我要添加到其中的一個取決於數組的長度。

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]。 但是,最后我將數組的每個元素添加到每個列表元素中。 為什么要這樣做,我可以解決嗎? 謝謝。

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)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM