你可以把它写成一个发电机:
def nestList(f,x,c):
for i in range(c):
yield x
x = f(x)
yield x
import math
print list(nestList(math.cos, 1.0, 10))
Run Code Online (Sandbox Code Playgroud)
或者,如果您希望将结果作为列表,则可以在循环中追加:
def nestList(f,x,c):
result = [x]
for i in range(c):
x = f(x)
result.append(x)
return result
import math
print nestList(math.cos, 1.0, 10)
Run Code Online (Sandbox Code Playgroud)