Reputation: 41
I'm trying to use the Counter()
function, which is usually simple and works without issue..
I've got the following:
name_freq = []
for names in list.names:
name_freq.append(names)
print(Counter(name_freq).most_common(3))
and I'm running into a really strange issue. It counts every single item as 1
, even duplicates.
Example count result: [{'bob':1, 'bill':1, 'bob':1}]
I didn't even think that was possible in Python list[]
. The spelling and capitalization is the exact same.
Any ideas why this is happening?
Upvotes: 3
Views: 3719
Reputation: 2047
import collections
name_freq = []
names= ['a','b','a','d','e','e','e','a']
for name in names:
name_freq.append(name)
print(collections.Counter(name_freq).most_common(3))
RESULT
[('a', 3), ('e', 3), ('b', 1)]
Upvotes: 0
Reputation: 864
Works also for me with a simple tuple. Try if this works. If, focus your effort on the list.names object
from collections import Counter
name_freq = []
list_names = ('bob', 'bill', 'jim', 'john', 'bob')
for names in list_names:
name_freq.append(names)
print(Counter(name_freq).most_common(3))
Upvotes: 2
Reputation: 203
nameoverall =[]
names =[["aa","bb","cc"],["aa","cc"],["dd"],["ee"]]
for namelist in names:
nameoverall.extend(namelist)
print(Counter(nameoverall).most_common(3))
Upvotes: 0
Reputation: 17368
Your code is working perfectly in my system. Run it once again.
Upvotes: 0