Reputation: 3011
Suppose I have a list of items, like:
['apple', 'red', 'apple', 'red', 'red', 'pear']
I want a dictionary that counts how many times each item appears in the list. So for the list above the result should be:
{'apple': 2, 'red': 3, 'pear': 1}
How can I do this simply in Python?
If you are only interested in counting instances of a single element in a list, see How do I count the occurrences of a list item?.
Upvotes: 301
Views: 451803
Reputation: 1
First create a list of elements to count
elements = [1, 2, 3, 2, 1, 3, 2, 1, 1, 4, 5, 4, 4]
make an empty dictionary, we also do the same with lists
>>> counts = {}
create a for loop that will count for the occurrences of the "key", for each occurrence we add 1
for element in elements:
if element in counts:
counts[element] +=1
check whether we encountered the key before or not if so we add 1, if not we use "else" so the new key is added to the dictionary.
>>> else:
>>> counts[element] = 1
Now print counts using 'items() so we could create sequence of the key-value pairs
for element, count in counts.items():
print(element, ":", count)
here the items() method shows us the key-value pairs, as if we ask: "for element aka in the element load the previous data into 'count' and update it into a sequence of key-value pairs which is what the item() does
CODE WITH NO COMMENTARY:
elements = [1, 2, 3, 2, 1, 3, 2, 1, 1, 4, 5, 4, 4]
counts = {}
for element in elements:
if element in counts:
counts[element] += 1
else:
counts[element] = 1
for element, count in counts.items():
print(element, ":", count)
OUTPUT:
1:4
2:3
3:2
4:3
5:1
Upvotes: 0
Reputation: 11
That is an easy answer m8!
def equalizeArray(arr):
# Counting the frequency of each element in the array
freq = {}
for i in arr:
if i not in freq:
freq[i] = 1
else:
freq[i] += 1
# Finding the element with the highest frequency
max_freq = max(freq.values())
# Calculating the number of deletions required
for key,value in freq.items():
if value == max_freq:
print(key,"been repeated:",value,"times")
Upvotes: 1
Reputation: 1
mylist = [1,2,1,5,1,1,6,'a','a','b']
result = {}
for i in mylist:
result[i] = mylist.count(i)
print(result)
Upvotes: -1
Reputation: 61643
If you use Numpy, the unique
function can tell you how many times each value appeared by passing return_counts=True
:
>>> data = ['apple', 'red', 'apple', 'red', 'red', 'pear']
>>> np.unique(data, return_counts=True)
(array(['apple', 'pear', 'red'], dtype='<U5'), array([2, 1, 3]))
The counts are in the same order as the distinct elements that were found; thus we can use the usual trick to create the desired dictionary (passing the two elements as separate arguments to zip
):
>>> dict(zip(*np.unique(data, return_counts=True)))
{'apple': 2, 'pear': 1, 'red': 3}
If you specifically have a large input Numpy array of small integers, you may get better performance from bincount
:
>>> data = np.random.randint(10, size=100)
>>> data
array([1, 0, 0, 3, 3, 4, 2, 4, 4, 0, 4, 8, 7, 4, 4, 8, 7, 0, 0, 2, 4, 2,
0, 9, 0, 2, 7, 0, 7, 7, 5, 6, 6, 8, 4, 2, 7, 6, 0, 3, 6, 3, 0, 4,
8, 8, 9, 5, 2, 2, 5, 1, 1, 1, 9, 9, 5, 0, 1, 1, 9, 5, 4, 9, 5, 2,
7, 3, 9, 0, 1, 4, 9, 1, 1, 5, 4, 7, 5, 0, 3, 5, 1, 9, 4, 8, 8, 9,
7, 7, 7, 5, 6, 3, 2, 4, 3, 9, 6, 0])
>>> np.bincount(data)
array([14, 10, 9, 8, 14, 10, 6, 11, 7, 11])
The n
th value in the output array indicates the number of times that n
appeared, so we can create the dictionary if desired using enumerate
:
>>> dict(enumerate(np.bincount(data)))
{0: 14, 1: 10, 2: 9, 3: 8, 4: 14, 5: 10, 6: 6, 7: 11, 8: 7, 9: 11}
Upvotes: 2
Reputation: 16328
In 2.7 and 3.1, there is the special Counter
(dict
subclass) for this purpose.
>>> from collections import Counter
>>> Counter(['apple','red','apple','red','red','pear'])
Counter({'red': 3, 'apple': 2, 'pear': 1})
Upvotes: 408
Reputation: 1368
Simply use list property count\
i = ['apple','red','apple','red','red','pear']
d = {x:i.count(x) for x in i}
print d
output :
{'pear': 1, 'apple': 2, 'red': 3}
Upvotes: 77
Reputation: 6618
I like:
counts = dict()
for i in items:
counts[i] = counts.get(i, 0) + 1
.get allows you to specify a default value if the key does not exist.
Upvotes: 358
Reputation: 26747
L = ['apple','red','apple','red','red','pear']
d = {}
[d.__setitem__(item,1+d.get(item,0)) for item in L]
print d
Gives {'pear': 1, 'apple': 2, 'red': 3}
Upvotes: 5
Reputation: 4332
I always thought that for a task that trivial, I wouldn't want to import anything. But i may be wrong, depending on collections.Counter being faster or not.
items = "Whats the simpliest way to add the list items to a dictionary "
stats = {}
for i in items:
if i in stats:
stats[i] += 1
else:
stats[i] = 1
# bonus
for i in sorted(stats, key=stats.get):
print("%d×'%s'" % (stats[i], i))
I think this may be preferable to using count(), because it will only go over the iterable once, whereas count may search the entire thing on every iteration. I used this method to parse many megabytes of statistical data and it always was reasonably fast.
Upvotes: 33
Reputation: 169494
>>> L = ['apple','red','apple','red','red','pear']
>>> from collections import defaultdict
>>> d = defaultdict(int)
>>> for i in L:
... d[i] += 1
>>> d
defaultdict(<type 'int'>, {'pear': 1, 'apple': 2, 'red': 3})
Upvotes: 66