- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码:
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/
最后一行给我一个错误 check_alert_json = [ ] for k in check_job_stats_json.keys(): if : check_aler
我是按照《Django for beginners》这本书学习Django的,有个奇怪的问题。下面的代码片段确实有效,但 Pylint 一直向我显示错误,我知道错误发生了。但是作者什么也没做。请问有人
我正在尝试编写一个函数来返回任何小于 1000 的数字的字符串。 每次我在交互式提示符下运行代码时,它似乎都可以正常工作,但是当我尝试导入 wordify 并使用高于 20 的测试编号运行它时,它会失
我从代码中收到一条错误消息,其中显示 TypeError: 'int' object is unsubscriptable。经过一番研究,我明白了它的意思,但我不明白为什么会出现问题。 我将问题范围缩
当我运行这段代码时,Python 解释器出现了一个奇怪的错误: def make_map(): map = [[Tile(0, 0) for col in range(MAP_WIDTH)]
我正在模拟循环算法,下面列出的代码给我错误 RR.Przesuniecie[Oczekujace_procesy] TypeError: 'instancemethod' object is unsu
使用 Python 运行此代码时出现此错误: TypeError: "NoneType" object is unsubscriptable". 代码: number = 0 with open('p
我有一个产生错误的 python 程序: def update_ranges(phonemelist) : """ updating the rows and columns of the l
在 python 中我得到这个错误: TypeError: 'int' object is unsubscriptable 这发生在行: sectorcalc[i][2]= ((today[2]/ye
我是 python 的新手,不太了解我的问题,非常感谢您的帮助。无论如何,这是编码行。 def Banker(warrior): gold = open(chairs[warrior-1],
我正在尝试扫描我的服务器是否存在漏洞,我正在使用 Wapiti 来执行此操作。奇怪的是,当我运行它时,我收到此错误。怎么了? root@server [~/wapiti-2.3.0/bin]# pyt
我正在尝试从 openerp 中具有此函数的类中读取另一个类的对象内的字段: def desc_cert(self, cr, uid, ids, field_name, field_args, con
我编译了这个程序,它从细胞图像中提取特征,并在 python-3.5 上使用 randomforestclassifier 判断它是否癌变。我是在 Windows 8.1 上制作的,它运行良好。但它不
我在执行以下脚本时遇到错误“int object is unsubscriptable”: element.reduceByKey( lambda x , y : x[1]+y[1]) with el
我只是将以下“最小”重现案例放在一起(最小引号是因为我想确保 pylint 没有抛出其他错误、警告、提示或建议 - 这意味着有一些样板): pylint_error.py : """ Docstrin
我有以下代码: from random import randint,choice add=lambda x:lambda y:x+y sub=lambda x:lambda y:x-y mul=la
我昨晚更新了 GoogleAppEngineLauncher,现在我收到了这个错误 Python 2.6.1: /usr/bin/python Tue Nov 8 09:25:13 2011 A
sqlite3 lib 对象 connect 上的 fetchone() 函数出现问题: 我执行的查询可能并不总是返回值; 检查它是否有返回值; 如果它返回一个值,我使用 fetchone()[0]
我想使用 Python 中的 Azure Functions 写入 Azure Blob 存储。 我正在使用 Azure Functions 的输出 Blob 存储绑定(bind)。 我的函数.jso
我想使用 Python 中的 Azure Functions 写入 Azure Blob 存储。 我正在使用 Azure Functions 的输出 Blob 存储绑定(bind)。 我的函数.jso
我是一名优秀的程序员,十分优秀!