删除连续的重复字母
寻找一种快速方法,当重复项彼此相邻时将其限制为最多 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是使用
groupby
的递归解决方案。我已经将您希望能够重复哪些字符留给您了(尽管默认为元音):这实际上只是对可能单词的“解决方案空间”进行启发式修剪的深度优先搜索。启发式是,我们一次只允许一次重复,并且仅当它是有效的可重复字母时。最后应该有 2**n 个单词,其中 n 是字符串中重复“允许”字符的次数。
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):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.
使用正则表达式:
use a regular expression:
使用
groupby
的单个字符的解决方案:以及最多两个字符的解决方案:
The solution for a single character using
groupby
:And the one for maximum of two characters:
这是一个Sh+Perl解决方案,恐怕我不懂Python:
关键是找到
(.)\1+
并用\1\1替换它的正则表达式
,全球范围内。Here is a Sh+Perl solution, I'm afraid I don't know Python:
The key is the regex that finds
(.)\1+
and replaces it by\1\1
, globally.使用正则表达式和按键事件!
Use regular expressions along with a key press event!