Note that there are some explanatory texts on larger screens.

plurals
  1. POAppending a list of lists in python
    primarykey
    data
    text
    <p>I have a list of lists. I want to add the elements of an array to one of the sublists, but the one I add it to depends on the length of the array.</p> <pre><code>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&lt;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&gt;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&lt;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() </code></pre> <p>So if:</p> <pre><code>specfp = [ 0.78076862 0.79608003 0.50719552] </code></pre> <p>Then I want each element to be added to the list sortedspec[3]. However, I end up with each element of my array being added to EVERY list element. Why is it doing this and can I fix it? Thankyou.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload