Longest consecutive substring of certain character type in python

99封情书 提交于 2021-02-17 00:03:39

问题


Is there a pythonic way to find the length of the longest consecutive substring of a certain character type, for instance the length of the longest consecutive substrings of digits/letters/printable characters?

For example in

s = "43gfd**54452**jhg4**fddsgf**"

The longest digit substring is of length 5, and the longest letter substring is of length 6.


回答1:


Regex and max with length as the key:

In [12]: s = "43gfd54452jhg4fddsgf"

In [13]: max(re.findall(r'\d+', s), key=len)  # digits
Out[13]: '54452'

In [14]: max(re.findall(r'\D+', s), key=len)  # non-digits
Out[14]: 'fddsgf'

Similarly, you can change the Regex pattern to get your desired substring type.




回答2:


If there are always "**" between each substring. All you have to do is iterate over the different elements, keeping in a variable the longest substring you have found so far.

longest_letter = 0
longest_digit = 0
for el in s.split("**"):
    if(el.isalpha()):
        len_letter = len(el)
        if(len_letter > longest_letter):
            longest_letter = len_letter
    if(el.isdigit()):
        len_digit = len(el)
        if(len_digit > longest_digit):
            longest_digit = len_digit
print (longest_letter)
print (longest_digit)


来源:https://stackoverflow.com/questions/54517998/longest-consecutive-substring-of-certain-character-type-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!