8

假设用户输入:

"daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm"

您将如何找到最多的连续“a”以及如何删除“a”并只留下其中 2 个而不是之前的大量。

我正在考虑将每个字母附加到一个新的空列表中,但我不确定这是否正确或之后该怎么做。

我真的不知道从哪里开始,但这就是我的想法:

  1. 要求用户输入。
  2. 创建一个空列表
  3. 将输入中的每个字母附加到列表中

接下来是什么我不知道。

第二次编辑(沿着这些方向):

sentence = input("Enter your text: ")
new_sentance = " ".join(sentence.split())
length = len(new_sentance)
alist = []
while (length>0):
    alist
print ()
4

5 回答 5

5

从输入字符串开始:

input = "daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm"
  • 要获得最大连续出现次数,您将使用:

    max(len(s) for s in re.findall(r'a+', input))
    
  • 要仅用 2 个“a”替换最长的不间断“a”序列,您可以使用:

    maxMatch = max(re.finditer(r'a+', input), key= lambda m: len(m.group()))
    output = input[:maxMatch.start()] + "aa" + input[maxMatch.end():]
    

    MatchObject首先,我通过针对 regex 测试输入字符串来获得可迭代的s a+,然后用于max获得MatchObject最大长度的 s 。然后,我将原始字符串的部分拼接到匹配的开头,字符串“aa”,以及匹配结束后的原始字符串部分,为您提供最终输出。

  • 要将所有出现的超过 2 个“a”替换为 2 个“a”,您可以使用:

    output = re.sub(r'a{3,}', "aa", input)
    
于 2013-09-13T03:41:39.320 回答
3

A lower level approach if you don't want to use regular expressions.

def count_and_reduce(s, a):
    num = 0
    maxnum = 0
    out = ''
    for c in s:
        if c == a:
            num += 1
            maxnum = max(num, maxnum)
        else:
            num = 0
        if num <= 2:
            out += c

    return maxnum, out
于 2013-09-13T02:34:46.027 回答
2

我在评论和另一个问题中看到了几个正则表达式的答案,所以我要走一条不同的路。只是得到计数可以通过许多不同的方式完成。

from itertools import groupby

inp = 'daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm';
char_groups = groupby(inp, lambda char:char=='a')
counts = [len(list(group)) for char, group in char_groups]
# We know every other element of 'counts' is an 'a' element.
# We just need to know whether to start at zero or one.
# If inp starts with 'a', start at 0. Otherwise start at 1.
max(counts[not inp.startswith('a')::2]) # 11

我很确定我见过的两个正则表达式答案都会用两个“a”替换每个“aa+”字符串。如果您只想用 'aa' 替换最长的 'a' 字符串,而不要理会其余的:

char_groups = groupby(inp)
counts = [(char, len(list(group))) for char, group in char_groups]
max_idx = max(range(len(counts)), key=lambda i:counts[i][1] if counts[i][0]=='a' else 0)
result = ''.join(char*count for char, count in counts[:max_idx]) + 'aa' + ''.join(char*count for char, count in counts[max_idx+1:])
# 'daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaafnnasm'
于 2013-09-13T01:53:17.750 回答
2

我会这样做的方式。

s = "daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm"
print(s)
a_len = len(s)
found_a_len = 0
keep_going = True
while a_len>0 and keep_going:
    aas = "a" * a_len
    if aas in s:
        found_a_len = a_len
        keep_going = False
    a_len=a_len -1
print ("max length of a:" , found_a_len)
keep_going = True
while keep_going:
    s=s.replace("aaa","aa")
    if "aaa" not in s:
        keep_going = False
print(s)

这输出:

daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm
max length of a: 11
daslakndlaajnjndibniaafijdnfijdnsijfnsdinifaafnnasm

有些人可能不喜欢我的编码风格,但对我来说,这段代码很容易推理。

于 2013-09-13T02:57:44.817 回答
0

尝试这个:

import collections

def runLenEncode(s):
    start, i = 0, 0;
    answer = []
    while i<len(s):
        while i<len(s) and s[start]==s[i]:
            i += 1
        answer.append((s[start], i-start))
        start = i
    return answer

def runLenFilter(encoding, thresholds, repLens):
    answer = []
    for char, count in encoding:
        if char in thresholds and count>=thresholds[char]:
            count = repLens[char]
        answer.append(char*count)
    return ''.join(answer)

def maxFilter(encoding, repLens):
    maxCounts = collections.defaultdict(int)
    for char, count in encoding:
        if char in repLens and count > maxCounts[char]:
            maxCounts[char] = count
    maxCounts = dict(maxCounts)
    answer = []
    for char, count in encoding:
        if char in repLens and count==maxCounts[char]:
            count = repLens[char]
        answer.append(char*count)
    return ''.join(answer)

if __name__ == "__main__":
    print('starting')

    s = "daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaaaaaaaaaaafnnasm"
    encoding = runLenEncode(s)
    print("encoding:", encoding)
    thresholds = {'a':3}
    repLens = {'a':2}
    decoded = runLenFilter(encoding, thresholds, repLens)
    print('lenFilter:', decoded)
    filtered = maxFilter(encoding, repLens)
    print("maxFilter:", filtered)

    print('done')

和输出

$ python3 myFile.py
starting
encoding: [('d', 1), ('a', 1), ('s', 1), ('l', 1), ('a', 1), ('k', 1), ('n', 1), ('d', 1), ('l', 1), ('a', 5), ('j', 1), ('n', 1), ('j', 1), ('n', 1), ('d', 1), ('i', 1), ('b', 1), ('n', 1), ('i', 1), ('a', 3), ('f', 1), ('i', 1), ('j', 1), ('d', 1), ('n', 1), ('f', 1), ('i', 1), ('j', 1), ('d', 1), ('n', 1), ('s', 1), ('i', 1), ('j', 1), ('f', 1), ('n', 1), ('s', 1), ('d', 1), ('i', 1), ('n', 1), ('i', 1), ('f', 1), ('a', 11), ('f', 1), ('n', 2), ('a', 1), ('s', 1), ('m', 1)]
lenFilter: daslakndlaajnjndibniaafijdnfijdnsijfnsdinifaafnnasm
maxFilter: daslakndlaaaaajnjndibniaaafijdnfijdnsijfnsdinifaafnnasm
done
于 2013-09-13T03:28:18.403 回答