我的代码是
T=np.empty()
for N in range ("some gemetric numbers: 1,2,4,8,16):
T[N]= trap(f1,a,b,N)
这是程序的样子
使用发电机:
def geom_generator(max_number):
i = 1
while i < max_number:
yield i
i = i*2
for i in geom_generator(max_number=1000): # just a random sample
# do something
print (i)
输出:
1
2
4
8
16
32
64
128
256
512
您可以循环固定序列:
for n in [1, 2, 4, 8, 16]:
# do stuff with n
或者您可以动态生成序列:
n = 1
while True:
# do stuff with n
n *= 2
break
请注意,除非您处于特定条件,否则第二个示例将永远循环。