Python вложенные функции для тех же параметров
У меня есть 2 списка Python, и я хочу рассчитать 4 различных значения, используя разные функции (TP, TN, FP, FN). Лучше, если я смогу определить параметры во внешней вложенной функции без определения параметров для каждой функции, так как параметры одинаковы для всех 4 функций. Я реализовал функцию, но она дает только вывод функции TP. не могли бы вы, пожалуйста, кто-нибудь помочь мне найти проблему здесь
def evaluation(list1,list2):
def TP():
count1 = 0
for i in range(0,35):
if Jac_test_list[i].strip()==Simmilar_list[i].strip()=='True':
count1+=1
#return count
print ('TP count :' + str( count1))
return TP
def TN():
count2 = 0
for i in range(0,35):
if Jac_test_list[i].strip()==Simmilar_list[i].strip()=='False':
count2+=1
#return count
print ('TN count :' + str( count2))
return TN
def FP():
count3 = 0
for i in range(0,35):
if (Jac_test_list[i].strip()=='True') & (Simmilar_list[i].strip()=='False'):
count3+=1
#return count
print ('FP count :' + str( count3))
return FP
def FN():
count4 = 0
for i in range(0,35):
if (Jac_test_list[i].strip()=='False') & (Simmilar_list[i].strip()=='True'):
count4+=1
#return count
print ('FN count :' + str( count4))
return FN
1 ответ
Решение
Я должен сказать, что это не лучший способ обмена параметрами между функциями, а просто для исправления вашего кода, ниже может быть то, что вам нужно.
def evaluation(Jac_test_list,Simmilar_list):
def TP():
count1 = 0
for i in range(0,35):
if Jac_test_list[i].strip()==Simmilar_list[i].strip()=='True':
count1+=1
#return count
print ('TP count :' + str( count1))
def TN():
count2 = 0
for i in range(0,35):
if Jac_test_list[i].strip()==Simmilar_list[i].strip()=='False':
count2+=1
#return count
print ('TN count :' + str( count2))
def FP():
count3 = 0
for i in range(0,35):
if (Jac_test_list[i].strip()=='True') & (Simmilar_list[i].strip()=='False'):
count3+=1
#return count
print ('FP count :' + str( count3))
def FN():
count4 = 0
for i in range(0,35):
if (Jac_test_list[i].strip()=='False') & (Simmilar_list[i].strip()=='True'):
count4+=1
#return count
print ('FN count :' + str( count4))
TP()
TN()
FP()
FN()