Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:一个迭代函数_Python - Fatal编程技术网

Python:一个迭代函数

Python:一个迭代函数,python,Python,我正在尝试编写一个程序,以xn+1=xn2+c的形式为我进行迭代。我该怎么做 例如,对于c=1和x0=1/2 (1/2)2+1=5/4, (5/4)2+1=25/16+1=41/16 ... 等 我的代码在这里不起作用: def main(): import math print("This program iterates a function of the form x^2 + c:") x=complex(input("Enter a seed value:"))

我正在尝试编写一个程序,以xn+1=xn2+c的形式为我进行迭代。我该怎么做

例如,对于c=1和x0=1/2

(1/2)2+1=5/4,
(5/4)2+1=25/16+1=41/16
... 等

我的代码在这里不起作用:

def main():
   import math
   print("This program iterates a function of the form x^2 + c:")
   x=complex(input("Enter a seed value:"))
   c=complex(input("Enter a value for c"))

for i in range(50):
   f(0) = x*x + c
   f(i) = f(i-1)*f(i-1) + c
   print(f(i))
main()

Python不允许我像matlab那样使用
f(0)
f(i)
,有什么替代方法吗?

制表符在Python中非常重要,请尝试下面的代码。这是一个很难猜测你的意图是什么,但它使你更接近最终结果

def main(x):
    import math
    print("This program iterates a function of the form x^2 + c:")
    c = complex(input("Enter a value for c"))

    print "C : %s" % c

    f = [x * x + c]

    for i in range(1, 50):
        f.append(f[i - 1] * f[i - 1] + c)

    print f

main(2)

制表符在python中非常重要,请尝试下面的代码。这是一个很难猜测你的意图是什么,但它使你更接近最终结果

def main(x):
    import math
    print("This program iterates a function of the form x^2 + c:")
    c = complex(input("Enter a value for c"))

    print "C : %s" % c

    f = [x * x + c]

    for i in range(1, 50):
        f.append(f[i - 1] * f[i - 1] + c)

    print f

main(2)

如果您对保留所有部分结果不感兴趣,可以这样迭代:

import math
print("This program iterates a function of the form x^2 + c:")
x = complex(input("Enter a seed value:"))
c = complex(input("Enter a value for c"))

f = x * x + c
for _ in range(50):
   f = f * f + c
   print(f)
def main():
    n = complex(input("Enter a seed value: "))
    c = complex(input("Enter a value for c: "))

    print n
    for term in range(50):
        n = f(n, c)
        print n

如果您对保留所有部分结果不感兴趣,可以这样迭代:

import math
print("This program iterates a function of the form x^2 + c:")
x = complex(input("Enter a seed value:"))
c = complex(input("Enter a value for c"))

f = x * x + c
for _ in range(50):
   f = f * f + c
   print(f)
def main():
    n = complex(input("Enter a seed value: "))
    c = complex(input("Enter a value for c: "))

    print n
    for term in range(50):
        n = f(n, c)
        print n

让我们调用函数
f

def f(x, c):
    return x * x + c
然后在
main()

import math
print("This program iterates a function of the form x^2 + c:")
x = complex(input("Enter a seed value:"))
c = complex(input("Enter a value for c"))

f = x * x + c
for _ in range(50):
   f = f * f + c
   print(f)
def main():
    n = complex(input("Enter a seed value: "))
    c = complex(input("Enter a value for c: "))

    print n
    for term in range(50):
        n = f(n, c)
        print n

在每次迭代中,
n
被重新分配给
f
的返回值,之前的值为
n

,让我们调用函数
f

def f(x, c):
    return x * x + c
然后在
main()

import math
print("This program iterates a function of the form x^2 + c:")
x = complex(input("Enter a seed value:"))
c = complex(input("Enter a value for c"))

f = x * x + c
for _ in range(50):
   f = f * f + c
   print(f)
def main():
    n = complex(input("Enter a seed value: "))
    c = complex(input("Enter a value for c: "))

    print n
    for term in range(50):
        n = f(n, c)
        print n
在每次迭代中,
n
被重新分配给
f
的返回值,之前的值为
n

good day

# x_(n+1) = x_(n)^2 + c

def f(n,c):
    a=0
    while a < n:
        a +=1
        yield a * a + c
你好

# x_(n+1) = x_(n)^2 + c

def f(n,c):
    a=0
    while a < n:
        a +=1
        yield a * a + c

Python中有一种称为生成器函数的特殊机制。它返回一个对象,该对象记住最后一个状态,并使用上一次调用(迭代器)的值执行函数体

它在语法上与正常函数不同,只是使用了
yield
命令而不是
return
。您通常可以在需要迭代器的地方使用这种生成器函数,例如for循环、容器构造函数等。请参阅代码:

def f(x, c, n=5):
    while n > 0:
        yield x        # returns x0 as first value
        x = x * x + c  # this value is to be returned next time
        n -= 1         # decrement the sequence counter


