保持阵列中只有真正的正值

时间:2015-02-27 22:45:56

标签: python arrays sorting

我正在寻找一种方法,只在我的列表(np.coeff)中添加数字(T_g),如果这个数字是真实的和正数的话。 (见最后一个循环) 在循环中,如果我在将这些值附加到np.roots(coeff)之前打印T_g,它会输出一个包含四个值的数组,其中一个值是真正的。该值并不总是在数组中的相同位置。 知道怎么做吗?我想过做一个if循环:

if *value of np.roots is real positive*: 
    T_g.append(rootss(coeff))

但是你可以看到我根本不知道如何写这个。

from numpy import *
import numpy as np
import pylab
import scipy
from scipy.optimize import leastsq
from math import *
import matplotlib.pyplot as plt
from scipy import integrate

#define constants used in equations:
alb = 0.2 #constant albedo
F = 866 #J/s*m**2
R = 287 #J/K*kg
U = 5 #m/s
C_p = 1000 #J/K*kg
C_d = 0.0015
p1 = 10**4
p2 = 10**5
p3 = 10**6 #Pa
sig = 5.67*10**-8 #J/s*m**2*K**4 #Stefan-Boltzmann cst


T_a = 424.663545093 #K  ###Found in code FindT_a.py


#define other constants in equation:
rho = p1/(R*T_a)      #FIRST USING P = 10**4 Pa
a = rho*C_p*C_d*U



#define a function (frange), that accepts float arguments
import math
def xfrange(start, stop, step):
    while start < stop:
        yield start
        start += step


z_daylist = []
T_g = []
roots = []

for i in range(len(z_daylist)):
    coeff = array([(sig),0,0,(a),((-a*T_a)-z_daylist[i])])
    def rootss(coeff):
        return np.roots(coeff)
    #print np.roots(coeff) #this prints an array of four values where one is real positive, not always at same position
        #keep only the positive real roots > How???
    T_g.append(rootss(coeff))

1 个答案:

答案 0 :(得分:0)

T_g.append([x for x in rootss(coeff) if np.isreal(x) and x > 0])