Suppose I have
data =
[[a, a, c],
[b, c, c],
[c, b, b],
[b, a, c]]
I want to get a list containing the element that occurs the most in each
Here's a solution without using the collections module
def get_most_common(data):
data = zip(*data)
count_dict = {}
common = []
for col in data:
for val in col:
count_dict[val] = count_dict.get(val, 0) + 1
max_count = max([count_dict[key] for key in count_dict])
common.append(filter(lambda k: count_dict[k] == max_count, count_dict))
return common
if __name__ == "__main__":
data = [['a','a','b'],
['b','c','c'],
['a','b','b'],
['b','a','c']]
print get_most_common(data)