I want to produce print the most listed item in a list
for example,
aList = ["SE","CpE","SE","CS","CS","SE"]
and result should be
aList = ["SE"]
which is the most listed item. Please Help!
I want to produce print the most listed item in a list
for example,
aList = ["SE","CpE","SE","CS","CS","SE"]
and result should be
aList = ["SE"]
which is the most listed item. Please Help!
from collections import Counter
a = Counter(aList)
a.most_common()[0]
http://docs.python.org/2/library/collections.html#collections.Counter
There are lots of such questions, but anyway.
You can do this using stdlib's collections.Counter
:
from collections import Counter
a = Counter(aList)
a.most_common()[0]
Or you can write your own class/function:
def count(list):
items = {}
for item in list:
if item not in items:
items[item] = 0
items[item] += 1
return items
>>> count(["SE","CpE","SE","CS","CS","SE"])
{"SE": 3,...}