gpt4 book ai didi

Python 类型错误 : "' list' object is not callable"and "' function' object is unsubscriptable"

转载 作者:行者123 更新时间:2023-12-01 05:59:16 26 4
gpt4 key购买 nike

我有以下代码:

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

最佳答案

    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 包含与您假设的对象类型不同的对象在这里。

关于Python 类型错误 : "' list' object is not callable"and "' function' object is unsubscriptable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11268429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com