how to set selection criteria in regex (how to set t the number of character in selection) in python


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))

# 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)

Asked By: RAnsari



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)]
Answered By: 0x0fba

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

Answered By: MDavidson
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.