I have a 2-column tab-separated input that I would like to populate a dictionary in python
. The first column associates to the key (there are duplicates) and the second column associates to the value.
Sample input:
cat tail
cat whisker
cat meow
cat black
dog tail
dog paw
dog bark
bird beak
I have written the following code, which produces an (albeit wrong) output that contains the dictionary format that I am looking for, which associates one key from col1 to all of its values in col2.
The code that I have been using is:
#!/usr/bin/python
# -*- coding: utf-8 -*-
keys = []
values = []
with open('animal-trial', "rU") as f:
for line in f:
line = line.split()
keys.append(line[0])
values.append(line[1])
d = {}
for k,v in zip(keys, values):
d.setdefault(k, []).append(v)
print d
I have looked up other references [HERE], [HERE] and [HERE], however, all of the suggestions, including with defaultdicts
bring me to the same output, rather than the desired output.
The actual output is:
{'cat': ['tail']}
{'cat': ['tail', 'whisker']}
{'cat': ['tail', 'whisker', 'meow']}
{'cat': ['tail', 'whisker', 'meow', 'black']}
{'dog': ['tail'], 'cat': ['tail', 'whisker', 'meow', 'black']}
{'dog': ['tail', 'paw'], 'cat': ['tail', 'whisker', 'meow', 'black']}
{'dog': ['tail', 'paw', 'bark'], 'cat': ['tail', 'whisker', 'meow', 'black']}
{'bird': ['beak'], 'dog': ['tail', 'paw', 'bark'], 'cat': ['tail', 'whisker', 'meow', 'black']}
The desired output is
{'bird': ['beak'], 'dog': ['tail', 'paw', 'bark'], 'cat': ['tail', 'whisker', 'meow', 'black']}
Can anyone point me to where I am making an error or have a more comprehensive solution so that the final result is one
dictionary?