-2

I am having a text file in which some binary numbers are there. I want to count number of occurrence of some digits/characters using pattern, and I want to sort it in descending order (till this point code working fine. but I want result should show only more than 7 characters. it means i can change in my selection pattern = r"(0+1+0+1+0+1+)" {<7}

import re
from collections import Counter



pattern = r"0+1+0+1+0+1+"

test_str = '0101010110110110110110110101010111011101110111101111010101111010111010101111011010101011011011011011011011'

cnt = Counter(re.findall(pattern, test_str))
print(cnt.most_common())


# result [('011011011', 2), ('010101', 1), ('0111011101111', 1), ('010111101', 1), ('0111101101', 1)]

result should be display only more than 8 character it no supposed to show ('010101', 1)

RAnsari
  • 17
  • 3

2 Answers2

0

Counter will do exactly the job.

from collections import Counter

cnt = Counter(re.findall(pattern, test_str))
print(cnt.most_common())  # [('110110', 3), ('110100', 2), ('101110', 2)]
0x0fba
  • 1,520
  • 1
  • 1
  • 11
-1

If you just want to print the highest value you can do this:

print(max(cnt.items()))
MDavidson
  • 67
  • 2