I am new to Python. I am trying to find a simple way of getting a count of the number of elements repeated in a list e.g.
MyList = ["a", "b", "a", "c", "c", "a", "c"]
Output:
a: 3
b: 1
c: 3
This question is related to
python
python-2.7
lst = ["a", "b", "a", "c", "c", "a", "c"]
temp=set(lst)
result={}
for i in temp:
result[i]=lst.count(i)
print result
Output:
{'a': 3, 'c': 3, 'b': 1}
yourList = ["a", "b", "a", "c", "c", "a", "c"]
expected outputs {a: 3, b: 1,c:3}
duplicateFrequencies = {}
for i in set(yourList):
duplicateFrequencies[i] = yourList.count(i)
Cheers!! Reference
This works for Python 2.6.6
a = ["a", "b", "a"]
result = dict((i, a.count(i)) for i in a)
print result
prints
{'a': 2, 'b': 1}
Use Counter
>>> from collections import Counter
>>> MyList = ["a", "b", "a", "c", "c", "a", "c"]
>>> c = Counter(MyList)
>>> c
Counter({'a': 3, 'c': 3, 'b': 1})
Source: Stackoverflow.com