dimanche 18 août 2019

Find the list of number in another list using nested For loops and If condition

I have 2 different excel files file 1/file 2. I have stored the values of the columns in 2 different lists. I have to search the number present in file 1 with file 2 and I wanted the output as per file 3/ExpectedAnswer.

File 1

File 2

File 3/ Expected Answer

I tried the below code for the above requirement. But I don't know where I'm going wrong.

for j in range(len(terr_code)):
    g=terr_code[j]
    #print(g)
    for lists in Zip_code:
        Zip_code= lists.split(";")
        while('' in Zip_code):
        Zip_code.remove('')     
    for i in range(len(Zip_code)):
        #print(i)
        h=Zip_code[i]
        print(g)
        if g in h:                            
            print(h)
            territory_code.append(str(terr_code[j]))
            print(territory_code[j])
            final_list.append(Zip_terr_Hem['NAME'][i])
            #print(final_list)
            s = ";"
            s= s.join(str(v) for v in final_list)
            #print(s)
            final_file['Territory Code'] = pd.Series(str(terr_code[j]))
            final_file['Territory Name'] = pd.Series(s)
final_file = pd.DataFrame(final_file )
final_file.to_csv('test file.csv', index=False)

The first for loop is working fine. But when I try to print the list of number from the 2nd for loop, the first number is getting printed multiple time. And though both the list are working, still they are not getting inside the if condition. Please tell me what I'm doing wrong here. Thanks

Aucun commentaire:

Enregistrer un commentaire