删除连续的重复字母

发布于 2024-11-24 11:55:08 字数 307 浏览 3 评论 0原文

寻找一种快速方法,当重复项彼此相邻时将其限制为最多 2 个。

例如:jeeeeeeeep => ['jep','jeep']

在 python 中寻找建议,但很高兴看到任何内容的示例 - 切换并不困难。

感谢您的帮助!

编辑:英语没有连续的任何(或许多)辅音(相同的字母),对吗?让我们限制这一点,这样就不会连续出现重复的辅音,并且连续最多有两个元音

编辑2:我很傻(嘿那个单词有两个辅音),只需检查所有字母,限制每个字母旁边的重复字母其他到两个。

Looking for a fast way to limit duplicates to a max of 2 when they occur next to each other.

For example: jeeeeeeeep => ['jep','jeep']

Looking for suggestions in python but happy to see an example in anything - not difficult to switch.

Thanks for any assistance!

EDIT: English doesn't have any (or many) consonants (same letter) in a row right? Lets limit this so no duplicate consonants in a row and up to two vowels in a row

EDIT2: I'm silly (hey that word has two consonants), just checking all letters, limiting duplicate letters that are next to each other to two.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

川水往事 2024-12-01 11:55:08

这是使用groupby的递归解决方案。我已经将您希望能够重复哪些字符留给您了(尽管默认为元音):

from itertools import groupby

def find_dub_strs(mystring):
    grp = groupby(mystring)
    seq = [(k, len(list(g)) >= 2) for k, g in grp]
    allowed = ('aeioupt')
    return rec_dubz('', seq, allowed=allowed)

def rec_dubz(prev, seq, allowed='aeiou'):
    if not seq:
        return [prev]
    solutions = rec_dubz(prev + seq[0][0], seq[1:], allowed=allowed)
    if seq[0][0] in allowed and seq[0][1]:
        solutions += rec_dubz(prev + seq[0][0] * 2, seq[1:], allowed=allowed)
    return solutions

这实际上只是对可能单词的“解决方案空间”进行启发式修剪的深度优先搜索。启发式是,我们一次只允许一次重复,并且仅当它是有效的可重复字母时。最后应该有 2**n 个单词,其中 n 是字符串中重复“允许”字符的次数。

>>> find_dub_strs('jeeeeeep')
['jep', 'jeep']
>>> find_dub_strs('jeeeeeeppp')
['jep', 'jepp', 'jeep', 'jeepp']
>>> find_dub_strs('jeeeeeeppphhhht')
['jepht', 'jeppht', 'jeepht', 'jeeppht']

Here's a recursive solution using groupby. I've left it up to you which characters you want to be able to repeat (defaults to vowels only though):

from itertools import groupby

def find_dub_strs(mystring):
    grp = groupby(mystring)
    seq = [(k, len(list(g)) >= 2) for k, g in grp]
    allowed = ('aeioupt')
    return rec_dubz('', seq, allowed=allowed)

def rec_dubz(prev, seq, allowed='aeiou'):
    if not seq:
        return [prev]
    solutions = rec_dubz(prev + seq[0][0], seq[1:], allowed=allowed)
    if seq[0][0] in allowed and seq[0][1]:
        solutions += rec_dubz(prev + seq[0][0] * 2, seq[1:], allowed=allowed)
    return solutions

This is really just a heuristically pruned depth-first search into your "solution space" of possible words. The heuristic is that we only allow a single repeat at a time, and only if it is a valid repeatable letter. You should end up with 2**n words at the end, where n is he number times an "allowed" character was repeated in your string.

>>> find_dub_strs('jeeeeeep')
['jep', 'jeep']
>>> find_dub_strs('jeeeeeeppp')
['jep', 'jepp', 'jeep', 'jeepp']
>>> find_dub_strs('jeeeeeeppphhhht')
['jepht', 'jeppht', 'jeepht', 'jeeppht']
梦与时光遇 2024-12-01 11:55:08

使用正则表达式:

>>> import re
>>> re.sub(r'(.)\1\1+', r'\1\1', 'jeeeep')
'jeep'

use a regular expression:

>>> import re
>>> re.sub(r'(.)\1\1+', r'\1\1', 'jeeeep')
'jeep'
请别遗忘我 2024-12-01 11:55:08

使用groupby的单个字符的解决方案:

>>> from itertools import groupby
>>> s = 'jeeeeeeeep'
>>> ''.join(c for c, unused in groupby(s))
'jep'

以及最多两个字符的解决方案:

''.join(''.join(list(group)[:2]) for unused, group in groupby(s))

The solution for a single character using groupby:

>>> from itertools import groupby
>>> s = 'jeeeeeeeep'
>>> ''.join(c for c, unused in groupby(s))
'jep'

And the one for maximum of two characters:

''.join(''.join(list(group)[:2]) for unused, group in groupby(s))
成熟的代价 2024-12-01 11:55:08

这是一个Sh+Perl解决方案,恐怕我不懂Python:

echo jjjjeeeeeeeeppppp | perl -ne 's/(.)\1+/\1\1/g; print $_;'

关键是找到(.)\1+并用\1\1替换它的正则表达式,全球范围内。

Here is a Sh+Perl solution, I'm afraid I don't know Python:

echo jjjjeeeeeeeeppppp | perl -ne 's/(.)\1+/\1\1/g; print $_;'

The key is the regex that finds (.)\1+ and replaces it by \1\1, globally.

沒落の蓅哖 2024-12-01 11:55:08

使用正则表达式和按键事件!

Use regular expressions along with a key press event!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文