“有没有办法获得最连续字母表的序列?”

“Is there a way to get sequence of most consecutive alphabet?”

我正在研究一个 python 问题,它有一个字符串,例如“aaabbcc”和一个数字 n(整数)。 我必须显示恰好出现 n 次的任何字母字符的序列。

我试过代码

import collections
str1 = 'aaabbcc'
d = collections.defaultdict(int)
for c in str1:
    d[c] += 1

for c in sorted(d, key=d.get, reverse=True):
  if d[c] > 1:
      print(c, d[c])

但我得到的输出是

a 3
b 2
c 2

我期待从用户那里获取整数输入 3 的输出。

integer= 3 
sequence= aaa

是否有其他解决方案?

这是一个似乎有效的基于正则表达式的方法:

input = "ddaaabbbbbbbbccceeeeeee"
n = 3
for match in re.finditer(r'(.)(?!)(.){' + str(n-1) + r'}(?!)', input):
print(match.group(0)[1:])

aaa
ccc

上面示例中使用的正则表达式模式是这样的:

(.)(?!)(.){2}(?!)

这表示:

(.)     match and capture any single character
(?!)  assert that the next character is different
(.)     then match and capture that next character
{2}   which is then followed by that same character exactly twice (total of 3)
(?!)  after three instances, the character that follows is NOT the same

一种loop-based方法(应该很漂亮straight-forward):

str1 = 'aaabbcc'
n = 3

count = 1
last = None
for char in str1:
    if last == char:
        count += 1
    else:
        if count == n:
            print(f'integer: {n} sequence: {n*last}')
        last = char
        count = 1
if count == n:
    print(f'integer: {n} sequence: {n*last}')

如果找到包含 str1.

的最后一个字符的解决方案,最后一个 if 语句将打印出解决方案

基于itertools.groupby的方法:

from itertools import groupby

str1 = 'aaabbcc'
n = 3

for key, group in groupby(str1):
    if len(tuple(group)) == n:
        print(f'integer: {n} sequence: {n*key}')

withot a key groupby 将按身份对序列进行分组 - 即每次 str1 中的字母发生变化时,它将产生该字母及其出现。