suppose your dictionary is named d and you want a count of the values:
from collections import Counter
d = {1: "dog", 2: "cat", 3: "dog", 4: "elephant"}
counts = Counter(d.values())
Now you can use the counts Counter:
# Values in the original dictionary
counts['dog'] # 2
counts['elephant'] # 1
# Value not in the original dictionary
counts['fish'] # 0
If you must use custom code and classes for this instead of the standard library, I think your error is here:
result.add((self.count(i), self.items[i]))
What you may want is:
animal_name = self.items[i] #?
result.add((self.count(animal_name), animal_name))
Otherwise you may want to share with us what is in self.items...
suppose your dictionary is named d and you want a count of the values:
from collections import Counter
d = {1: "dog", 2: "cat", 3: "dog", 4: "elephant"}
counts = Counter(d.values())
Now you can use the counts Counter:
# Values in the original dictionary
counts['dog'] # 2
counts['elephant'] # 1
# Value not in the original dictionary
counts['fish'] # 0
If you must use custom code and classes for this instead of the standard library, I think your error is here:
result.add((self.count(i), self.items[i]))
What you may want is:
animal_name = self.items[i] #?
result.add((self.count(animal_name), animal_name))
Otherwise you may want to share with us what is in self.items...
d = {1:"Dog",2:"Cat", 3:"Dog", 4:"Elephant"}
count = {}
for v in d.values():
if(not(v in count)):
count[v] = 0
count[v] += 1
print(count)
{'Dog': 2, 'Cat': 1, 'Elephant': 1}
Videos
You should use collections.Counter:
>>> from collections import Counter
>>> d = {'a':3, 'b':9, 'c':88, 'd': 3}
>>> Counter(d.values()).most_common()
[(3, 2), (88, 1), (9, 1)]
I'd use a defaultdict to do this (basically the more general version of the Counter). This has been in since 2.4.
from collections import defaultdict
counter = defaultdict( int )
b = {'a':3,'b':9,'c':88,'d':3}
for k,v in b.iteritems():
counter[v]+=1
print counter[3]
print counter[88]
#will print
>> 2
>> 3
Since you want to count the number of times each language appears in each position in a ranking, we can use dict.setdefault (which allows to set a default value if a key doesn't exist in a dict yet) to set a default dictionary of zero values to each language rank as we iterate over the list and walk the dicts.
The idea is to initialize a dictionary that maps language names to 0s (using dict.fromkeys) and in every iteration, if a language exists in some position, then add the number corresponding to the language by one in the inner dictionary.
out = {}
for d in lst:
for num, language in d.items():
out.setdefault(num, dict.fromkeys(lst[0].values(), 0))
out[num][language] += 1
Output:
{'language1': {'C++': 1, 'PHP': 1, 'Python': 2, 'Java': 1},
'language2': {'C++': 2, 'PHP': 1, 'Python': 1, 'Java': 1},
'language3': {'C++': 2, 'PHP': 0, 'Python': 2, 'Java': 1},
'language4': {'C++': 0, 'PHP': 2, 'Python': 1, 'Java': 2}}
Basically enke's answer, but with defaultdict and Counter.
from collections import Counter, defaultdict
result = defaultdict(Counter)
for d in data:
for k, v in d.items():
result[k].update([v])
Note that Counters return 0 for missing keys, e.g.
>>> Counter()['C++']
0