我想整合函数func(s),当给定s值时,它会给出一个列表。
def func(s):
for i in range(1, 100):
p = i * 2 + s * 2
yield p
def trap0 (f,lowerl,upperl,n):
h = float (upperl-lowerl)/n
s =(list(f(lowerl))+list(f(upperl)))/2
for i in range (1,n):
s = s+f(lowerl+i*h)
return s * -h
lists1=[]
lists1.append(trap0(func,-1,9,50))
print lists1
我一直在努力 TypeError:+:'generator'和'generator'的不支持的操作数类型。请帮忙
答案 0 :(得分:0)
由于func
返回一个生成器,您需要将其返回的内容转换为列表:
def trap0 (f,lowerl,upperl,n):
h = float (upperl-lowerl)/n
s =(list(f(lowerl)) + list(f(upperl)))/2
for i in range (1,n):
s = s + list(f(lowerl+i*h))
return s * -h