Created
June 3, 2011 10:18
-
-
Save msalvadores/1006138 to your computer and use it in GitHub Desktop.
python-find-occurrences-of-strings-from-a-reference-file-within-an-input-file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#stackoverflow answer http://stackoverflow.com/questions/6225590/python-find-occurrences-of-strings-from-a-reference-file-within-an-input-file | |
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 | |
if __name__ == "__main__": | |
x = count_line_occurrences(["a","b","c"],["c","c","b","a"]) | |
print x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment