2012-06-29 21 views
1

我有以下代碼:Python的TypeErrors:「‘清單’對象不是可調用」和‘‘功能’的對象是unsubscriptable’

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穩定回購協議中的版本。

[剪斷]這裏:

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)) 
+1

'[1,2,3,4]()' - nope:not callable。 'def myFunc():pass; myFunc []' - nope:不可索引。 – 2012-06-29 20:20:43

回答

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包含不同類型的對象比你這裏假設什麼。

+0

另外,在第一個例子中,如果'i'是一個函數列表,它將起作用,因爲索引[0]的函數將被調用。由於函數是對象,它們可以放在列表中。現在第二個例子根本無法工作。 – heltonbiker

+0

好吧,這很愚蠢 - 問題出現在突變階段,特別是這個部分:'+ [op [0](randint(left,right)),op [1]] +' 我連接了列表而不是附加對基因的新操作。可愛的傢伙:) – BeNikis

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指數。

相關問題