I have two files. Call one file as a reference file. This file contains a list of strings each on a separate line. The other file is an input file. It also has strings on on each line.
I want to find the occurrence of each string from the reference file within the input file. This is my code
def count_line_occurrences(ref_list,input_list):
line_counter = {}
# Initialization
for ref_line in ref_list:
ref_line = ref_line.rstrip()
line_counter[ref_line] = 0
for input_line in input_list:
input_line = input_line.rstrip()
for ref_line in ref_list:
#print ref_line
for input_line in input_list:
#print input_line
if str(input_line).find(str(ref_line)) != -1:
print 'found ' + ref_line
line_counter[ref_line] += 1
return line_counter
However it is not working.
Note – This is not a HW problem. But this is a part of a bigger task. Also, strangely, I have implemented this part in Perl and it is working fine. I want to shift the project to Python and I am having issues here. Thanks in advance for the help.
It sort of works for me. Calling your function like …
prints out …
@Sumod … isn’t that the expected behavior ?
Edition after seeing @Sumod’s input data
So the problem with your input is that you are not cleaning correctly the trailing characters
\t\n.The following code works …
notice that prior to the init of your counters I do the cleaning of both input lists with …
The
string.stripfunction in python receives the characters I want to clean up.For the input …
I get …