I have two lists and would like to create a dictionary to record the occurrence of tuples.
My current code:
tup_to_find_test = [('good', 'pea'), ('leaf', 'sweet')]
self_per_list_test = [('leaf', 'liquid'), ('leaf', 'sweet'), ('leaf', 'sweet'),('good', 'pea'),('good', 'pea'),('good', 'pea')]
from collections import defaultdict
tup_dict_test = defaultdict(int)
for tup_to_find_test in self_per_list_test:
tup_dict_test[tup_to_find_test]+=1
My result is:
defaultdict(int, {('leaf', 'liquid'): 1, ('leaf', 'sweet'): 1, ('good', 'pea'): 3})
My desired result is:
('leaf', 'liquid'): 0, ('leaf', 'sweet'): 2, ('good', 'pea'): 3})
I do not know why the count of ('leaf', 'liquid') is 1. Isn't the default integer of defaultdict(int) zero? Why I got 1 for the ('leaf', 'liquid') tuple?