目前我在执行的操作是,通过python编写一个判断语句
max_idx = 0
max_val = predictions_list[0][1]
num = 0
for i in range(len(predictions_list)):
if predictions_list[i][1] >= max_val:
max_val = predictions_list[i][1]
max_idx = i
#print ("%s = %f" % (predictions_list[max_idx][0], predictions_list[max_idx][1]))
# 判断相似度大于等于0.9时输出
if predictions_list[max_idx][1] >= 0.9:
print("%s = %f" % (predictions_list[max_idx][0], predictions_list[max_idx][1]))
num = predictions_list[max_idx][0] # 储存上个数的值
else:
# 否则小于0.9时是上个数字+1
predictions_list[max_idx][1] < 0.9
predictions_list[max_idx][0] = str[num] + str[1]
print("%s = %f" % (predictions_list[max_idx][0], predictions_list[max_idx][1]))
File "" , line 61, in
TypeError: 'type' object isn't subscriptable
第61行为,倒数第二个代码行,在搜了一圈也没找到答案
你给的代码的第16行str[num] + str[1]这个里面的num是一个你前面定义的变量吧,然后你str[num]是想把num转换为字符串的格式对吧。如果是这样的话应该为str(num)才是格式转化。题主在弄变量名的时候尽量避免用函数名等特殊的来做函数名。比如本次错误就是 str[1]为取str变量的序号为1的字符,导致str[num]转换弄混了应该是str(num)
str是类型,你不要把str当变量名
str[num] + str[1]
这里应该替换成你自己定义的变量,不要写str
你str[num]是想干啥?