Skip to content

Instantly share code, notes, and snippets.

@msalvadores
Created June 3, 2011 10:18
Show Gist options
  • Save msalvadores/1006138 to your computer and use it in GitHub Desktop.
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
#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