python-将类实例的输出作为输入传递给另一个
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-将类实例的输出作为输入传递给另一个,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3992字,纯文字阅读大概需要6分钟。
内容图文
![python-将类实例的输出作为输入传递给另一个](/upload/InfoBanner/zyjiaocheng/691/0f4047ea6b4d4604825a0eaf18800484.jpg)
我有以下代码:
import numpy as np
class B():
def __init__(self, a,b):
self.a = a
self.b = b
class Criteria():
def __init__(self, method, minimum, maximum, measures=None):
self.method = method
self.minimum = minimum
self.maximum = maximum
self.measures = measures if measures is not None else None
def calcs(self):
if self.measures is not None:
for x in self.measures:
if (x.a > self.minimum and x.a < self.maximum):
x.a = 999
return self.measures
def avg(self):
if self.measures is not None:
return np.average([x.value for x in self.measures])
else:
return np.average(3)# Here should be the result where None is defined
# Now I put just an arbitrary number
class Evaluate():
def __init__(self, criteria):
self.criteria = criteria
testdata = np.array([Evaluate(
np.array([Criteria('V', 1,100,
np.array([B(100, 0.1),
B(11, 0.3),
B(300, 0.2),
B(33, 0.1)],dtype=object)
),
Criteria('AVG', 22, 220, None)])
)])
for x in testdata:
for idx, el in enumerate(x.criteria):
if el.method == 'V':
el.calcs() # this result must be passed as input to the `el.avg()`
if el.method == 'AVG':
el.avg()
我有一个B类,其中包含一些数据(a和b字段).
我正在将这些数据加载到Criteria类中,以通过条件并进行相应的更改.
然后,Evaluate类将满足以上所有条件.
我在Criteria类中使用了measures = None,因为在例如avg函数的情况下,我可能没有度量值可以在其上计算平均值,但是我(这是我的情况)来自先前Criteria类的度量值我正在应用平均值.
现在,我要完成的就是这个.
最初加载数据:
B(100, 0.1),
B(11, 0.3),
B(300, 0.2),
B(33, 0.1)
通过传递条件(通过运行calcs函数),这些数据将变为:
B(100, 0.1),
B(999, 0.3),
B(300, 0.2),
B(999, 0.1)
现在,以上数据(第一个条件的结果/输出必须作为输入传递给第二个条件,并使用avg函数计算平均值.我不知道在没有任何参数的情况下是否有可能avg功能.拥有自我.
因此,我的最终结果将是值599.5.
解决方法:
这是脚本的修改.主要是我添加了代表.但是我也将措施的无情况更改为空列表[]:
import numpy as np
class B():
def __init__(self, a,b):
self.a = a
self.b = b
def __repr__(self):
return 'B(%s, %s)'%(self.a, self.b)
class Criteria():
def __init__(self, method, minimum, maximum, measures=None):
self.method = method
self.minimum = minimum
self.maximum = maximum
self.measures = measures # may be None
def __repr__(self):
# **edit** work with None
if self.measures is None:
measures = 'measures: None'
else:
measures = [' measures:[']
for m in self.measures:
measures.append(' {}'.format(m))
measures.append(' ]')
measures = '\n'+ '\n'.join(measures)
return 'C({0.method},{0.minimum},{0.maximum}, {1})'.format(self, measures)
def calcs(self):
if self.measures is not None:
for x in self.measures:
if (x.a > self.minimum and x.a < self.maximum):
x.a = 999
return self.measures
def avg(self, calcs=None):
# **edit** work with None and calcs
if calcs is None:
calcs = self.measures
if calcs is None:
return 'none'
elif len(calcs)==0:
return '[]'
else:
return np.average([x.a for x in calcs])
class Evaluate():
def __init__(self, criteria):
self.criteria = criteria
def __repr__(self):
#return 'E({})'.format(self.criteria)
astr = 'Evaluate \n'
for c in self.criteria:
astr += '{}\n'.format(c)
return astr
考虑制作一组Criteria对象. AVG必须以某种方式知道其使用的度量.一种方法是在施工过程中使用的测量参数.
b1 = np.array([B(100, 0.1),
B(11, 0.3),
B(300, 0.2),
B(33, 0.1)],dtype=object)
b2 = np.array([B(1, 0.1), B(2,.5)])
c1 = Criteria('V', 1, 100, b1)
c2 = Criteria('V', 2, 200, b2)
c3 = Criteria('AVG', 22, 220, None)
c4 = Criteria('AVG', 22, 220, c2.measures)
c5 = Criteria('AVG', 22, 222, c1.measures)
编辑更改迭代以保存最后的计算结果,如果AVG??度量为“无”,则使用该迭代. C.avg现在带有一个可选参数.
last_calcs = None
for c in alist:
if c.method=='V':
last_calcs = c.calcs()
print('calcs', c.measures)
if c.method=='AVG':
if c.measures is None:
avg = c.avg(last_calcs)
else:
avg = c.avg()
print('AVG', avg)
有:
alist = [c3,c1,c3,c2,c3,c4, c5]
这将产生:
evaluate
AVG none # c3 with nothing preceeding
calcs [B(100, 0.1) B(999, 0.3) B(300, 0.2) B(999, 0.1)]
AVG 599.5 # c3 following c1
calcs [B(1, 0.1) B(2, 0.5)]
AVG 1.5 # c3 following c2
AVG 1.5 # c4 with same measures as c2
AVG 599.5 # c5 with same measures as c1
内容总结
以上是互联网集市为您收集整理的python-将类实例的输出作为输入传递给另一个全部内容,希望文章能够帮你解决python-将类实例的输出作为输入传递给另一个所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。