Filter strings where n equals characters in a string

Is there a way to filter these lines from a list of lines that contains, for example, 3 identical characters per line? I created a method that can do this, but I'm curious if there is a more pythonic way, or a more efficient or easier way to do this.

list_of_strings = [] def check_3_in_row(string): for ch in set(string): if ch*3 in string: return True return False new_list = [x for x in list_of_strings if check_3_in_row(x)] 

EDIT: I just found one solution:

 new_list = [x for x in set(keywords) if any(ch*3 in x for ch in x)] 

But I'm not sure which way is faster - regexp or this.

+6
source share
1 answer

You can use regex like

 >>> list_of_strings = ["aaa", "dasdas", "aaafff", "afff", "abbbc"] >>> [x for x in list_of_strings if re.search(r'(.)\1{2}', x)] ['aaa', 'aaafff', 'afff', 'abbbc'] 

Here . matches any character and is fixed in the group ( (.) ). And we check if the same captured character is saved (we use backreference \1 for the first captured group in the line) two times ( {2} means two times).

+6
source

Source: https://habr.com/ru/post/987859/


All Articles