# Using the generator function in a for loop.
for value in f(1/2, 1, 14):  # I want 14 members of the sequence
    print(value)

# Using the generator function to build a list of the values.
print('----------------------------------')
lst = list(f(1/2, 1, 10))    # 10 members wanted here
print(lst)


# Using the standard module called fractions for the same function.
print('==================================')
from fractions import Fraction as frac

# Using the generator function in a for loop.
for value in f(frac(1, 2), 1):  # default number of loop used here
    print(value)

# Using the generator function to build a list of the values.
print('----------------------------------')
lst = list(f(frac(1, 2), 1, 10))  # 10 members wanted here
print(lst)

# Generating Mandelbrot set values.
print('==================================')
# Using the generator function in a for loop.
for value in f(complex(0), complex(0, 1)):  # default number of loop used here
    print(value)
Python不像matlab那样以符号方式计算表达式。但是,它具有标准模块分数,该模块具有分数类来表示分数。您也可以对该类型使用相同的生成器函数,因为它定义了自己的乘法和加法。由于Python整数的限制小于float,使用分数可能会得到更大的结果(如果有意义的话)。但是你可能想生成一个Mandelbrot集,对吗

它显示在我的控制台上(换行):


Python中有一种称为生成器函数的特殊机制。它返回一个对象,该对象记住最后一个状态,并使用上一次调用(迭代器)的值执行函数体

它在语法上与正常函数不同,只是使用了
yield
命令而不是
return
。您通常可以在需要迭代器的地方使用这种生成器函数,例如for循环、容器构造函数等。请参阅代码:

def f(x, c, n=5):
    while n > 0:
        yield x        # returns x0 as first value
        x = x * x + c  # this value is to be returned next time
        n -= 1         # decrement the sequence counter


# Using the generator function in a for loop.
for value in f(1/2, 1, 14):  # I want 14 members of the sequence
    print(value)

# Using the generator function to build a list of the values.
print('----------------------------------')
lst = list(f(1/2, 1, 10))    # 10 members wanted here
print(lst)


# Using the standard module called fractions for the same function.
print('==================================')
from fractions import Fraction as frac

# Using the generator function in a for loop.
for value in f(frac(1, 2), 1):  # default number of loop used here
    print(value)

# Using the generator function to build a list of the values.
print('----------------------------------')
lst = list(f(frac(1, 2), 1, 10))  # 10 members wanted here
print(lst)

# Generating Mandelbrot set values.
print('==================================')
# Using the generator function in a for loop.
for value in f(complex(0), complex(0, 1)):  # default number of loop used here
    print(value)
Python不像matlab那样以符号方式计算表达式。但是,它具有标准模块分数,该模块具有分数类来表示分数。您也可以对该类型使用相同的生成器函数,因为它定义了自己的乘法和加法。由于Python整数的限制小于float,使用分数可能会得到更大的结果(如果有意义的话)。但是你可能想生成一个Mandelbrot集,对吗

它显示在我的控制台上(换行):


除了
f(i)=f(i-1)+c没有任何意义。读完代码后,我实际上正要说。。。你到底想干什么?洛莉,对不起,布莱恩,这是我的错。我已经根据应该发生的事情修改了我的代码。我这里的主要问题是,既然Python似乎不接受它,我可以用什么来代替f()?我已经在代码中提供了,您将希望使用.append和f[I]来获得以前存储的值。顺便说一句,我已经扩展了这个例子,实际上是以前面的值的乘法为基础,除了
f(I)=f(I-1)+c没有任何意义。读完代码后,我实际上正要说。。。你到底想干什么?洛莉,对不起,布莱恩,这是我的错。我已经根据应该发生的事情修改了我的代码。我这里的主要问题是,既然Python似乎不接受它,我可以用什么来代替f()?我已经在代码中提供了,您将希望使用.append和f[I]来获得以前存储的值。顺便说一句,我已经扩展了这个例子,实际上是基于我得到的前一个值“不能分配函数调用”的乘法。这是我第一次使用Python,所以我不知道在这里使用什么。我需要知道我可以使用什么工具。我想你需要进一步了解python的基础知识。这里有很多错误。你有没有学习过python教程?我以前写过函数的代码。我学习的教程在两周内发布,因此这仍然是一种新语言。
f(n)
在python中是函数调用而不是数组操作。我得到“无法分配函数调用”。这是我第一次使用Python,所以我不知道在这里使用什么。我需要知道我可以使用什么工具。我想你需要进一步了解python的基础知识。这里有很多错误。你有没有学习过python教程?我以前写过函数的代码。我学习的教程在两周内发布,所以这仍然是一种新语言。
f(n)
在python中是一个函数调用,而不是数组操作创造性的,哇。我现在绝对可以尊重计算机科学专业了。创意,哇。我现在绝对可以尊重计算机科学专业的学生了。@RikPoggi:不。我的是迭代的,就像你的一样:)唯一的区别是我将数学函数从
main()
提取到
f()
中,使它更易于阅读、测试和维护。@J