python - to display the maximum count of numbers -
this question has answer here:
for example consider file: input.txt
12 23 45 45 45 34 34 56 12 12 12 67 89
what need code display number repeated maximum times output should follows
12 4 45 3 34 2 23 1 56 1 67 1 89 1
the code wrote:
= [] f = open("out","r") lines = f.readlines() in lines: j = i.split() a.append(j) print len(a)
it prints total length 13
if 1 suggest how code in python obtain result expect.it helpful??????
using collections.counter
>>> l # assume read list of numbers file. [12, 23, 45, 45, 45, 34, 34, 56, 12, 12, 12, 67, 89] >>> collections import counter >>> counter(l).most_common() [(12, 4), (45, 3), (34, 2), (67, 1), (23, 1), (56, 1), (89, 1)]
Comments
Post a Comment