python - Match common words in two files, and add info from one file to the other -
is there way find matches between first word in 2 files ([0]=[0], , add second word [1] 1 file other in common lines?
example:
file 1:
cow 3 lion 5 monkey 2 file 2:
lion meat goat grass gorilla banana desired output:
lion 5 meat #common first word between files + second word file 2 in file 1 any ideas?
you can use collections.defaultdict:
import collections open('file1') f1, open('file2') f2: my_dict = collections.defaultdict(list) x in f1: x = x.strip().split() my_dict[x[0]].append(x[1]) x in f2: x = x.strip().split() if x[0] in my_dict: my_dict[x[0]].append(x[1]) my_dict like: {'lion': [5,'meat'], ....}
Comments
Post a Comment