-2

i have 2 python list and i want to calculate 4 different values using different functions(TP, TN, FP, FN). its better if i can define parameters in outer nested function without defining parameters for each and every functions as parameters are same for all 4 functions. i have implemented a function, but it gives only the TP function output. could you please someone help me to find the issue here

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
Sampath Rajapaksha
  • 111
  • 1
  • 1
  • 11
  • 2
    Hint: `return` ends the function. Nothing after your first `return TP` will be executed. – BrenBarn May 06 '17 at 21:49
  • What do you expect to see when you call evaluation(list1,list2)? Can you give an example? – Allen Qin May 06 '17 at 21:50
  • Why are you using nested functions here at all? Is this suppose to be a function factory? But then, you are creating closures over `Jac_test_list` and `Simmilar_list`, which are non-local as far as I can tell. – juanpa.arrivillaga May 06 '17 at 22:10
  • @Allen below are the expected output TP count :12, TN count :16, FP count :1, FN count :6 , in other words i want to print all which are in Print() . i have to run this few times. so i like to define parameter in only only place rather than defining them in all 4 functions – Sampath Rajapaksha May 07 '17 at 02:59
  • What's your input lists list1 and list2? Can you give an example? – Allen Qin May 07 '17 at 07:38

1 Answers1

0

I have to say this is not the best way to share parameters among functions but just for the sake of fixing your code, below might be what you are after.

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()
Allen Qin
  • 19,507
  • 8
  • 51
  • 67