我正在创建一个找到多项式根的程序。有术语对象的列表(术语)。每个术语具有功效,系数和符号(负或正)属性。我希望该生成器返回幂为0的项的系数,但它似乎不起作用。 我知道这个问题很可能是有条件的,因为当我删除它时,它会返回每个术语的系数列表
[term.coeff for term in self.terms if term.power == 0]
class InvalidInput(Exception):
pass
class Term(object):
def __init__(self, term):
self.sign = term[0]
self.coeff = term[1]
self.power = term[4]
self.string = term
def __repr__(self):
return self.string
class Polynomial(object):
#parse quotient string into list of its terms
def __init__(self, quotient):
self.quotient = quotient
if len(self.quotient) % 5 != 0:
raise InvalidInput
self.terms = []
self.term_count = int(len(self.quotient) // 5)
for i_term in range(self.term_count):
term = self.quotient[i_term*5:i_term*5+5]
self.terms.append(Term(term))
self.order = max( [term.power for term in self.terms] )
print([term.power for term in self.terms])
self.const = [term.coeff for term in self.terms if term.power == 0]
Poly = Polynomial("+5x^2-3x^1+5x^0")
答案 0 :(得分:0)
有效
df_2 = df.loc[english_only, ['text']]
答案 1 :(得分:0)
我意识到了我的问题。我正在将类型字符串与类型int进行比较,该类型将始终返回false。我对其进行了更改,以便将属性存储为整数。