Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.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中的简单L系统_Python_Python 3.x_Function_Iteration_L Systems - Fatal编程技术网

python中的简单L系统

python中的简单L系统,python,python-3.x,function,iteration,l-systems,Python,Python 3.x,Function,Iteration,L Systems,您好,我正在寻找一种在python中实现一个简单L-系统的方法,它将包含三个参数:axiom、规则和迭代次数(如果迭代次数=0,则输出将是以前的输入axiom)。我想出了一些代码,它只在一次迭代中起作用,我不知道如何实现更多 我想出的代码是: # x = axiom # y = rules # z would be iterations which I dont know how to implement def lsystem(x,y): output = '' for

您好,我正在寻找一种在python中实现一个简单L-系统的方法,它将包含三个参数:axiom、规则和迭代次数(如果迭代次数=0,则输出将是以前的输入axiom)。我想出了一些代码,它只在一次迭代中起作用,我不知道如何实现更多

我想出的代码是:

#  x = axiom
#  y = rules
#  z would be iterations which I dont know how to implement

def lsystem(x,y):
    output = ''
    for i in x:
        if i in y:
            output += y[i]
        else:
            output += i
    print(output)

rules = { "A" : "ABA" , "B" : "BBB"}
# output    lsystem("AB",rules) ---> ABABBB    

如果
iterations==0
,则需要返回给定的
公理。在这个函数中,您将返回给定的参数
公理
,因此如果
迭代次数==0
,您将返回给定的、未触及的公理

然后,稍后,在
迭代
结束时,如果存在
迭代
,则从
迭代
中获得的新创建的公理将被转换为
公理
,以便返回良好的值,如果需要,下一次
迭代
将有新创建的公理进行迭代。:)


你的代码有什么问题?您只想知道如何实现
z
参数?需要如何实现:如果z为0,如果z优于x中的迭代次数,等等……是的,我想知道如何实现z参数,我认为它应该实现为某种循环。z=2的步骤看起来像这个output=AB->output=ababbbb-->output abababbbbbbbb如果我的答案符合您的需要,请随时告诉我最新情况按预期工作。非常感谢。我认为这个代码不正确。如果我们将规则设置为:rules:(A->A+B,B->A-B),N=3的结果应该是:A+B+A-B+A+B-A+B,但这里的答案是:A+B+A-B+A+B-A-B
def lsystem(axioms, rules, iterations):
    #    We iterate through our method required numbers of time.
    for _ in range(iterations):
        #    Our newly created axioms from this iteration.
        newAxioms = ''

        #    This is your code, but with renamed variables, for clearer code.
        for axiom in axioms:
            if axiom in rules:
                newAxioms += rules[axiom]
            else:
                newAxioms += axiom
        #    You will need to iterate through your newAxioms next time, so...
        #    We transfer newAxioms, to axioms that is being iterated on, in the for loop.
        axioms = newAxioms
    return axioms

rules = { "A" : "ABA" , "B" : "BBB"}
print(lsystem('AB', rules, 0))
# outputs : 'AB'

print(lsystem('AB', rules, 1))
# outputs : 'ABABBB'

print(lsystem('AB', rules, 2))
# outputs : 'ABABBBABABBBBBBBBB'