首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python TypeErrors:"‘list’对象不可调用“和"'function‘对象不可订阅”

Python TypeErrors:"‘list’对象不可调用“和"'function‘对象不可订阅”
EN

Stack Overflow用户
提问于 2012-06-30 04:18:26
回答 2查看 1.6K关注 0票数 1

我有以下代码:

代码语言:javascript
复制
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时,得到的结果如下:

代码语言:javascript
复制
 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作为排序时,错误是这样的:

代码语言:javascript
复制
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稳定代码库中的版本。

截图如下:

代码语言:javascript
复制
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))
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-30 04:24:21

代码语言:javascript
复制
    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包含的对象类型与您在这里假设的不同。

票数 2
EN

Stack Overflow用户

发布于 2012-06-30 04:33:32

尝试以下修改:

代码语言:javascript
复制
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个索引中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11268429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档