python - Check how many times a key of a tuple repeats itself in a dict? -
i have dictionary following structure:
{('key1','key2'): 50, ('key3','key4'): 70, ('key1','key5'): 90.....}
i want count in dictionary number of times 'key1' example appears first word in key tuple.
i started writing below mentioned code not think further:
count = 0 leng = 0 = 0 key1,key2 in range(1,len(bigrams)): count = count +1 leng = leng + (bigrams.get((key1,key2),0)) print(count) print(leng)
any suggestion how should proceed ?
from collections import defaultdict der = {('key1','key2'): 50, ('key3','key4'): 70, ('key1','key5'): 90} b = defaultdict(int) item, ler in der: b[item] += 1 print b ## defaultdict(int, {'key1': 2, 'key3': 1}) print b['key1'] ## [2]
Comments
Post a Comment