1

我有以下代码:

from random import randint,choice

add=lambda x:lambda y:x+y
sub=lambda x:lambda y:x-y
mul=lambda x:lambda y:x*y


ops=[[add,'+'],[sub,'-'],[mul,'*']]

def gen_expression(length,left,right):
    expr=[]
    for i in range(length):
        op=choice(ops)
        expr.append([op[0](randint(left,right)),op[1]])
    return expr

def eval_expression (expr,x):
    for i in expr:
           x=i[0](x)
    return x

def eval_expression2 (expr,x):
    for i in expr:
           x=i(x)
    return x
[snip , see end of post]
def genetic_arithmetic(get,start,length,left,right):
    batch=[]
    found = False
    for i in range(30):
        batch.append(gen_expression(length,left,right))

    while not found:
        batch=sorted(batch,key=lambda y:abs(eval_expression(y,start)-get))
        print evald_expression_tostring(batch[0],start)+"\n\n"

                #combine                        
        for w in range(len(batch)):
            rind=choice(range(length))
            batch.append(batch[w][:rind]+choice(batch)[rind:])

            #mutate
        for w in range(len(batch)):
            rind=choice(range(length))
            op=choice(ops)
            batch.append(batch[w][:rind]+[op[0](randint(left,right)),op[1]]+batch[w][rind+1:])

        found=(eval_expression(batch[0],start)==get)

    print "\n\n"+evald_expression_tostring(batch[0],start)

当我尝试使用 eval_expression 作为排序键调用genetic_artihmetic 时,我得到了这个:

 Traceback (most recent call last):
  File "<pyshell#113>", line 1, in <module>
    genetic_arithmetic(0,10,10,-10,10)
  File "/home/benikis/graming/py/genetic_number.py", line 50, in genetic_arithmetic
    batch=sorted(batch,key=lambda y:abs(eval_expression(y,start)-get))
  File "/home/benikis/graming/py/genetic_number.py", line 50, in <lambda>
    batch=sorted(batch,key=lambda y:abs(eval_expression(y,start)-get))
  File "/home/benikis/graming/py/genetic_number.py", line 20, in eval_expression
    x=i[0](x)
TypeError: 'function' object is unsubscriptable

当我尝试使用 eval_expression2 作为排序时,错误是这样的:

Traceback (most recent call last):
  File "<pyshell#114>", line 1, in <module>
    genetic_arithmetic(0,10,10,-10,10)
  File "/home/benikis/graming/py/genetic_number.py", line 50, in genetic_arithmetic
    batch=sorted(batch,key=lambda y:abs(eval_expression2(y,start)-get))
  File "/home/benikis/graming/py/genetic_number.py", line 50, in <lambda>
    batch=sorted(batch,key=lambda y:abs(eval_expression2(y,start)-get))
  File "/home/benikis/graming/py/genetic_number.py", line 25, in eval_expression2
    x=i(x)
TypeError: 'list' object is not callable

据我所知,我的猜测是 sorted() 正在尝试对子列表进行递归排序,也许?这里到底发生了什么?

Python 版本是 2.6 - debian stable repos 中的版本。

[剪辑]在这里:

def expression_tostring(expr):
    expr_str=len(expr)*'('+'x '
    for i in expr :
        if i[1]=='*':
            n=i[0](1)
        else:
            n=i[0](0)
        expr_str+=i[1]+' '+str(n)+') '

    return expr_str

def evald_expression_tostring(expr,x):
    exprstr=expression_tostring(expr).replace('x',str(x))
    return exprstr+ ' = ' + str(eval_expression(expr,x))
4

2 回答 2

2
    x=i[0](x)  #here i is a function so you can't perform indexing operation on it      


    x=i(x) #here i is a list so you can't call it as a function

在这两种情况下, 的值i都是从中获取的expr,可能expr包含与您在此处假设的不同类型的对象。

于 2012-06-29T20:24:20.603 回答
0

试试这个修改:

def gen_expression(length,left,right):
    expr=[]
    for i in range(length):
        op=choice(ops)
        expr.append([op[0], randint(left,right),op[1]])
    return expr

def eval_expression (expr,x):
    for i in expr:
           x=i[0](i[1])
    return x

您已经expr.append([op[0](randint(left,right)),op[1]])将调用函数的返回值放入第 0 个索引中。

于 2012-06-29T20:33:31.960 回答